Android improves mediaplayer by 21st to play online videos

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

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:

In this article, the program video from the http://daily3gp.com, you can replace the program in the video link, try other films.

The source code of Main. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <framelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_height = "fill_parent" Android: layout_width = "fill_parent"> <br/> <surfaceview Android: id = "@ + ID/surfaceview1" <br/> Android: layout_height = "fill_parent" Android: layout_width = "fill_parent"> </surfaceview> <br/> <linearlayout Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: layout_gravity = "bottom" <br/> Android: orientation = "vertical"> <br/> <linearlayout Android: Orientation = "horizontal" <br/> Android: layout_gravity = "center_horizontal" Android: layout_margintop = "4.0dip" <br/> Android: layout_height = "wrap_content" Android: layout_width = "wrap_content"> <br/> <button Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: Id = "@ + ID/btnplayurl" <br/> Android: TEXT = "playing online videos"> </button> <br/> <button Android: layout_height = "wrap_content" Android: id = "@ + ID/btnpause" <br/> Android: text = "pause" Android: layout_width = "80dip"> </button> <br/> <button Android: layout_height = "wrap_content" <br/> Android: layout_width = "80dip" Android: text = "stop" Android: id = "@ + ID/btnstop"> </button> <br/> </linearlayout> <br/> <linearlayout Android: orientation = "horizontal" <br/> Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" <br/> Android: layout_marginbottom = "20dip"> <br/> <seekbar Android: paddingright = "10dip" Android: layout_gravity = "center_vertical" <br/> Android: paddingleft = "10dip" Android: layout_weight = "1.0" <br/> Android: layout_height = "wrap_content" Android: layout_width = "wrap_content" <br/> Android: Id = "@ + ID/skbprogress" Android: max = "100"> </seekbar> <br/> </linearlayout> <br/> </framelayout>

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:

Videowidth = mediaplayer. getvideowidth (); <br/> videoheight = mediaplayer. getvideoheight (); <br/> If (videoheight! = 0 & videowidth! = 0) {<br/> arg0.start (); <br/>}

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:

Package COM. videoplayer; </P> <p> Import Java. io. ioexception; <br/> Import Java. util. timer; <br/> Import Java. util. timertask; <br/> Import android. media. audiomanager; <br/> Import android. media. mediaplayer; <br/> Import android. media. mediaplayer. onbufferingupdatelistener; <br/> Import android. media. mediaplayer. oncompletionlistener; <br/> Import android. OS. handler; <br/> Import android. OS. message; <br/> Import Android. util. log; <br/> Import android. view. surfaceholder; <br/> Import android. view. surfaceview; <br/> Import android. widget. seekbar; </P> <p> public class player implements onbufferingupdatelistener, <br/> oncompletionlistener, mediaplayer. onpreparedlistener, <br/> surfaceholder. callback {<br/> private int videowidth; <br/> private int videoheight; <br/> Public mediaplayer; <br/> private surfac Eholder surfaceholder; <br/> private seekbar skbprogress; <br/> private timer mtimer = new timer (); <br/> Public player (surfaceview, seekbar skbprogress) <br/>{< br/> This. skbprogress = skbprogress; <br/> surfaceholder = surfaceview. getholder (); <br/> surfaceholder. addcallback (this); <br/> surfaceholder. settype (surfaceholder. surface_type_push_buffers); <br/> mtimer. schedule (mtimertasks, 0, 1000); <br/ >}</P> <p> /****************************** * *********************** <br/> * update the progress bar through the timer and handler. <br/> *************************************** * *************/<br/> timertask mtimertask = new timertask () {<br/> @ override <br/> Public void run () {<br/> If (mediaplayer = NULL) <br/> return; <br/> If (mediaplayer. isplaying () & skbprogress. ispressed () = false) {<br/> handleprogress. sendemptymessage (0); <br/>} <Br/>}< br/>}; </P> <p> handler handleprogress = new handler () {<br/> Public void handlemessage (Message MSG) {</P> <p> int position = mediaplayer. getcurrentposition (); <br/> int duration = mediaplayer. getduration (); </P> <p> If (duration> 0) {<br/> long Pos = skbprogress. getmax () * position/duration; <br/> skbprogress. setprogress (INT) POS); <br/>}< br/>}; <br/> }; <br/> //******************************* * ******************** </P> <p> Public void play () <br/>{< br/> mediaplayer. start (); <br/>}</P> <p> Public void playurl (string videourl) <br/>{< br/> try {<br/> mediaplayer. reset (); <br/> mediaplayer. setdatasource (videourl); <br/> mediaplayer. prepare (); // automatically play after prepare <br/> // mediaplayer. start (); <br/>}catch (illegalargumentexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br />} Catch (illegalstateexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p> Public void pause () <br/>{< br/> mediaplayer. pause (); <br/>}</P> <p> Public void stop () <br/>{< br/> If (mediaplayer! = NULL) {<br/> mediaplayer. stop (); <br/> mediaplayer. release (); <br/> mediaplayer = NULL; <br/>}</P> <p> @ override <br/> Public void surfacechanged (surfaceholder arg0, int arg1, int arg2, int arg3) {<br/> log. E ("mediaplayer", "Surface Changed"); <br/>}</P> <p> @ override <br/> Public void surfacecreated (surfaceholder arg0) {<br/> try {<br/> mediaplayer = new mediaplayer (); <br/> mediaplayer. setdisp Lay (surfaceholder); <br/> mediaplayer. setaudiostreamtype (audiomanager. stream_music); <br/> mediaplayer. setonbufferingupdatelistener (this); <br/> mediaplayer. setonpreparedlistener (this); <br/>} catch (exception e) {<br/> log. E ("mediaplayer", "error", e); <br/>}< br/> log. E ("mediaplayer", "surface created"); <br/>}</P> <p> @ override <br/> Public void surfacedestroyed (surfaceholder arg0) {<br/> log. E ("Media Player "," surface destroyed "); <br/>}</P> <p> @ override <br/>/** <br/> * Play through onprepared <br/> */<br/> Public void onprepared (mediaplayer arg0) {<br/> videowidth = mediaplayer. getvideowidth (); <br/> videoheight = mediaplayer. getvideoheight (); <br/> If (videoheight! = 0 & videowidth! = 0) {<br/> arg0.start (); <br/>}< br/> log. E ("mediaplayer", "onprepared"); <br/>}</P> <p> @ override <br/> Public void oncompletion (mediaplayer arg0) {<br/> // todo auto-generated method stub </P> <p >}</P> <p> @ override <br/> Public void onbufferingupdate (mediaplayer arg0, int bufferingprogress) {<br/> skbprogress. setsecondaryprogress (bufferingprogress); <br/> int currentprogress = skbprogress. getmax () * mediaplayer. getcurrentposition ()/mediaplayer. getduration (); <br/> log. E (currentprogress + "% play", bufferingprogress + "% buffer"); </P> <p >}< br/>

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:

 

Package COM. videoplayer; </P> <p> Import android. app. activity; <br/> Import android. content. PM. activityinfo; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. surfaceview; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. seekbar; </P> <p> public class test_videoplayer extends activity {<br/> private surfaceview; <br/> private button btnpause, btnplayurl, btnstop; <br/> private seekbar skbprogress; <br/> private player; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> setrequestedorientation (activityinfo. screen_orientation_landscape); <br/> surfaceview = (surfaceview) This. findviewbyid (R. id. surfaceview1); </P> <p> btnplayurl = (button) This. findviewbyid (R. id. btnplayurl); <br/> btnplayurl. setonclicklistener (New clickevent (); </P> <p> btnpause = (button) This. findviewbyid (R. id. btnpause); <br/> btnpause. setonclicklistener (New clickevent (); </P> <p> btnstop = (button) This. findviewbyid (R. id. btnstop); <br/> btnstop. setonclicklistener (New clickevent (); </P> <p> skbprogress = (seekbar) This. findviewbyid (R. id. skbprogress); <br/> skbprogress. setonseekbarchangelistener (New seekbarchangeevent (); <br/> player = new player (surfaceview, skbprogress ); </P> <p >}</P> <p> class clickevent implements onclicklistener {</P> <p> @ override <br/> Public void onclick (view arg0) {<br/> If (arg0 = btnpause) {<br/> player. pause (); <br/>}else if (arg0 = btnplayurl) {<br/> string url = "http://daily3gp.com/vids/family_guy_penis_car.3gp"; <br/> player. playurl (URL); <br/>}else if (arg0 = btnstop) {<br/> player. stop (); <br/>}</P> <p >}< br/>}</P> <p> class seekbarchangeevent implements seekbar. onseekbarchangelistener {<br/> int progress; </P> <p> @ override <br/> Public void onprogresschanged (seekbar, int progress, <br/> Boolean fromuser) {<br/> // originally (Progress/seekbar. getmax () * player. mediaplayer. getduration () <br/> This. progress = progress * player. mediaplayer. getduration () <br/>/seekbar. getmax (); <br/>}</P> <p> @ override <br/> Public void onstarttrackingtouch (seekbar) {</P> <p >}</P> <p> @ override <br/> Public void onstoptrackingtouch (seekbar) {<br/> // seekto () the parameter is a number relative to the video time, rather than a seekbar. number relative to getmax () <br/> player. mediaplayer. seekto (Progress); <br/>}</P> <p>}

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.