Android Streaming Media Player

Source: Internet
Author: User

Today, I was very happy. Many people on the Forum asked me how to write a streaming media player? Do two players need to rotate in turn? Because Android does not provide stream creation forms like j2's, it is still not very easy to write in short. Recently, we are also studying Android streaming media playback. Here we provide a demo written a long time ago, for mutual learning and reference.

 

/** <Br/> * the android interface still cannot be used to directly create a player through a stream, but Android provides a comprehensive file system interface, now we use a new solution: <br> <br/> * <1>, write the downloaded data to a temporary file <br> <br/> * <2> and create a player from the temporary file <br> <br/> * todo: <br> <br/> * <1>. Clear unused temporary files. <Br> <br/> * <2> to suspend logical optimization. --- Throw an exception .... <Br> <br/> * <3> logical processing of multipart download, which is related to <2>. <Br> <br/> * <4>, overall Optimization <br/> * @ author shenrenkui <br/> */<br/> public class audioplayer implements onerrorlistener, onbufferingupdatelistener, <br/> mediaplayer. oncompletionlistener {<br/> Private Static final string tag = "audioplayer"; <br/> private mediaplayer mplayer; <br/> private string current; <br/> Private Static final int min_buff = 100*1024; <br/> private int totalkbre AD = 0; <br/> private handler = new handler (); <br/> private file dltempfile; <br/> private file bufftempfile; <br/> private final string temp_download_file_name = "tempmediadata"; <br/> private final string temp_buff_file_name = "tempbufferdata"; <br/> private final string file_postfix = ". dat "; <br/> private final int per_read = 1024; <br/> private Boolean pause; <br/> private Boolean St OP; <br/> private final int unknown_length =-1; <br/> private handler mhandler = NULL; </P> <p> Public void sethandler (handler) {<br/> mhandler = handler; <br/>}</P> <p> Public void play (final string path) {<br/> downloadover = false; <br/> totalkbread = 0; <br/> try {<br/> log. V (TAG, "playing:" + path); <br/> If (path. equals (current) & mplayer! = NULL) {<br/> mplayer. start (); <br/> return; <br/>}< br/> current = path; <br/> mplayer = NULL; <br/> New playthread (current ). start (); <br/>}catch (exception e) {</P> <p >}< br/>}</P> <p> private void setlistener () {<br/> If (mplayer! = NULL) {<br/> mplayer. setonerrorlistener (this); <br/> mplayer. setonbufferingupdatelistener (this); <br/> mplayer. setoncompletionlistener (this); <br/>}</P> <p>/** <br/> * download data, multipart download <br/> * todo: networking and segmentation <br/> * @ Param mediaurl <br/> * @ Param start <br/> * @ Param end <br/> */<br/> private void playfromnet (string mediaurl, int start, int end) {<br/> urlconnection Cn = NULL; <br/> fileoutput Stream out = NULL; <br/> inputstream is = NULL; <br/> try {<br/> Cn = new URL (mediaurl ). openconnection (); <br/> CN. connect (); <br/> is = cn. getinputstream (); <br/> int medialength = cn. getcontentlength (); <br/> If (is = NULL) {<br/> return; <br/>}< br/> deletetempfile (true ); <br/> dltempfile = file. createtempfile (temp_download_file_name, <br/> file_postfix); <br/> out = new fileoutputstream (DL Tempfile); <br/> byte Buf [] = new byte [per_read]; <br/> int readlength = 0; <br/> while (readlength! =-1 &&! Stop) {<br/> If (pause) {<br/> try {<br/> thread. sleep (1000); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> continue; <br/>}< br/> readlength = is. read (BUF); <br/> If (readlength> 0) {<br/> try {<br/> out. write (BUF, 0, readlength); <br/> totalkbread + = readlength; <br/>} catch (exception e) {<br/> log. E (TAG, E. tostring (); <br/>}< br/> dealwithb Ufferdata (); <br/>}< br/> If (totalkbread = medialength) {<br/> downloadover = true; <br/> dealwithlastdata (); <br/> // delete a temporary file <br/> If (dltempfile! = NULL & dltempfile. exists () {<br/> dltempfile. delete (); <br/>}< br/>} catch (malformedurlexception e) {<br/> log. E (TAG, E. tostring (); <br/>}catch (ioexception e) {<br/> log. E (TAG, E. tostring (); <br/>}finally {<br/> If (OUT! = NULL) {<br/> try {<br/> out. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> If (is! = NULL) {<br/> try {<br/> is. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> private Boolean downloadover = false; <br/> private Boolean wasplayed = false; </P> <p> private void dealwithbufferdata () {<br/> If (mplayer = NULL |! Wasplayed) {<br/> If (totalkbread> = min_buff) {<br/> try {<br/> startmediaplayer (); <br/>} catch (exception E) {<br/>}< br/>} else if (mplayer. getduration ()-mplayer. getcurrentposition () <= 1000) {<br/> deletetempfile (true); <br/> transferbuffertomediaplayer (); <br/>}</P> <p> private void startmediaplayer () {<br/> try {<br/> deletetempfile (true ); <br/> bufftempfile = fil E. createtempfile (temp_buff_file_name, <br/> file_postfix); <br/> filesystemutil. copyfile (dltempfile, bufftempfile); <br/> mplayer = new mediaplayer (); <br/> setlistener (); <br/> mplayer. setdatasource (bufftempfile. getabsolutepath (); <br/> mplayer. prepare (); <br/> mplayer. start (); <br/> wasplayed = true; <br/>} catch (ioexception E) {</P> <p >}< br/>}</P> <p> private void transferbuffertomediaplaye R () {<br/> try {<br/> Boolean wasplaying = mplayer. isplaying (); <br/> int curposition = mplayer. getcurrentposition (); <br/> mplayer. pause (); <br/> bufftempfile = file. createtempfile (temp_buff_file_name, <br/> file_postfix); <br/> filesystemutil. copyfile (dltempfile, bufftempfile); <br/> mplayer = new mediaplayer (); <br/> mplayer. setdatasource (bufftempfile. getabsolutepath (); <br/> mplayer. prepare (); <Br/> mplayer. seekto (curposition); <br/> Boolean atendoffile = mplayer. getduration () <br/>-mplayer. getcurrentposition () <= 1000; <br/> If (wasplaying | atendoffile) {<br/> mplayer. start (); <br/>}< br/>} catch (exception E) {<br/>}</P> <p> private void dealwithlastdata () {<br/> runnable Updater = new runnable () {<br/> Public void run () {<br/> transferbuffertomediaplayer (); <br />}< Br/>}; <br/> handler. post (Updater); <br/>}</P> <p> Public void oncompletion (mediaplayer MP) {<br/> If (mhandler! = NULL) {<br/> mhandler. sendemptymessage (preferences. media_ended); <br/>}</P> <p> Public Boolean onerror (mediaplayer, int what, int extra) {<br/> If (mediaplayer! = NULL) {<br/> mediaplayer. Stop (); <br/> mediaplayer. Release (); <br/>}< br/> If (mhandler! = NULL) {<br/> mhandler. sendemptymessage (preferences. media_error); <br/>}< br/> return true; <br/>}</P> <p> Public void onbufferingupdate (mediaplayer arg0, int percent) {<br/> log. D (TAG, "onbufferingupdate called ---> percent:" + percent); <br/> If (mhandler! = NULL) {<br/> mhandler. sendemptymessage (preferences. emdia_buff_change); <br/>}</P> <p> private class playthread extends thread {<br/> private string URL; </P> <p> playthread (string URL) {<br/> This. url = URL; <br/>}</P> <p> Public void run () {<br/> If (! Urlutil. isnetworkurl (URL) {<br/> mplayer = new mediaplayer (); <br/> setlistener (); <br/> try {<br/> // If (URL. startswith ("content: //") {<br/> // mplayer. setdatasource (mediaplayservice. this, Uri <br/> //. parse (URL); <br/> //} else {<br/> mplayer. setdatasource (URL); <br/>/}< br/> mplayer. setaudiostreamtype (audiomanager. stream_music); <br/> setvolume (0f); <br/> mplayer. prepare (); <br/> mplayer. start (); <br/>}catch (illegalargumentexception e) {<br/> log. E (TAG, E. tostring (); <br/>} catch (illegalstateexception e) {<br/> log. E (TAG, E. tostring (); <br/>}catch (ioexception e) {<br/> log. E (TAG, E. tostring (); <br/>}< br/>} else {<br/> playfromnet (URL, 0, unknown_length ); <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.