版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、321影音講師:光福微博:weibo./321chi navideo22_當(dāng)萬能播放器播放失敗的時候提示用戶&點(diǎn)擊確定的時候退出播放器-111_當(dāng)播放出錯回調(diào)new On ErrorListe ner() videoview.set On ErrorListe ner(Overridepublic boolea n on Error(MediaPlayer mp,int what, int extra) /Toast.makeText(getApplicati onCon text(),"視頻播放出錯",1).show();/1.提示用戶-根本做法/什么情況導(dǎo)致播放
2、失敗/a,播放過程中沒有網(wǎng)絡(luò);-重試/b,播放一個不支持的格式一進(jìn)來就報錯-用萬能播放器/c,下載視頻的時候中間有空白-無法解決new AlertDialog.Builder(VitamioPlayerActivity.this ).setMessage(.setPositiveButt on("播放器不支持該視頻")"確定,new DialogI nteface .On ClickListe ner() public void onClick(DialogInteface dialog,int whichButton) /退出播放器).setCa ncelabl
3、e(.show();fin ish();false )return true);23_系統(tǒng)播放器和萬能播放器手動切換相關(guān)處理-291_系統(tǒng)播放器和萬能播放器手動切換case R.id. btn switchnew AlertDialog.Builder(VideoPlayerActivity.this ).setMessage( .setNegativeButt on( .setPositiveButt on(int whichButton) )"當(dāng)前是系統(tǒng)播放器,是否要切換萬能播放器播放"下次再說",null )"立刻切換,new DialogI nt
4、eface .On ClickListe ner() public voidon Click(Dialog In terface dialog,/退出播放器startVitamioPlyer();")/*.setCa ncelable(.show();break ;false )*設(shè)置標(biāo)題和播放數(shù)據(jù)*/private void setData() if (videoLists != null&& videoLists .size()>0)Videoitem videoItem=videoLists .get( curre ntPositi on;tv_video
5、 _n ame .setText(videoltem.getTitle();isNetUri = utils.isNetUri(Uri.parse (videoitem.getData();videoview .setVideoPath(videoltem.getData();else if ( uri != null )isNetUri = utils.isNetUri(uri); videoview .setVideoURI(uri );tv_video_name .setText(uri +"");2_支持播放網(wǎng)絡(luò)視頻有緩沖效果*/判斷是否是網(wǎng)絡(luò)資源param uri
6、returnpublicboolea n isNetUri(Uri uri) boolean result = false ; if (uri != null ) if (uri.toString().contains(| uri.toStri ng().c ontains( | uri.toStri ng().c ontains( result =true ; else result =false ;return result;" ")"RTSP")"MMS") 在 handler 中if (isNetUri )/設(shè)置緩存-針對的網(wǎng)
7、絡(luò)視頻/設(shè)置緩中進(jìn)度-到那個等分int buffer = videoview .getBufferPerce ntage();0100;/要緩沖多少int totalBuffer = buffer *video_seekBar .getMax();/得到緩存的進(jìn)度int sec on daryProgress = totalBuffer / 100;video_seekBarelse .setSecondaryProgress(secondaryProgress);video_seekBar .setSecondaryProgress(0);3_屏蔽系統(tǒng)播放器和 Vitamio 播放器切換的動
8、畫res/anim 目錄下<?xml version ="1.0" encoding ="utf-8" ?><translate xmlns:android ="schemas.android./apk/res/android" android:toXDelta ="0" android:fromYDelta ="0" android:duration ="500" android:toYDelta ="0" android:fromXD
9、elta ="0" ></ translate >4_播放器釋放資源在 handler 中case FINISH :if ( videoview != null ) videoview .stopPlayback();finish(); / 關(guān)閉播放器 break ;04_音樂播放器public class AudioListActivityexte nds BaseActivity private ArrayList<Audioltem>audioListsprivateListViewlv_audio_listprivateTextView
10、tv_no audioprivate Utils utils ;privateHan dlerhan dler = new Han dler()publicvoid han dleMessage(a ndroid.os.Message msg) lv_audio_list if ( audioLists tv_no audio else tv_no audio.setAdapter( new VideoListAdapter(); != null &&audioLists.size()>0).setVisibility(View.GONE;.setVisibility(V
11、iew.VISIBLE );Overrideprotected void onCreate(Bundle savedInstanceState) super .onCreate(savedInstanceState);setTitle( "本地音樂);setRightButto n( View.GONEutils = new Utils();lv_audio_list = (ListView) fin dViewByld(R.id.lv_audio_list );tv_n oaudio= (TextView) fin dViewByld(R.id.tv_n oaudio );/得到
12、的視頻getVideData();lv_audioist.setO nl temClickListe ner(new On ItemClickListe ner() Overridepublic void on ltemClick(AdapterView<?> pare nt. View view,int position, long id) /點(diǎn)擊的時候只傳播放地址Audioltem videoltem =audioLists .get(positi on);String path = videoItem.getData();In te nt in te nt =new Inte
13、n t(l ntent.ACTION_VIEW);intent.setDataAndType(Uri.parse (path), "audio/*");startActivity(i nte nt););private class VideoListAdapterexte nds BaseAdapterOverridepublic int getCo un t() retur n audioLists .size();Overridepublic View getView( int positi on.View con vertView,ViewGroup pare nt)
14、II把布局文件->View 對象View view;ViewHolder holder;if (convertView != null )view = con vertView;holder = (ViewHolder) view.getTag(); else view = View. in flate (AudioListActivity.this ,R.l ayout.videolist_itemholder = holder. view.fi ndViewById(R.id. holder. view.fi ndViewByld(R.id. holder. view.fi ndVi
15、ewByld(R.id. holder. view.fi ndViewByld(R.id.,null );new ViewHolder();iv_ic on= (ImageView)iv_ic on );tv_n ame = (TextView) tv_name );tv_durati on= (TextView)tv_durati on );tv_size = (TextView) tv_size );view.setTag(holder); II把對應(yīng)的關(guān)系保存起來 II得到具體的某一條視頻的信息holder. iv_ic on .setlmageResource(R.drawable.m
16、usic_default_bg );AudioItem videoltem =audioLists .get(positi on);holder.tv_name .setText(videoItem.getTitle();/ 把毫秒轉(zhuǎn)換成: 1:20:30 這里形式String tiemStr = videoItem.getDuration();if (tiemStr != null )String timeStr =utils .stringForTime(Integer. parseInt (tiemStr); holder. tv_duration .setText(timeStr);S
17、tring sizeStr =Formatter. formatFileSize (AudioListActivity. this Long. parseLong (videoItem.getSize();holder. tv_size .setText(sizeStr);return view;Overridepublic Object getItem( int position) / TODOAuto-generated method stub return null ;Overridepublic long getItemId( int position) / TODOAuto-gene
18、rated method stub return 0;/ 容器static class ViewHolderImageView iv_icon ;TextView tv_name ;TextView tv_duration ;TextView tv_size ;/* 得到 里面的視頻 媒體掃描器*/private void getVideData() audioLists = new ArrayList<AudioItem>();/在子線程去得到視頻new Thread。public void run() /查找視頻的路徑Uri uri = MediaStore.Audio.Med
19、ia.Strin g project ion = MediaStore.Audio.Media.MediaStore.Audio.Media.MediaStore.Audio.Media.MediaStore.Audio.Media.Cursor cursor = getC onten tResolver().query(uri,EXTERNAL_CONTENT_URIDISPLAY_NAMEDURATION) SIZE, DATA;projecti on.null , null , null );while (cursor.moveToNext()Stri ng duration = cur
20、sor.getStri ng(1);視頻總時長int length = Integer.valueOf (duration);if (length > 1000*10)AudioItem videoItem =new AudioItem();String title = cursor.getString(0);/ 標(biāo)題絕對路徑videoItem.setDurati on( durati on);String size = cursor.getStri ng(2);videoItem.setSize(size);String data = cursor.getStri ng(3);/視頻文
21、件大小/視頻在sdcard 的videoItem.setData(data); audioLists .add(videoItem);videoItem.setTitle(title); cursor.close();handler .sendEmptyMessage(O);.start();Overridepublic View getC onten tView() View view = View.in flate(AudioListActivity.thisR.layout. activity_audiolist , null ); return view;Override public
22、 void clickRightButton() Overridepublic void clickLeftButton() / 退出頁面 finish();02_ 音樂播放器頁面 -動畫列表<?xml version ="1.0" encoding<animation-list xmlns:android android:oneshot ="false"="utf-8" ?>="schemas.android./apk/res/android"><itemandroid:drawab
23、le android:duration<item android:drawable android:duration<item android:drawable android:duration<item android:drawable android:duration<item android:drawable android:duration<item android:drawable android:duration="drawable/now_playing_matrix_01"="150" />="
24、;drawable/now_playing_matrix_02"="150" />="drawable/now_playing_matrix_03"="150" />="drawable/now_playing_matrix_04"="150" />="drawable/now_playing_matrix_05"="150" />="drawable/now_playing_matrix_06"=&q
25、uot;150" /><iteman droid:drawable= "drawable/no w_play in g_matrix_07"an droid:durati on= "150" />an droid:drawable= "drawable/no w_play in g_matrix_08"an droid:durati on= "150" /><iteman droid:drawable= "drawable/no w_play in g_matrix
26、_09"an droid:durati on= "150" /><item</ animation-list >播放動畫rocketImage = (ImageView) fin dViewByld(R.id.rocketlmage.setBackgro un dResource(R.drawable.iv_ic on);ani matio nist);rocketAnimation= (Animation Drawable)rocketA ni mati on.start();rocketimage.getBackgro un d();0
27、3播放頁面的完成-40<?xml version ="1.0"encoding ="utf-8"?>xmln s:a ndroid="schemas.a ndroid./apk/res/a ndroid"= "match_pare nt"="match_pare nt"="drawable/base_bg"vRelativeLayoutan droid:layout_width an droid:layout_height an droid:backgro und
28、vRelativeLayoutan droid:layout_widthan droid:layout_height="match_pare nt"="wrap_c on te nt">vlmageViewicon"an droid:id ="+id/ivan droid:layout_width="wrap_c ontent"an droid:layout_height="wrap_c onten t"an droid:layout_ce nterHorizo ntal="tr
29、ue"/>an droid:backgro und="drawable/a ni matio n_l ist"<TextViewan droid:id ="+id/tv_artist"an droid:layout_width ="wrap_c ontent"an droid:layout_height="wrap_c onten t"an droid:layout_ce nterHorizo ntal="true"an droid:layout_margi nTop =&
30、quot;40dip"an droid:text ="演唱者an droid:textColor ="#ffffff"an droid:textSize ="16sp"/><TextViewan droid:id="+id/tv_ name"an droid:layout_width="wrap_c ontent"an droid:layout_height="wrap_c onten t"an droid:layout_ce nterHorizo ntal=&q
31、uot;true"an droid:layout_marg in Top="60dip"android:text ="歌曲名稱an droid:textColor="#ffffff"an droid:textSize ="18sp"/></ RelativeLayout ><Lin earLayout="match_pare nt"="wrap_c ontent"an droid:layout_widthan droid:layout_heightan
32、 droid:layout_alig nPare ntBottom="true"an droid:orie ntatio n="vertical" >="wrap_c onten t" ="wrap_c on te nt" ="right"<TextViewan droid:id ="+id/tv_time" an droid:layout_width an droid:layout_height an droid:layout_gravityan droid:l
33、ayout_marg in Right="10dip" an droid:text 三"20:11.22"an droid:textColor ="#ffffff"an droid:textSize= "18sp"/><SeekBar="match_pare nt"="wrap_c on te nt"an droid:id ="+id/audio_seekBar" an droid:layout_width an droid:layout_he
34、ight an droid:max ="100"an droid:maxHeight="5dip"android:progress ="10"android:progressDrawable= "drawable/music_progress_horizontal" android:thumb= "drawable/seek_thumb " />="match_parent" ="wrap_content" ="horizontal&quo
35、t; ><LinearLayout android:layout_width android:layout_height android:orientation <Button="10dip"android:layout_marginBottomandroid:id= "+id/btn_shuffle""drawable/btn_shuffle_order_selector"android:background=android:layout_widthandroid:layout_height <Button=
36、"wrap_content"="wrap_content" />android:layout_marginBottom android:id ="+id/btn_pre" android:background= android:layout_width android:layout_height <Button="10dip""drawable/btn_pre_audio_selector"="wrap_content"="wrap_content&qu
37、ot; />android:layout_marginBottom ="10dip" android:id ="+id/btn_play_pause"="drawable/btn_pause_audio_selector"="wrap_content"="wrap_content" />android:backgroundandroid:layout_widthandroid:layout_height <Buttonandroid:layout_marginBottomand
38、roid:id ="+id/btn_next"="10dip"android:backgroundandroid:layout_width android:layout_height <Button="drawable/btn_next_audio_selector"="wrap_content"="wrap_content" />="10dip"android:layout_marginBottomandroid:id ="+id/btn_lyric&
39、quot;="drawable/btn_lyric_selector" ="wrap_content" ="wrap_content" />android:backgroundandroid:layout_width android:layout_height </ LinearLayout ></ LinearLayout</ RelativeLayout04_ 創(chuàng)立效勞 MusicPlayerService& 和創(chuàng)立對應(yīng)的 aidl 文件IMusicPlayerService.aidl-501
40、_回憶效勞調(diào)用的過程,基于此類改成2_ 創(chuàng) 建 服 務(wù) MusicPlayerService.javaIMusicPlayerService.aidl, 名字一定要改extends Service public class MusicPlayerServiceOverride public void onCreate() super .onCreate();/* 根據(jù)位置翻開一個音頻并且播放* param position*/public void openAudio( int position) * 開始播放音頻*/public void start() /*/public void pau
41、se() * 停止*/public void stop() * 得到歌曲的名稱*/public String getAudioName() return "" ;* 得到歌曲演唱者的名字*/public String getArtistName() return "" ;* 得到歌曲的當(dāng)前播放進(jìn)度*/public int getCurrentPosition() return 0;* 得到歌曲的當(dāng)前總進(jìn)度*/public int getDuration() return 0;/* * 播放下一首歌曲*/public void next() /* 播放上一
42、首歌曲*/public void pre() * 得到播放模式*/public int getPlayMode() return 0;* 設(shè)置播放模式*/public void setPlayMode( int mode) Overridepublic IBinder onBind(Intent intent) return stub ;IMusicPlayerService.Stubstub= new IMusicPlayerService.Stub() MusicPlayerServiceservice= MusicPlayerService.this ;Overridepublic vo
43、idstop()throwsRemoteException service.stop();Overridepublic voidstart()throwsRemoteException service.start();Overridepublic void setPlayMode( int mode) throws RemoteException service .setPlayMode(mode);Overridepublic void pre() throws RemoteException service .pre();Overridepublic void pause() throws
44、 RemoteException service .pause();Overridepublic void openAudio( int position) throws RemoteException service .openAudio(position);Overridepublic void next() throws RemoteException service .next();Overridepublic int getPlayMode() throws RemoteException / TODOAuto-generated method stub return service
45、 .getPlayMode();Overridepublic int getDuration() throws RemoteException / TODOAuto-generated method stub return service .getDuration();Overridepublic int getCurre ntPositi on()throws RemoteExcepti on / TODOAuto-ge nerated method stub retur n service .getCurre ntPositi on();Overridepublic Stri ng get
46、AudioName()throws RemoteExcepti on / TODOAuto-ge nerated method stub return service .getAudioName();Overridepublic Stri ng getArtistName()throws RemoteExcepti on / TODOAuto-ge nerated method stub return service .getArtistName();3_把方法體去掉package .atguigu.mobileplayer.audio;in terface IMusicPlayerServi
47、ce *根據(jù)位置翻開一個音頻并且播放* param positi on*/ void ope nAudio(i nt position);r暫停 */ void pause();*開始播放音頻*/void start();*/void stop();l播放下一首歌曲*/void n ext();r*播放上一首歌曲*/void pre();r*得到播放模式*/int getPlavModeO; *設(shè)置播放模式*/void setPlayMode(i nt mode*得到歌曲的名稱*/Stri ng getAudioName();*得到歌曲演唱者的名字*/String getArtistName(
48、);/*得到歌曲的當(dāng)前播放進(jìn)度*/int getCurre ntPositi on();*得到歌曲的當(dāng)前總進(jìn)度*/int getDurati on();05_實(shí)現(xiàn)播放一個音樂文件并且設(shè)置暫停和播放按鈕狀態(tài)-161_效勞中加載音頻Overridepublic void on Create。 super .onCreate。; getAudioData();*得到 里面的視頻媒體掃描器*/private void getAudioData() audioLists = new ArrayList<Audioltem>(); /在子線程去得到視頻new Thread() public v
49、oid run() /查找視頻的路徑Uri uri = MediaStore.Audio.Media. Strin g project ion = MediaStore.Audio.Media.DISPLAY_NA MEMediaStore.Audio.Media.MediaStore.Audio.Media.MediaStore.Audio.Media.EXTERNAL_CONTENT_URIDURATION)SIZE,DATA;projecti on,/視頻總時長Cursor cursor = getC onten tResolver().query(uri, null , null ,
50、null );while (cursor.moveToNext() String duration = cursor.getString(1);int length = Integer.valueOf (duration);if (length > 1000 * 10) AudioItem videoItem =new AudioItem();Stri ng title = cursor.getStri ng(0);/ 標(biāo)題videoItem.setTitle(title);videoItem.setDurati on( durati on);/視頻文件大小String size = c
51、ursor.getString(1);videoItem.setSize(size);的絕對路徑videoItem.setData(data); audioLists .add(videoItem);cursor.close();;.start();/* *根據(jù)位置翻開一個音頻并且播放* param position*/publicvoid openAudio(int position) tryif (mediaPlayer!= null ) mediaPlayer.reset();mediaPlayer=null ;mediaPlayer = new MediaPlayer();if ( a
52、udioLists != null ) curre ntAudioltem = audioLists.get(positi on);mediaPlayer .setDataSource(curre ntAudioltem.getData();mediaPlayer.set On PreparedListe ner(mOn PreparedListe ner);mediaPlayer.set On Completio nListe ner(mOn Completi on Liste ner);mediaPlayer.set On ErrorListe ner(mOn ErrorListe ner
53、);mediaPlayer.prepareAs yn c(); catch (Exception e) / TODOAuto-ge nerated catch blocke.pri ntStackTrace();private OnPreparedListener mOnPreparedListener = new OnPreparedListener() Overridepublic void onPrepared(MediaPlayer mp) start();private OnCompletionListener mOnCompletionListener = new OnComple
54、tionListener() Overridepublic void onCompletion(MediaPlayer mp) next();private OnErrorListener mOnErrorListener = new OnErrorListener() Overridepublic boolean onError(MediaPlayer mp, int what, int extra) / TODOAuto-generated method stub return false ;開始播放音頻*/public void start() mediaPlayer .start();
55、2_當(dāng)啟動播放器頁面時候綁定效勞/*啟動效勞 */ private void startService() Intent intent =new Inten t();inten t.setAct ion(".atguigu.mobileplayer.musicplayerservice");bin dService(i ntent, conn. Con text.BIND_AUTO_CREATE;startService(i nten t);功能清單文件要配置<servicean droid: name =".atguigu.mobileplayer.aud
56、io.MusicPlayerService"<i nte nt-filter><act ion/>an droid:n ame= ".atguigu.mobileplayer.musicplayerservice"</ intent-filter></ service >private ServiceC onnectionconn = new ServiceC onnection() Overridepublic void on ServiceDisc onn ected(Comp onen tName n ame)
57、 try if (service != null )service .stop();service = n ull ; catch (RemoteException e) / TODOAuto-ge nerated catch blocke.pri ntStackTrace();Overridepublic void on ServiceC onn ected(Comp onen tNamename, IBinderiBi nder)service = IMusicPlayerService.Stub.asln terface(iB in der);try if (service !=null )service.ope nAudio(curre ntPositi on); catch (RemoteException e) / TODOAuto-generated catch block e.printStackTrace(); ;3_根據(jù)得到的播放位置,播放具體位置的音頻* 得到播放列表傳入的數(shù)據(jù)*/ private void getData() currentPosition = getIntent().getIntExtra("currentPosition&qu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度常州智能化消防系統(tǒng)設(shè)計與施工合同2篇
- 二零二五年度離婚協(xié)議中商業(yè)秘密保護(hù)協(xié)議2篇
- 二零二五年度環(huán)保工程合作合同范本
- 2025年協(xié)作出版合同范文
- 2025招聘合同范本范文
- 建設(shè)工程勘察設(shè)計合同范本
- 股權(quán)收購合同書范本
- 達(dá)人簽約協(xié)議
- 二零二五年建筑行業(yè)員工勞動關(guān)系轉(zhuǎn)移三方合同3篇
- 天津中心有限合伙企業(yè)合伙協(xié)議
- 統(tǒng)編版一年級語文上冊 第5單元教材解讀 PPT
- CSCEC8XN-SP-安全總監(jiān)項(xiàng)目實(shí)操手冊
- 加減乘除混合運(yùn)算600題直接打印
- 口腔衛(wèi)生保健知識講座班會全文PPT
- 成都市產(chǎn)業(yè)園區(qū)物業(yè)服務(wù)等級劃分二級標(biāo)準(zhǔn)整理版
- 最新監(jiān)督學(xué)模擬試卷及答案解析
- ASCO7000系列GROUP5控制盤使用手冊
- 污水處理廠關(guān)鍵部位施工監(jiān)理控制要點(diǎn)
- 財政投資評審中心工作流程
- 帶狀皰疹的護(hù)理查房
- 10個地基基礎(chǔ)工程質(zhì)量通病及防治措施
評論
0/150
提交評論