Play video using MediaPlayer and Surfaceview

Source: Internet
Author: User

The previous introduction of the use of Videoview play video, using Videoview play video simple and convenient, but the habit of using MediaPlayer developers or use MediaPlayer; MediaPlayer itself is mainly used to play audio, not provide video output screen, need to rely on Surfaceview,videoview is its sub-class;

To play a video step:
"1" Layout file join Surfaceview

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

"3" Create MediaPlayer object, load video file

player.setDataSource(Environment.getExternalStorageDirectory"/20150915_160202.mp4");

"4" Get surfaceholder Add listener

  SurfaceHolder surfaceHolder=mSurfaceView.getHolder();                    surfaceHolder.setKeepScreenOn(true);

"5" Calls MediaPlayer's Setdisplay (Surfaceholder sh)

 player.setDisplay(surfaceHolder);//设置视频播放位置

"6" Start (), Stop (), pause ()

The following is the program source code

 Public  class mainactivity extends Activity {    PrivateSurfaceview Msurfaceview;PrivateButton Mbutton;PrivateMediaPlayer player;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Msurfaceview = (Surfaceview) Findviewbyid (R.id.surfaceview);        Mbutton = (Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {if(Player = =NULL) {player =NewMediaPlayer (); } player.reset ();Try{Player.setdatasource (environment.getexternalstoragedirectory () +"/20150915_160202.mp4"); Player.setaudiostreamtype (Audiomanager.stream_music);//Set playback sound typeSurfaceholder Surfaceholder=msurfaceview.getholder (); Surfaceholder.setkeepscreenon (true); Player.setdisplay (Surfaceholder);//Set video playback locationPlayer.prepare ();//PreparationPlayer.setonpreparedlistener (NewMediaplayer.onpreparedlistener () {@Override                         Public void onprepared(MediaPlayer MP) {Mp.start ();//Start playback}                    }); }Catch(IOException e)                {E.printstacktrace ();    }            }        }); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Play video using MediaPlayer and Surfaceview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.