用MediaPlayer累制作简单音乐播放器(播放一首指定的音乐)
涉及方法:
start()开始或继续播放音乐;
pause()暂停播放音乐;
reset()将MediaPlayer对象重置到刚刚创建的状态;
stop()停止播放音频(点PLAY不能继续播放);
release()释放掉与MediaPlayer相关的资源;
isPlaying()判断当前MediaPlayer是否正在播放音乐;
具体代码:
activity_music.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="@drawable/music">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="消愁(Live)"
android:textSize="40sp"
android:textColor="#FFFFFFFF"
android:layout_gravity="center"
android:layout_marginTop="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="薛之谦/毛不易"
android:textSize="20sp"
android:textColor="#FFFFFFFF"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="play"
android:textSize="25sp" />
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="pause"
android:textSize="25sp" />
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="stop"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
在res表中新建raw文件夹,并将想要播放的音乐复制进去,以便在MusicActivity.java中调用
MusicActivity.java
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MusicActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private Button mBtn1,mBtn2,mBtn3;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
initMediaPlayer();//调用initMediaPlayer()方法为MediaPlayer初始化操作
mBtn1=findViewById(R.id.play);//实例化播放按钮
mBtn2=findViewById(R.id.pause);//实例化暂停按钮
mBtn3=findViewById(R.id.replay);//实例化停止按钮
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()){//点击PLAY按钮时进行判断如果MediaPlayer没有播放音乐,则调用start()方法开始播放
mediaPlayer.start();
}
}
});
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()){
//点击PAUSE按钮时进行判断如果MediaPlayer正在播放音乐,则调用pause()方法暂停播放
mediaPlayer.pause();
}
}
});
mBtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()){//点击STOP按钮时进行判断如果MediaPlayer正在播放音乐,则调用reset()方法重置,因为再次点击PLAY时不能不能继续播放,所以重新调用initMediaPlayer()方法,点击PLAY就可以重新播放了
mediaPlayer.reset();
initMediaPlayer();
}
}
});
}
private void initMediaPlayer() {
mediaPlayer=MediaPlayer.create(this,R.raw.m1);//调用提前准备好的音乐
}
@Override
protected void onDestroy() {//释放与MediaPlayer有关的资源
super.onDestroy();
if (mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
结果展示:
点击PLAY播放音乐,点击PAUSE暂停,点击STOP再点击PLAY重新播放