美文网首页
Android 音乐播放器

Android 音乐播放器

作者: 霖风_ | 来源:发表于2018-12-19 20:50 被阅读0次

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

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

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
     <Button
         android:id="@+id/play"
         android:text="Play"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/pause"
            android:text="Pause"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/stop"
            android:text="Stop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    主活动设置功能调用

    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 音乐播放器

          本文链接:https://www.haomeiwen.com/subject/bdkskqtx.html