国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android MediaPlayer實現音樂播放器實例代碼

2019-12-12 04:04:23
字體:
來源:轉載
供稿:網友

Android MediaPlayer實現音樂播放器

1、布局文件

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:orientation="vertical" >    <TextView     android:id="@+id/hint"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:padding="10px"     android:text="單擊“開始”按鈕播放音頻" />    <LinearLayout     android:id="@+id/linearLayout1"     android:layout_width="match_parent"     android:layout_height="wrap_content" >   <Button     android:id="@+id/button1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="播放" />    <Button     android:id="@+id/button2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:enabled="false"     android:text="暫停" />    <Button     android:id="@+id/button3"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:enabled="false"     android:text="停止" />   </LinearLayout> </LinearLayout> 

2、MainActivity的成員變量

private MediaPlayer player;//MediaPlayer對象   private boolean isPause = false;//是否暫停   private File file;//要播放的音頻文件   private TextView hint;//聲明顯示提示信息的文本框 

3、onCreate()方法中獲取組件

final Button button1 = (Button)findViewById(R.id.button1);//獲取“播放”按鈕     final Button button2 = (Button)findViewById(R.id.button2);//獲取“暫停/繼續”按鈕     final Button button3 = (Button)findViewById(R.id.button3);//獲取“停止”按鈕     hint = (TextView)findViewById(R.id.hint);//獲取用于顯示提示信息的文本框     file = new File("/storage/emulated/0/qqmusic/song/喬維怡 - 白月光[mqms2].mp3");//獲取要播放的文件     if(file.exists()){       player = MediaPlayer.create(this, Uri.parse(file.getAbsolutePath()));//創建MediaPlayer獨享     }else{       hint.setText("要播放的音頻文件不存在!");       button1.setEnabled(false);       return;     } 

4、編寫play()方法

private void play(){     try {       player.reset();       player.setDataSource(file.getAbsolutePath());//重新設置要播放的音頻       player.prepare();//預加載音頻       player.start();//開始播放       hint.setText("正在播放音頻.....");     } catch (Exception e) {       e.printStackTrace();     }   } 

5、為MediaPlayer對象添加監聽事件,播完重新播放

player.setOnCompletionListener(new OnCompletionListener() {       @Override       public void onCompletion(MediaPlayer mp) {         play();//重新開始播放       }     }); 

6、為播放添加單擊事件監聽器

button1.setOnClickListener(new OnClickListener() {       @Override       public void onClick(View v) {         play();//開始播放音樂         if(isPause){           button2.setText("暫停");           isPause = false;//設置暫停標記變量的值為false         }         button2.setEnabled(true);//“暫停/繼續”按鈕可用         button3.setEnabled(true);//"停止"按鈕可用         button1.setEnabled(false);//“播放”按鈕不可用       }     }); 

7、在“暫停/繼續”按鈕添加單擊事件監聽器

button2.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v) {       if(player.isPlaying()&&!isPause){         player.pause();//暫停播放         isPause = true;         ((Button)v).setText("繼續");         hint.setText("暫停播放音頻...");         button1.setEnabled(true);//“播放”按鈕可用       }else{         player.start();//繼續播放         ((Button)v).setText("暫停");         hint.setText("正在播放音頻...");         isPause = false;         button1.setEnabled(false);//“播放”按鈕不可用       }     }   }); 

8、停止按鈕

button3.setOnClickListener(new OnClickListener() {       @Override       public void onClick(View v) {         player.stop();//停止播放         hint.setText("停止播放音頻...");         button2.setEnabled(false);//“暫停/繼續”按鈕不可用         button3.setEnabled(false);//“停止”按鈕不可用         button1.setEnabled(true);//“播放”按鈕可用       }     }); 

9、重寫Activity的onDestroy()方法

@Override   protected void onDestroy() {     if(player.isPlaying()){       player.stop();//停止音頻的播放     }     player.release();//釋放資源     super.onDestroy();   } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰台区| 西畴县| 新竹市| 惠来县| 景洪市| 阜阳市| 浙江省| 商丘市| 图木舒克市| 务川| 周宁县| 普定县| 贵德县| 鹿泉市| 红桥区| 和硕县| 隆德县| 元氏县| 浦县| 江津市| 高要市| 拜城县| 五指山市| 桦南县| 大庆市| 鄄城县| 连山| 民丰县| 长武县| 江华| 屏东市| 吉林省| 如东县| 漠河县| 永昌县| 军事| 济宁市| 邯郸县| 木兰县| 隆子县| 阳西县|