在上一篇博客,Android-MediaPlayer-音频播放-普通准备,介绍了普通准备的播放

一般在开发中,要使用异步准备比较好,因为准备是要去准备硬件来播放,是耗性能的

异步准备和普通准备的区别

  普通准备:一直是主线程,会发生阻塞

  异步准备:主线程 + 一个子线程,不会发生阻塞


MediaPlayer是Android设计的媒体播放器,不仅仅可以播放音频文件,还可以播放视频文件

播放:Audio(音频,.mp3)相关

播放:Video(视频,.mp4)相关

以下图,是Android官方提供:MediaPlayer时序图:

只要会看这个图:就能实现音频/视频播放,暂停,继续,停止,重播,等等

看图规律:

    1.蓝色椭圆形是状态,例如:Initialized已初始化状态,Prepared准备状态,Started启动状态,Stopped停止状态,End结束状态,等等;

    2.单箭头是方法调用:例如:调用reset方法重置,调用prepare方法准备,调用start方法播放,等等;

    3.双箭头是监听回调:例如:onError回调错误,等等


此MediaPlayer播放使用异步准备

package liudeli.my_media1;

import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; /**
* 此MediaPlayer播放使用使用异步准备,不是普通准备
*/
public class MediaPlayAsyncAudioActivity extends AppCompatActivity { private TextView tvPlayerPath; // 显示播放的路径
private TextView tvAudioInfo; // 歌曲时长/歌手/专辑
private TextView tvAudioThisDuration; // 当前播放的时长
private TextView tv_play_state; // 播放的状态 /**
* 媒体播放器,可以播放(音频/视频)
* 播放(音频/视频)操作一模一样
*/
private MediaPlayer mediaPlayer; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_audio); tvPlayerPath = findViewById(R.id.tv_player_path);
tvAudioInfo = findViewById(R.id.tv_audio_info);
tvAudioThisDuration = findViewById(R.id.tv_audio_this_duration);
tv_play_state = findViewById(R.id.tv_play_state); mediaPlayer = new MediaPlayer(); // 为了测试,这样写,真实开发中,不这样写
new Thread(){
@Override
public void run() {
super.run();
while (true) { runOnUiThread(new Runnable() {
@Override
public void run() {
if (mediaPlayer.isPlaying()) {
tvAudioThisDuration.setText("当前时长:" + postions(mediaPlayer.getCurrentPosition()));
}
}
}); SystemClock.sleep(1000);
}
}
}.start(); /**
* 监听播放完成
*/
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("mp", "播放完成");
Toast.makeText(MediaPlayAsyncAudioActivity.this, "播放完成", Toast.LENGTH_SHORT).show();
tvAudioThisDuration.setText("当前时长:-");
}
}); /**
* 监听播放错误
*/
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
tv_play_state.setText("播放异常");
return false;
}
}); /**
* 去获取第一条外置存储的音频文件.mp3 的路径
* 通过Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 获取外置存储音频文件
* getContentResolver.query(uri)
*/
initAudioPlayerPath(); tv_play_state.setText("---");
} /**
* 去获取第六条外置存储的音频文件.mp3 的路径
*/
private void initAudioPlayerPath() {
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// 查询的列
String[] projection = new String[]{MediaStore.Audio.Media.DATA, // 音频路径
MediaStore.Audio.Media.DURATION, // 音频时长
MediaStore.Audio.Media.ARTIST, // 歌手
MediaStore.Audio.Media.ALBUM // 专辑
};
// 让Android系统也会去读取外置存储
Cursor cursor = getContentResolver().query(uri,
projection,
null,
null,
null,
null); /**
* 把游标移到第一行:cursor.moveToFirst()
* 把游标移到第六行:cursor.moveToPosition(6)
*/
if (cursor.moveToPosition(6)) {
// if (cursor.moveToFirst()) {
tvPlayerPath.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
tvAudioInfo.setText("歌曲时长:" + postions(Integer.parseInt(duration)) + " \n歌手:" + artist + " \n专辑:" + album);
}
} /**
* 转换时长值
*/
private String postions(int postion) {
int musicTime = postion / 1000;
return musicTime / 60 + ":" + musicTime % 60;
} /**
* 开始播放:此次播放使用异步准备, 不是普通准备
* @param view
*/
public void player(View view) {
try {
// 重置
mediaPlayer.reset();
// 设置音频文件路径
mediaPlayer.setDataSource(tvPlayerPath.getText().toString().trim());
// 异步准备并播放
asyncPrepare();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 异步准备的行为
*/
private void asyncPrepare() {
// 准备:是操作硬件在播放,所以需要准备
mediaPlayer.prepareAsync();
// 监听异步准备,一旦准备完成,就会调用此方法
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// 调用此方法,代表异步准备完成✅
// 开始播放
mediaPlayer.start(); tv_play_state.setText("播放中...");
}
});
} /**
* 暂停播放 继续播放
* @param view
*/
public void pause(View view) {
/**
* 这种方式可以拿到控件
*/
Button pause_continue = (Button) view;
if (mediaPlayer.isPlaying()) {
pause_continue.setText("继续");
// 暂停
mediaPlayer.pause(); tv_play_state.setText("暂停中...");
} else {
pause_continue.setText("暂停");
// 继续播放
mediaPlayer.start(); tv_play_state.setText("播放中...");
}
} /**
* 停止播放
*/
public void stop(View view) {
// 停止播放
mediaPlayer.stop(); tv_play_state.setText("---");
} /**
* 重播
* @param view
*/
public void recorded(View view) {
try {
// 先停止
mediaPlayer.stop(); // 异步准备并播放
asyncPrepare();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mediaPlayer.isPlaying()) {
tv_play_state.setText("播放中...");
}
}
} /**
* 此Activity销毁后,一定要
* mediaPlayer.release();
* mediaPlayer = null;
* 因为 MediaPlayer 是操作硬件在播放,所以一定要释放资源
*/
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
mediaPlayer = null;
System.gc();
}
}

AndroidManifest.xml 配置 外部存储读取权限:

Android系统也会去读取外置存储,需要读取外部存储的权限

  <!--
getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, ...)
Android系统也会去读取外置存储,需要读取外部存储的权限
-->
<uses-permission android:name="android.permission.READ_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:layout_height="match_parent"
tools:context=".MediaPlayAudioActivity"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放音频.mp3路径:"
/> <TextView
android:id="@+id/tv_player_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/mnt/sdcard/"
/> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放"
android:onClick="player"
/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停"
android:onClick="pause"
/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止"
android:onClick="stop"
/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重播"
android:onClick="recorded"
/> </LinearLayout> <TextView
android:id="@+id/tv_audio_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="test"
/> <TextView
android:id="@+id/tv_audio_this_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="歌曲时长:-"
/> <TextView
android:id="@+id/tv_play_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="30dp"
android:layout_gravity="center_horizontal"
/> </LinearLayout>

异步准备性能比普通准备要好:

最新文章

  1. iOS开发UI篇—CAlayer(创建图层)
  2. Fastboot模式和Recovery模式
  3. .net程序单元测试介绍
  4. css div垂直居中
  5. hadoop datanode启动失败
  6. 安卓设备通过USB接口读取UVC摄像头权限问题
  7. Win10无法上网提示缺少一个或者多个网络协议的处理方法
  8. 代码研磨 Slim v3 (一)--app-&gt;get()&amp;route-&gt;add()
  9. FastJson解析对象及对象数组--项目经验
  10. 在Java中兼容Windows和Linux的路径处理
  11. netbeans字体与颜色配置模板相关网站
  12. mysql 备份和恢复的两条命令
  13. js和jquery设置css样式的几种方法
  14. appium工作原理
  15. NotePad++配置使之支持jquery、html、css、javascript、php提示
  16. jquery 简单的别踩白块小游戏
  17. 010-ThreadGroup线程组
  18. Problem A: Dreamweaver 解题报告
  19. 解决Web部署 svg/woff/woff2字体 404错误 iis 解决Web部署 svg/woff/woff2字体 404错误
  20. STM32使用FatFs

热门文章

  1. generatorConfig.xml
  2. 学习笔记之C# 教程 | 菜鸟教程
  3. [html]CSS 小贴士
  4. user_add示例
  5. Python之分布式监控系统开发
  6. Python Geoip 获取IP地址经度、纬度
  7. Redis 与 Lua Script
  8. BashProfile
  9. 2015年传智播客JavaEE 第168期就业班视频教程day38-SSH综合案例-1
  10. 2015年传智播客JavaEE 第168期就业班视频教程17-登录功能业务逻辑实现(代码)