Android MediaPlayer實現音樂播放器
1、布局文件
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
android:text="單擊“開始”按鈕播放音頻" />
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="暫停" />
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="停止" />
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();
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!