Android 使用mediaplayer播放res/raw文件夾中的音樂的實例
Android 使用mediaplayer播放res/raw文件夾中的音樂的實例
(1)在res文件夾中新建一個文件夾重命名為raw,并且將要播放的音樂放到raw文件夾里面
(2)修改layout目錄下的xml布局文件,添加3個按鈕空間和一個文本控件,用于提示當前播放狀態(tài)和 播放暫停 停止等功能。具體代碼如下
<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:text="單擊播放開始播放音樂" />
<LinearLayout
android:orientation="horizontal"
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:text="暫停" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
</LinearLayout>
</LinearLayout>
(3)打開MainActivity 在該類中,定義所需的成員變量,具體代碼如下
private MediaPlayer mp;//mediaPlayer對象 private Button play,pause,stop;//播放 暫停/繼續(xù) 停止 按鈕 private TextView hint;//顯示當前播放狀態(tài) private boolean isPause=false;//是否暫停
(4)在onCreate()方法中,獲取播放 暫停/繼續(xù) 停止 按鈕 提示當前狀態(tài)的文本框,并為mediaplayer對象創(chuàng)建播放的對象,具體代碼如下。
play=(Button) findViewById(R.id.button1); pause=(Button) findViewById(R.id.button2); stop=(Button) findViewById(R.id.button3); hint=(TextView) findViewById(R.id.hint); hint.setTextSize(20); mp=MediaPlayer.create(MainActivity.this, R.raw.sound);//創(chuàng)建mediaplayer對象
(5)編寫用于播放音樂的無返回值的play()方法。在該方法中首先調用mediaplayer對象的reset()方法重置mediaplayer對象,然后重新為其設置要播放的音頻文件。最后調用start()方法開始播放音頻
private void play(){
try{
mp.reset();
mp=MediaPlayer.create(MainActivity.this, R.raw.sound);//重新設置要播放的音頻
mp.start();//開始播放
hint.setText("正在播放音頻...");
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}catch(Exception e){
e.printStackTrace();//輸出異常信息
}
}
(6)為mediaplayer對象添加完成時間監(jiān)聽器,用于當音樂播放完畢后重新開始播放音樂
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
play();//重新開始播放
}
});
(7)為播放按鈕添加單擊事件監(jiān)聽器
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
play();
if(isPause){
pause.setText("暫停");
isPause=false;
}
}
});
(8)為暫停按鈕添加單擊事件監(jiān)聽器
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mp.isPlaying()&&!isPause){
mp.pause();
isPause=true;
pause.setText("繼續(xù)");
hint.setText("暫停播放音頻...");
play.setEnabled(true);
}else{
mp.start();
pause.setText("暫停");
hint.setText("繼續(xù)播放音頻...");
isPause=false;
play.setEnabled(false);
}
}
});
(9)為停止按鈕添加單擊事件監(jiān)聽器
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
hint.setText("停止播放音頻...");
pause.setEnabled(false);
stop.setEnabled(false);
play.setEnabled(true);
}
});
(10)一定要記得這個。重寫Activity的onDestroy()方法,用于在當前Activity銷毀時,停止正在播放的音頻,并釋放mediaplayer所占用的資源,否則你每打開一次就會播放一次,并且上次播放的不會停止 你可以試試的,我解釋不清楚
protected void onDestroy() {
// TODO Auto-generated method stub
if(mp.isPlaying()){
mp.stop();
}
mp.release();//釋放資源
super.onDestroy();
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Android環(huán)形進度條(安卓默認形式)實例代碼
這篇文章主要介紹了Android環(huán)形進度條(安卓默認形式)實例代碼的相關資料,需要的朋友可以參考下2016-03-03
Flutter?阻止系統(tǒng)鍵盤彈出的優(yōu)雅方式
這篇文章主要為大家介紹了Flutter?阻止系統(tǒng)鍵盤彈出的優(yōu)雅方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android用PopupWindow實現自定義overflow
這篇文章主要介紹了Android用PopupWindow實現自定義overflow的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android?Camera+SurfaceView自動聚焦防止變形拉伸
這篇文章主要為大家介紹了Android自定義相機Camera+SurfaceView實現自動聚焦防止變形拉伸詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Android中BroadcastReceiver實現短信關鍵字自動回復功能
實現手機短信監(jiān)聽的方式有兩種:一是通過ContentObserver觀察者實現監(jiān)聽,另一種就是通過廣播即BroadcastReceiver實現短信監(jiān)聽,文章中通過使用BroadcastReceiver實現有新短信的及時監(jiān)聽及包含設定的關鍵字時自動回復2018-06-06

