Android 音乐播放器

通过使用MediaPlayer类来简单实现,下面是详细步骤和代码

  1. 创建一个MediaPlayer对象
  2. 调用setDataSource(),设置音频文件路径
  3. 调用prepare() 使对象进入准备状态
  4. 调用start()方法播放音乐
  5. 调用pause()方法暂停

AndroidManifest.xml添加权限读取用户存储


用户界面文件添加播放、暂停、停止按钮




 

主活动设置功能调用

package com.example.peipeng.playaudiotest;

import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.Manifest;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MediaPlayer mediaPlayer = new MediaPlayer();//实例化对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button play = findViewById(R.id.play);
        Button pause = findViewById(R.id.pause);
        Button stop = findViewById(R.id.stop);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
        if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            },1);

            }
        else{
            initMediaPlayer();//初始化MediaPlayer
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,String[] permission,int[] grantResults){
        switch (requestCode){
            case 1:
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    initMediaPlayer();
                }else {
                    Toast.makeText(this,"reject permission cannot use app",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
                default:
        }

    }
    private void initMediaPlayer(){
        try{
            File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");
            if(file !=null)//想要添加判断 是否找到music.map3
                Toast.makeText(MainActivity.this,"the sd have not music.mp3",Toast.LENGTH_SHORT).show();
            mediaPlayer.setDataSource(file.getPath());//指定音频路径
            mediaPlayer.prepare();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.play:
                if(!mediaPlayer.isPlaying()){
                mediaPlayer.start();//
                }
                break;
            case R.id.pause:
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }
                break;
            case R.id.stop:
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.reset();
                    initMediaPlayer();
                }
                break;
                default:
                    break;
        }
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        if(mediaPlayer != null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}
Android 音乐播放器_第1张图片
运行结果

你可能感兴趣的:(Android 音乐播放器)