Network video of an android instance

Source: Internet
Author: User

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:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_height = "fill_parent"
  4. Android: layout_width = "fill_parent">
  5. <Surfaceview
  6. Android: Id = "@ + ID/surfaceview1"
  7. Android: layout_height = "fill_parent"
  8. Android: layout_width = "fill_parent">
  9. </Surfaceview>
  10. <Linearlayout
  11. Android: layout_height = "wrap_content"
  12. Android: layout_width = "fill_parent"
  13. Android: layout_gravity = "bottom"
  14. Android: Orientation = "vertical">
  15. <Linearlayout
  16. Android: Orientation = "horizontal"
  17. Android: layout_gravity = "center_horizontal"
  18. Android: layout_margintop = "4.0dip"
  19. Android: layout_height = "wrap_content"
  20. Android: layout_width = "wrap_content">
  21. <Button
  22. Android: layout_width = "wrap_content"
  23. Android: layout_height = "wrap_content"
  24. Android: Id = "@ + ID/btnplayurl"
  25. Android: text = "playing online videos">
  26. </Button>
  27. <Button Android: layout_height = "wrap_content"
  28. Android: Id = "@ + ID/btnpause"
  29. Android: text = "paused"
  30. Android: layout_width = "80dip">
  31. </Button>
  32. <Button
  33. Android: layout_height = "wrap_content"
  34. Android: layout_width = "80dip"
  35. Android: text = "stop"
  36. Android: Id = "@ + ID/btnstop"> </button>
  37. </Linearlayout>
  38. <Linearlayout Android: Orientation = "horizontal"
  39. Android: layout_width = "fill_parent"
  40. Android: layout_height = "wrap_content"
  41. Android: layout_marginbottom = "20dip">
  42. <Seekbar
  43. Android: paddingright = "10dip"
  44. Android: layout_gravity = "center_vertical"
  45. Android: paddingleft = "10dip"
  46. Android: layout_weight = "1.0"
  47. Android: layout_height = "wrap_content"
  48. Android: layout_width = "wrap_content"
  49. Android: Id = "@ + ID/skbprogress"
  50. Android: max = "100" type = "codeph" text = "/codeph"> </seekbar>
  51. </Linearlayout>
  52. </Linearlayout>
  53. </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:

  1. Videowidth = mediaplayer. getvideowidth ();
  2. Videoheight = mediaplayer. getvideoheight ();
  3. If (videoheight! = 0 & videowidth! = 0 ){
  4. Arg0.start ();
  5. }

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:

  1. Package EOE. Demo;
  2. Import java. Io. ioexception;
  3. Import java. util. timer;
  4. Import java. util. timertask;
  5. Import Android. Media. audiomanager;
  6. Import Android. Media. mediaplayer;
  7. Import Android. Media. mediaplayer. onbufferingupdatelistener;
  8. Import Android. Media. mediaplayer. oncompletionlistener;
  9. Import Android. OS. Handler;
  10. Import Android. OS. message;
  11. Import Android. util. log;
  12. Import Android. View. surfaceholder;
  13. Import Android. View. surfaceview;
  14. Import Android. widget. seekbar;
  15. Public class player implements onbufferingupdatelistener,
  16. Oncompletionlistener, mediaplayer. onpreparedlistener,
  17. Surfaceholder. Callback {
  18. Private int videowidth;
  19. Private int videoheight;
  20. Public mediaplayer;
  21. Private surfaceholder;
  22. Private seekbar skbprogress;
  23. Private timer mtimer = new timer ();
  24. Public player (surfaceview, seekbar skbprogress ){
  25. This. skbprogress = skbprogress;
  26. Surfaceholder = surfaceview. getholder ();
  27. Surfaceholder. addcallback (this );
  28. Surfaceholder. settype (surfaceholder. surface_type_push_buffers );
  29. Mtimer. Schedule (mtimerjob, 0, 1000 );
  30. }
  31. /* Update the progress bar through the timer and handler */
  32. Timertask mtimertask = new timertask (){
  33. @ Override
  34. Public void run (){
  35. If (mediaplayer = NULL)
  36. Return;
  37. If (mediaplayer. isplaying () & skbprogress. ispressed () = false ){
  38. Handleprogress. sendemptymessage (0 );
  39. }
  40. }
  41. };
  42. Handler handleprogress = new handler (){
  43. Public void handlemessage (Message MSG ){
  44. Int position = mediaplayer. getcurrentposition ();
  45. Int duration = mediaplayer. getduration ();
  46. If (duration> 0 ){
  47. Long Pos = skbprogress. getmax () * position/duration;
  48. Skbprogress. setprogress (INT) POS );
  49. }
  50. };
  51. };
  52. Public void play (){
  53. Mediaplayer. Start ();
  54. }
  55. Public void playurl (string videourl ){
  56. Try {
  57. Mediaplayer. Reset ();
  58. Mediaplayer. setdatasource (videourl );
  59. Mediaplayer. Prepare (); // play automatically after prepare
  60. // Mediaplayer. Start ();
  61. } Catch (illegalargumentexception e ){
  62. // Todo auto-generated Catch Block
  63. E. printstacktrace ();
  64. } Catch (illegalstateexception e ){
  65. // Todo auto-generated Catch Block
  66. E. printstacktrace ();
  67. } Catch (ioexception e ){
  68. // Todo auto-generated Catch Block
  69. E. printstacktrace ();
  70. }
  71. }
  72. Public void pause (){
  73. Mediaplayer. Pause ();
  74. }
  75. Public void stop (){
  76. If (mediaplayer! = NULL ){
  77. Mediaplayer. Stop ();
  78. Mediaplayer. Release ();
  79. Mediaplayer = NULL;
  80. }
  81. }
  82. @ Override
  83. Public void surfacechanged (surfaceholder arg0, int arg1, int arg2, int arg3 ){
  84. Log. E ("mediaplayer", "Surface Changed ");
  85. }
  86. @ Override
  87. Public void surfacecreated (surfaceholder arg0 ){
  88. Try {
  89. Mediaplayer = new mediaplayer ();
  90. Mediaplayer. setdisplay (surfaceholder );
  91. Mediaplayer. setaudiostreamtype (audiomanager. stream_music );
  92. Mediaplayer. setonbufferingupdatelistener (this );
  93. Mediaplayer. setonpreparedlistener (this );
  94. } Catch (exception e ){
  95. Log. E ("mediaplayer", "error", e );
  96. }
  97. Log. E ("mediaplayer", "surface created ");
  98. }
  99. @ Override
  100. Public void surfacedestroyed (surfaceholder arg0 ){
  101. Log. E ("mediaplayer", "surface destroyed ");
  102. }
  103. @ Override
  104. /**
  105. * Playing Through onprepared
  106. */
  107. Public void onprepared (mediaplayer arg0 ){
  108. Videowidth = mediaplayer. getvideowidth ();
  109. Videoheight = mediaplayer. getvideoheight ();
  110. If (videoheight! = 0 & videowidth! = 0 ){
  111. Arg0.start ();
  112. }
  113. Log. E ("mediaplayer", "onprepared ");
  114. }
  115. @ Override
  116. Public void oncompletion (mediaplayer arg0 ){
  117. // Todo auto-generated method stub
  118. }
  119. @ Override
  120. Public void onbufferingupdate (mediaplayer arg0, int bufferingprogress ){
  121. Skbprogress. setsecondaryprogress (bufferingprogress );
  122. Int currentprogress = skbprogress. getmax () * mediaplayer. getcurrentposition ()/mediaplayer. getduration ();
  123. Log. E (currentprogress + "% play", bufferingprogress + "% buffer ");
  124. }
  125. }

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:

  1. Package EOE. Demo;
  2. Import Android. App. activity;
  3. Import Android. content. PM. activityinfo;
  4. Import android.net. Uri;
  5. Import Android. OS. Bundle;
  6. Import Android. util. log;
  7. Import Android. View. surfaceview;
  8. Import Android. View. view;
  9. Import Android. View. View. onclicklistener;
  10. Import Android. widget. Button;
  11. Import Android. widget. seekbar;
  12. Public class test_videoplayer extends activity {
  13. Private surfaceview;
  14. Private button btnpause, btnplayurl, btnstop;
  15. Private seekbar skbprogress;
  16. Private player;
  17. /** Called when the activity is first created .*/
  18. @ Override
  19. Public void oncreate (bundle savedinstancestate ){
  20. Super. oncreate (savedinstancestate );
  21. Setcontentview (R. layout. Main );
  22. Setrequestedorientation (activityinfo. screen_orientation_landscape );
  23. Surfaceview = (surfaceview) This. findviewbyid (R. Id. surfaceview1 );
  24. Btnplayurl = (button) This. findviewbyid (R. Id. btnplayurl );
  25. Btnplayurl. setonclicklistener (New clickevent ());
  26. Btnpause = (button) This. findviewbyid (R. Id. btnpause );
  27. Btnpause. setonclicklistener (New clickevent ());
  28. Btnstop = (button) This. findviewbyid (R. Id. btnstop );
  29. Btnstop. setonclicklistener (New clickevent ());
  30. Skbprogress = (seekbar) This. findviewbyid (R. Id. skbprogress );
  31. Skbprogress. setonseekbarchangelistener (New seekbarchangeevent ());
  32. Player = new player (surfaceview, skbprogress );
  33. }
  34. Class clickevent implements onclicklistener {
  35. @ Override
  36. Public void onclick (view arg0 ){
  37. If (arg0 = btnpause ){
  38. Player. Pause ();
  39. } Else if (arg0 = btnplayurl ){
  40. String url = "http://daily3gp.com/vids/family_guy_penis_car.3gp ";
  41. Player. playurl (URL );
  42. } Else if (arg0 = btnstop ){
  43. Player. Stop ();
  44. }
  45. }
  46. }
  47. Class seekbarchangeevent implements seekbar. onseekbarchangelistener {
  48. Int progress;
  49. @ Override
  50. Public void onprogresschanged (seekbar, int progress,
  51. Boolean fromuser ){
  52. // Originally (Progress/seekbar. getmax () * player. mediaplayer. getduration ()
  53. This. Progress = progress * player. mediaplayer. getduration ()
  54. /Seekbar. getmax ();
  55. }
  56. @ Override
  57. Public void onstarttrackingtouch (seekbar ){
  58. }
  59. @ Override
  60. Public void onstoptrackingtouch (seekbar ){
  61. // Seekto () is a number relative to the video time, not a number relative to seekbar. getmax ().
  62. Player. mediaplayer. seekto (Progress );
  63. }
  64. }
  65. }

Copy code

Related Article

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.