您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > android实现流媒体播放远程mp3文件代码
博客分类:Android开发Java代码1.packagecom.shadow.util;2.3.importjava.io.BufferedInputStream;4.importjava.io.BufferedOutputStream;5.importjava.io.File;6.importjava.io.FileInputStream;7.importjava.io.FileOutputStream;8.importjava.io.IOException;9.importjava.io.InputStream;10.importjava.net.URL;11.importjava.net.URLConnection;12.importjava.util.ArrayList;13.importjava.util.List;14.15.importcom.shadow.service.AudioPlayService.LocalBinder;16.17.importandroid.app.Service;18.importandroid.content.Context;19.importandroid.content.Intent;20.importandroid.media.MediaPlayer;21.importandroid.os.Binder;22.importandroid.os.Handler;23.importandroid.os.IBinder;24.importandroid.util.Log;25.importandroid.widget.Button;26.importandroid.widget.ImageButton;27.importandroid.widget.ProgressBar;28.importandroid.widget.TextView;29.importandroid.widget.Toast;30.31./**32.*MediaPlayerdoesnotyetsupportstreamingfromexternalURLssothisclassprovidesapseudo-streamingfunction33.*bydownloadingthecontentincrementally&playingassoonaswegetenoughaudioinourtemporarystorage.34.*/35.publicclassStreamingMediaPlayerextendsService{36.37.privatestaticfinalintINTIAL_KB_BUFFER=96*10/8;//assume96kbps*10secs/8bitsperbyte38.39.privateTextViewtextStreamed;40.41.privateImageButtonplayButton;42.43.privateProgressBarprogressBar;44.45.//TrackfordisplaybyprogressBar46.privatelongmediaLengthInKb,mediaLengthInSeconds;47.privateinttotalKbRead=0;48.//CreateHandlertocallViewupdatesonthemainUIthread.49.privatefinalHandlerhandler=newHandler();50.privateMediaPlayermediaPlayer;51.privateFiledownloadingMediaFile;52.privatebooleanisInterrupted;53.privateContextcontext;54.privateintcounter=0;55.privatestaticRunnabler;56.privatestaticThreadplayerThread;57.privateLocalBinderlocalBinder=newLocalBinder();58.privateMediaPlayerplayer;59.privatebooleanisPause=false;//播放器是否处于暂停状态60.privatebooleanisSame=false;//所点播歌曲是否是当前播放歌曲61.privateIntegerposition=-1;//设置播放标记62.privateListStringmusic_name;//歌曲列表63.privateListStringmusic_path;64.65.publicStreamingMediaPlayer(Contextcontext,TextViewtextStreamed,ImageButtonplayButton,ButtonstreamButton,ProgressBarprogressBar)66.{67.this.context=context;68.this.textStreamed=textStreamed;69.this.playButton=playButton;70.this.progressBar=progressBar;71.}72.73./**74.*ProgressivlydownloadthemediatoatemporarylocationandupdatetheMediaPlayerasnewcontentbecomesavailable.75.*/76.publicvoidstartStreaming(finalStringmediaUrl,longmediaLengthInKb,longmediaLengthInSeconds)throwsIOException{77.78.this.mediaLengthInKb=mediaLengthInKb;79.this.mediaLengthInSeconds=mediaLengthInSeconds;80.81.r=newRunnable(){82.publicvoidrun(){83.try{84.Log.i(downloadAudioIncrement,downloadAudioIncrement);85.downloadAudioIncrement(mediaUrl);86.}catch(IOExceptione){87.Log.e(getClass().getName(),UnabletoinitializetheMediaPlayerforfileUrl=+mediaUrl,e);88.return;89.}90.}91.};92.playerThread=newThread(r);93.playerThread.start();94.//newThread(r).start();95.}96.97./**98.*DownloadtheurlstreamtoatemporarylocationandthencallthesetDataSource99.*forthatlocalfile100.*/101.publicvoiddownloadAudioIncrement(StringmediaUrl)throwsIOException{102.103.URLConnectioncn=newURL(mediaUrl).openConnection();104.cn.addRequestProperty(User-Agent,NSPlayer/10.0.0.4072WMFSDK/10.0);105.cn.connect();106.InputStreamstream=cn.getInputStream();107.if(stream==null){108.Log.e(getClass().getName(),UnabletocreateInputStreamformediaUrl:+mediaUrl);109.}110.111.downloadingMediaFile=newFile(context.getCacheDir(),downloadingMedia.dat);112.113.//Justincaseapriordeletionfailedbecauseourcodecrashedorsomething,wealsodeleteanypreviously114.//downloadedfiletoensurewestartfresh.Ifyouusethiscode,alwaysdelete115.//nolongeruseddownloadselseyou'llquicklyfillupyourharddiskmemory.Ofcourse,youcanalso116.//storeanypreviouslydownloadedfileinaseparatedatacacheforinstantreplayifyouwantedaswell.117.if(downloadingMediaFile.exists()){118.downloadingMediaFile.delete();119.}120.121.FileOutputStreamout=newFileOutputStream(downloadingMediaFile);122.bytebuf[]=newbyte[16384];123.inttotalBytesRead=0,incrementalBytesRead=0;124.do{125.intnumread=stream.read(buf);126.if(numread=0)127.break;128.out.write(buf,0,numread);129.totalBytesRead+=numread;130.incrementalBytesRead+=numread;131.totalKbRead=totalBytesRead/1000;132.133.testMediaBuffer();134.fireDataLoadUpdate();135.}while(validateNotInterrupted());136.stream.close();137.if(validateNotInterrupted()){138.fireDataFullyLoaded();139.}140.}141.142.privatebooleanvalidateNotInterrupted(){143.if(isInterrupted){144.if(mediaPlayer!=null){145.mediaPlayer.pause();146.//mediaPlayer.release();147.}148.returnfalse;149.}else{150.returntrue;151.}152.}153.154.155./**156.*TestwhetherweneedtotransferbuffereddatatotheMediaPlayer.157.*InteractingwithMediaPlayeronnon-mainUIthreadcancausescrashestosoperformthisusingaHandler.158.*/159.privatevoidtestMediaBuffer(){160.Runnableupdater=newRunnable(){161.publicvoidrun(){162.if(mediaPlayer==null){163.//OnlycreatetheMediaPlayeroncewehavetheminimumbuffereddata164.if(totalKbRead=INTIAL_KB_BUFFER
本文标题:android实现流媒体播放远程mp3文件代码
链接地址:https://www.777doc.com/doc-2897253 .html