The last time mediaplayer played Network Audio, it introduced mediaplayer's method of Network Audio buffering and progress bar control. This time I will explain how mediaplayer played network video. Playing a network video requires a surfaceview more than playing the Network Audio. After you are familiar with playing the network audio on the mediaplayer, I believe you can quickly learn how to play the network video. Let's take a look at the program running in this article:
:
The source code of Main. XML is as follows:
Java code:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: layout_height = "fill_parent"
- Android: layout_width = "fill_parent">
- <Surfaceview
- Android: Id = "@ + ID/surfaceview1"
- Android: layout_height = "fill_parent"
- Android: layout_width = "fill_parent">
- </Surfaceview>
- <Linearlayout
- Android: layout_height = "wrap_content"
- Android: layout_width = "fill_parent"
- Android: layout_gravity = "bottom"
- Android: Orientation = "vertical">
- <Linearlayout
- Android: Orientation = "horizontal"
- Android: layout_gravity = "center_horizontal"
- Android: layout_margintop = "4.0dip"
- Android: layout_height = "wrap_content"
- Android: layout_width = "wrap_content">
- <Button
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: Id = "@ + ID/btnplayurl"
- Android: text = "playing online videos">
- </Button>
- <Button Android: layout_height = "wrap_content"
- Android: Id = "@ + ID/btnpause"
- Android: text = "paused"
- Android: layout_width = "80dip">
- </Button>
- <Button
- Android: layout_height = "wrap_content"
- Android: layout_width = "80dip"
- Android: text = "stop"
- Android: Id = "@ + ID/btnstop"> </button>
- </Linearlayout>
- <Linearlayout Android: Orientation = "horizontal"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: layout_marginbottom = "20dip">
- <Seekbar
- Android: paddingright = "10dip"
- Android: layout_gravity = "center_vertical"
- Android: paddingleft = "10dip"
- Android: layout_weight = "1.0"
- Android: layout_height = "wrap_content"
- Android: layout_width = "wrap_content"
- Android: Id = "@ + ID/skbprogress"
- Android: max = "100" type = "codeph" text = "/codeph"> </seekbar>
- </Linearlayout>
- </Linearlayout>
- </Framelayout>
Copy code
Player. java is the core of this article, player. java implements "progress bar Update", "data buffer", and "surfaceholder lifecycle". "surfaceholder lifecycle" is the biggest difference between video and audio playback (), surfacedestroyed (), surfacechanged () can create \ release some resources. Note the following:
Java code:
- Videowidth = mediaplayer. getvideowidth ();
- Videoheight = mediaplayer. getvideoheight ();
- If (videoheight! = 0 & videowidth! = 0 ){
- Arg0.start ();
- }
Copy code
Some videos cannot be played by the android player. When the video cannot be played, videoheight = 0 and videowidth = 0 are used to determine whether to play the video.
Player. Java source code is as follows:
Java code:
- Package EOE. Demo;
- Import java. Io. ioexception;
- Import java. util. timer;
- Import java. util. timertask;
- Import Android. Media. audiomanager;
- Import Android. Media. mediaplayer;
- Import Android. Media. mediaplayer. onbufferingupdatelistener;
- Import Android. Media. mediaplayer. oncompletionlistener;
- Import Android. OS. Handler;
- Import Android. OS. message;
- Import Android. util. log;
- Import Android. View. surfaceholder;
- Import Android. View. surfaceview;
- Import Android. widget. seekbar;
- Public class player implements onbufferingupdatelistener,
- Oncompletionlistener, mediaplayer. onpreparedlistener,
- Surfaceholder. Callback {
- Private int videowidth;
- Private int videoheight;
- Public mediaplayer;
- Private surfaceholder;
- Private seekbar skbprogress;
- Private timer mtimer = new timer ();
- Public player (surfaceview, seekbar skbprogress ){
- This. skbprogress = skbprogress;
- Surfaceholder = surfaceview. getholder ();
- Surfaceholder. addcallback (this );
- Surfaceholder. settype (surfaceholder. surface_type_push_buffers );
- Mtimer. Schedule (mtimerjob, 0, 1000 );
- }
- /* Update the progress bar through the timer and handler */
- Timertask mtimertask = new timertask (){
- @ Override
- Public void run (){
- If (mediaplayer = NULL)
- Return;
- If (mediaplayer. isplaying () & skbprogress. ispressed () = false ){
- Handleprogress. sendemptymessage (0 );
- }
- }
- };
- Handler handleprogress = new handler (){
- Public void handlemessage (Message MSG ){
- Int position = mediaplayer. getcurrentposition ();
- Int duration = mediaplayer. getduration ();
- If (duration> 0 ){
- Long Pos = skbprogress. getmax () * position/duration;
- Skbprogress. setprogress (INT) POS );
- }
- };
- };
- Public void play (){
- Mediaplayer. Start ();
- }
- Public void playurl (string videourl ){
- Try {
- Mediaplayer. Reset ();
- Mediaplayer. setdatasource (videourl );
- Mediaplayer. Prepare (); // play automatically after prepare
- // Mediaplayer. Start ();
- } Catch (illegalargumentexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- } Catch (illegalstateexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- } Catch (ioexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- }
- Public void pause (){
- Mediaplayer. Pause ();
- }
- Public void stop (){
- If (mediaplayer! = NULL ){
- Mediaplayer. Stop ();
- Mediaplayer. Release ();
- Mediaplayer = NULL;
- }
- }
- @ Override
- Public void surfacechanged (surfaceholder arg0, int arg1, int arg2, int arg3 ){
- Log. E ("mediaplayer", "Surface Changed ");
- }
- @ Override
- Public void surfacecreated (surfaceholder arg0 ){
- Try {
- Mediaplayer = new mediaplayer ();
- Mediaplayer. setdisplay (surfaceholder );
- Mediaplayer. setaudiostreamtype (audiomanager. stream_music );
- Mediaplayer. setonbufferingupdatelistener (this );
- Mediaplayer. setonpreparedlistener (this );
- } Catch (exception e ){
- Log. E ("mediaplayer", "error", e );
- }
- Log. E ("mediaplayer", "surface created ");
- }
- @ Override
- Public void surfacedestroyed (surfaceholder arg0 ){
- Log. E ("mediaplayer", "surface destroyed ");
- }
- @ Override
- /**
- * Playing Through onprepared
- */
- Public void onprepared (mediaplayer arg0 ){
- Videowidth = mediaplayer. getvideowidth ();
- Videoheight = mediaplayer. getvideoheight ();
- If (videoheight! = 0 & videowidth! = 0 ){
- Arg0.start ();
- }
- Log. E ("mediaplayer", "onprepared ");
- }
- @ Override
- Public void oncompletion (mediaplayer arg0 ){
- // Todo auto-generated method stub
- }
- @ Override
- Public void onbufferingupdate (mediaplayer arg0, int bufferingprogress ){
- Skbprogress. setsecondaryprogress (bufferingprogress );
- Int currentprogress = skbprogress. getmax () * mediaplayer. getcurrentposition ()/mediaplayer. getduration ();
- Log. E (currentprogress + "% play", bufferingprogress + "% buffer ");
- }
- }
Copy code
Test_videoplayer.java is the main program that calls the player class. The key part is the seekbarchangeevent drag event: the progress of seekbar is 0 ~ The number in seekbar. getmax (), while the mediaplayer. seekto () parameter is 0 ~ Mediaplayer. getduration (). Therefore, the mediaplayer. seekto () parameter is (Progress/seekbar. getmax () * mediaplayer. getduration ().
The source code of test_videoplayer.java is as follows:
Java code:
- Package EOE. Demo;
- Import Android. App. activity;
- Import Android. content. PM. activityinfo;
- Import android.net. Uri;
- Import Android. OS. Bundle;
- Import Android. util. log;
- Import Android. View. surfaceview;
- Import Android. View. view;
- Import Android. View. View. onclicklistener;
- Import Android. widget. Button;
- Import Android. widget. seekbar;
- Public class test_videoplayer extends activity {
- Private surfaceview;
- Private button btnpause, btnplayurl, btnstop;
- Private seekbar skbprogress;
- Private player;
- /** Called when the activity is first created .*/
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Setrequestedorientation (activityinfo. screen_orientation_landscape );
- Surfaceview = (surfaceview) This. findviewbyid (R. Id. surfaceview1 );
- Btnplayurl = (button) This. findviewbyid (R. Id. btnplayurl );
- Btnplayurl. setonclicklistener (New clickevent ());
- Btnpause = (button) This. findviewbyid (R. Id. btnpause );
- Btnpause. setonclicklistener (New clickevent ());
- Btnstop = (button) This. findviewbyid (R. Id. btnstop );
- Btnstop. setonclicklistener (New clickevent ());
- Skbprogress = (seekbar) This. findviewbyid (R. Id. skbprogress );
- Skbprogress. setonseekbarchangelistener (New seekbarchangeevent ());
- Player = new player (surfaceview, skbprogress );
- }
- Class clickevent implements onclicklistener {
- @ Override
- Public void onclick (view arg0 ){
- If (arg0 = btnpause ){
- Player. Pause ();
- } Else if (arg0 = btnplayurl ){
- String url = "http://daily3gp.com/vids/family_guy_penis_car.3gp ";
- Player. playurl (URL );
- } Else if (arg0 = btnstop ){
- Player. Stop ();
- }
- }
- }
- Class seekbarchangeevent implements seekbar. onseekbarchangelistener {
- Int progress;
- @ Override
- Public void onprogresschanged (seekbar, int progress,
- Boolean fromuser ){
- // Originally (Progress/seekbar. getmax () * player. mediaplayer. getduration ()
- This. Progress = progress * player. mediaplayer. getduration ()
- /Seekbar. getmax ();
- }
- @ Override
- Public void onstarttrackingtouch (seekbar ){
- }
- @ Override
- Public void onstoptrackingtouch (seekbar ){
- // Seekto () is a number relative to the video time, not a number relative to seekbar. getmax ().
- Player. mediaplayer. seekto (Progress );
- }
- }
- }
Copy code