(1)在res文件夹中新建一个文件夹重命名为raw,并且将要播放的音乐放到raw文件夹里面
(2)修改layout目录下的xml布局文件,添加3个按钮空间和一个文本控件,用于提示当前播放状态和 播放暂停 停止等功能。具体代码如下
- <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>
- private MediaPlayer mp;//mediaPlayer对象
- private Button play,pause,stop;//播放 暂停/继续 停止 按钮
- private TextView hint;//显示当前播放状态
- private boolean isPause=false;//是否暂停
- 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);//创建mediaplayer对象
- 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();//输出异常信息
- }
- }
- mp.setOnCompletionListener(new OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer arg0) {
- // TODO Auto-generated method stub
- play();//重新开始播放
- }
- });
- play.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- play();
- if(isPause){
- pause.setText("暂停");
- isPause=false;
- }
- }
- });
(8)为暂停按钮添加单击事件监听器
- 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("继续");
- hint.setText("暂停播放音频...");
- play.setEnabled(true);
- }else{
- mp.start();
- pause.setText("暂停");
- hint.setText("继续播放音频...");
- isPause=false;
- play.setEnabled(false);
- }
- }
- });
- 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();
- }
//权限和依赖自己加
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:converter-gson:2.4.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' implementation 'io.reactivex.rxjava2:rxjava:2.1.13' implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'com.google.dagger:dagger:2.8' annotationProcessor 'com.google.dagger:dagger-compiler:2.7' implementation 'com.facebook.fresco:fresco:0.12.0' // 支持 GIF 动图,需要添加 implementation 'com.facebook.fresco:animated-gif:0.12.0' implementation 'com.android.support:support-v4:27.1.1' implementation 'com.jcodecraeer:xrecyclerview:1.3.2' implementation 'com.android.support:design:27.1.1' implementation 'com.github.bumptech.glide:glide:4.7.1' implementation 'com.amap.api:map2d:5.2.0' implementation 'com.amap.api:location:4.0.1' implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'