在Android中播放音频文件一般使用MediaPlayer类来实现,他对多种格式的音频文件提供了非常全面的控制方法,从而使得播放器变得十分简单。
File file = new File(Environment.getExternalStorageDirectory(), "456.mp3"); mediaPlayer.setDataSource(file.getPath()); // 指定音频文件的路径 mediaPlayer.prepare(); // 让MediaPlayer进入到准备状态 mediaPlayer.start(); // 开始播放 mediaPlayer.pause(); // 暂停播放 mediaPlayer.reset(); // 停止播放
完整代码:
//音频播放 public class MainActivity extends AppCompatActivity { private MediaPlayer mediaPlayer = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button play = (Button) findViewById(R.id.play); Button pause = (Button) findViewById(R.id.pause); Button stop = (Button) findViewById(R.id.stop); 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 } play.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!mediaPlayer.isPlaying()) { mediaPlayer.start(); // 开始播放 } } }); pause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); // 暂停播放 } } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mediaPlayer.isPlaying()) { mediaPlayer.reset(); // 停止播放 initMediaPlayer(); } } }); } private void initMediaPlayer() { try { File file = new File(Environment.getExternalStorageDirectory(), "456.mp3"); mediaPlayer.setDataSource(file.getPath()); // 指定音频文件的路径 mediaPlayer.prepare(); // 让MediaPlayer进入到准备状态 } catch (Exception e) { e.printStackTrace(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { initMediaPlayer(); } else { Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show(); finish(); } break; default: } } @Override protected void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } }
xml:
<?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:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/play" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="播放" /> <Button android:id="@+id/pause" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="暂停" /> <Button android:id="@+id/stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止" /> </LinearLayout>
转发表明出处:https://blog.csdn.net/qq_35698774/article/details/107053094
点击下载源码
android互助群:
感谢:郭霖的《第一行代码 第二版》