目录:
Mediaplayer
常用的API
生命周期
播放文件来源
最基本实现放歌(file文件形式)
网络流放歌
内容提供器(content provider)播放
使用锁
播放错误,监听处理
注意事项
在服务里实现后台放歌
Mediaplayer
在Android中播放音频文件一般是使用MediaPlayer类实现的,它具有很全面的控制方法,支持多种格式。
常用的API
方法名 | 功能描述 |
---|---|
setDataSource() | 设置要播放的音频文件的位置 |
prepare() | 在开始播放之前调用这个方法完成准备工作 |
start() | 开始播放或者继续播放音频 |
pause() | 暂停播放音频 |
reset() | 将MediaPlayer对象重置到开始创建的状态,prepare()之前 |
seekTo() | 从指定的位置开始播放音频 |
stop() | 停止播放音频。从生命周期(在下面)看出调用这个方法后,MediaPlayer无法再start()播放音频 |
release() | 释放掉与MediaPlayer有关的资源,防止占用过多内存 |
isPlaying() | 当前是否在播放音频 |
getDuration() | 载入音频文件的长度 |
setAudioStreamType | 设置资源为音频流 |
生命周期
播放文件来源
- file
- 网络流
- 内容提供器
最基本实现放歌(file文件形式)
基本步骤:
- 获取音频文件路径
- 用setDataSource()方法设置音频文件的路径
- 调用prepare()方法使MediaPlayer进入到准备状态
- 调用start()方法开始播放
- 调用pause()可以停止播放
- 调用reset()方法重置播放
- 调用stop()方法停止播放音频,从生命周期看出无法直接再start()
- 释放内存
实践
首先下载一个mp3文件mu.mp3放到sd卡根目录(建议用真机)
这里要查看内存卡资源,先声明权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...其他的不用写了,允许一个权限,该权限所在的权限组就全部同意了
另外还有Android 6.0以上的运行时权限在代码里写(程序运行时申请权限)
接着布局文件,书写三个控制按钮,play,pause,reset
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="play"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="pause"/>
<Button
android:id="@+id/reset"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="reset"/>
</LinearLayout>
接着Activity中,
private MediaPlayer mMediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化button
iniView();
//判断权限够不够,不够就给
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 {
//够了就设置路径等,准备播放
iniMediaPlayerFile();
}
}
//获取到权限回调方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
iniMediaPlayerFile();
} else {
Toast.makeText(this, "权限不够获取不到音乐,程序将退出", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
break;
}
}
/*
* 添加file文件到MediaPlayer对象并且准备播放音频
**/
private void iniMediaPlayerFile() {
//获取文件路径
File file = new File(Environment.getExternalStorageDirectory(), "mu.mp3");
try {
//此处的两个方法需要捕获IO异常
//设置音频文件到MediaPlayer对象中
mMediaPlayer.setDataSource(file.getPath());
//让MediaPlayer对象准备
mMediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!mMediaPlayer.isPlaying()) {
//如果还没开始播放,就开始
mMediaPlayer.start();
}
break;
case R.id.pause:
if (mMediaPlayer.isPlaying()) {
//如果在播放,就暂停
mMediaPlayer.pause();
}
break;
case R.id.reset:
if (mMediaPlayer.isPlaying()) {
//如果在播放,就恢复到初始状态
mMediaPlayer.reset();
//重新添加file文件到MediaPlayer对象并且准备播放音频
iniMediaPlayerFile();
}
break;
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
//停止播放音频
mMediaPlayer.stop();
//释放内存
mMediaPlayer.release();
}
}
保证sd卡根目录下有音频文件mu.mp3,启动程序,这个时候就写好了播放器了
音频看不见,截图不放了
网络流放歌
其实是下载完了再放,和酷狗那些播放器不一样
String url = "http://........"; // 你的音频URL
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // 可能会很久,为了缓冲
mediaPlayer.start();
Activity里初始化一个新的MediaPlayer对象mediaPlayer2
private MediaPlayer mediaPlayer2 = new MediaPlayer();
布局文件加入3个控制按钮
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="网络流形式"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/play2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="play"/>
<Button
android:id="@+id/pause2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="pause"/>
<Button
android:id="@+id/reset2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="reset"/>
</LinearLayout>
添加方法iniMediaPlayerInternet()
//初始化网络流播放
private void iniMediaPlayerInternet() {
String url = "http://win.web.rc03.sycdn.kuwo.cn/9c1f523c86e747c0d7219559befbc493/58ca8370/resource/a2/23/27/1060703964.aac";
//设置资源为音频流
mediaPlayer2.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer2.setDataSource(url);
mediaPlayer2.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
在判断权限的加入
.....
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 1);
}else{
iniMediaPlayerFile();
//加到这里
iniMediaPlayerInternet();
.....
}```
按钮事件
case R.id.play2:
if (!mediaPlayer2.isPlaying()) {
mediaPlayer2.start();
}
break;
case R.id.pause2:
if (mediaPlayer2.isPlaying()) {
mediaPlayer2.pause();
}
break;
case R.id.reset2:
if (mediaPlayer2.isPlaying()) {
mediaPlayer2.reset();
iniMediaPlayerInternet();
}
break;
最后同file播放形式要释放内存
记得声明网络权限,
<uses-permission android:name="android.permission.INTERNET"/>
编译安装,听到歌。。。
#内容提供器(content provider)播放
**步骤**
首先获取歌的列表信息
ContentResolver contentResolver = getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
// 查询失败,则处理错误
} else if (!cursor.moveToFirst()) {
//moveToFirst()
//This method will return false if the cursor is empty.
//设备里没歌
} else {
int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
do {
long thisId = cursor.getLong(idColumn);//这个id就是播放的id
String thisTitle = cursor.getString(titleColumn);
// ...process entry...
} while (cursor.moveToNext());
}
cursor.close();
> ###注意
这里要把cursor关掉
接着
//这个id就是播放的id,对应扫描到音乐id,可以在下面控制
long id = /* retrieve it from somewhere */;
Uri contentUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(getApplicationContext(), contentUri);
// ...prepare and start...
具体例子
同上添加
···
private long[] idBag;//申请一个存歌曲id的long数组
···
private void iniMediaPlayercontentUri() {
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
Toast.makeText(this, "query failed,这里可以处理错误", Toast.LENGTH_SHORT).show();
} else if (!cursor.moveToFirst()) {
Toast.makeText(this, "没有歌在设备上", Toast.LENGTH_SHORT).show();
} else {
Log.d(TAG, "有歌在设备");
Toast.makeText(this, "有歌在设备", Toast.LENGTH_SHORT).show();
int titleColumn = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
//歌曲数量
int totalMusic = cursor.getCount();
idBag = new long[totalMusic];
do {
long thisId = cursor.getLong(idColumn);
String thisTitle = cursor.getString(titleColumn);
//这里还可以写更多过程获取更多媒体的详细数据
idBag[i] = thisId;
i++;
Log.d(TAG, "当前歌曲id:"+thisId);
Log.d(TAG, "当前歌曲title:"+thisTitle);
} while (cursor.moveToNext());
}
//具体放歌
long id = idBag[0];
Uri contentUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
mediaPlayer3.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer3.setDataSource(this, contentUri);
mediaPlayer3.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
####贴一下全部代码
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Button palyButton;
private Button pauseButton;
private Button resetButton;
private Button palyButton2;
private Button pauseButton2;
private Button resetButton2;
private Button palyButton3;
private Button pauseButton3;
private Button resetButton3;
private long[] idBag;
private int i = 0;
private MediaPlayer mMediaPlayer = new MediaPlayer();
private MediaPlayer mediaPlayer2 = new MediaPlayer();
private MediaPlayer mediaPlayer3 = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iniView();
//判断权限够不够,不够就给
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 {
iniMediaPlayerFile();
iniMediaPlayerInternet();
iniMediaPlayercontentUri();
}
}
private void iniMediaPlayercontentUri() {
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
Toast.makeText(this, "query failed,这里可以处理错误", Toast.LENGTH_SHORT).show();
} else if (!cursor.moveToFirst()) {
Toast.makeText(this, "没有歌在设备上", Toast.LENGTH_SHORT).show();
} else {
Log.d(TAG, "有歌在设备");
Toast.makeText(this, "有歌在设备", Toast.LENGTH_SHORT).show();
int titleColumn = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
//歌曲数量
int totalMusic = cursor.getCount();
idBag = new long[totalMusic];
do {
long thisId = cursor.getLong(idColumn);
String thisTitle = cursor.getString(titleColumn);
//这里还可以写更多过程获取更多媒体的详细数据
idBag[i] = thisId;
i++;
Log.d(TAG, "当前歌曲id:"+thisId);
Log.d(TAG, "当前歌曲title:"+thisTitle);
} while (cursor.moveToNext());
}
long id = idBag[0];
Uri contentUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
mediaPlayer3.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer3.setDataSource(this, contentUri);
mediaPlayer3.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
private void iniMediaPlayerInternet() {
String url = "http://win.web.rc03.sycdn.kuwo.cn/9c1f523c86e747c0d7219559befbc493/58ca8370/resource/a2/23/27/1060703964.aac";
//设置资源为音频流
mediaPlayer2.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer2.setDataSource(url);
mediaPlayer2.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
private void iniMediaPlayerFile() {
//获取文件路径
File file = new File(Environment.getExternalStorageDirectory(), "mu.mp3");
try {
//此处的两个方法需要捕获IO异常
//设置音频文件到MediaPlayer对象中
mMediaPlayer.setDataSource(file.getPath());
//让MediaPlayer对象准备
mMediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
//获取到权限回调方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
iniMediaPlayerFile();
} else {
Toast.makeText(this, "权限不够获取不到音乐,程序将退出", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
}
break;
case R.id.pause:
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
break;
case R.id.reset:
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
iniMediaPlayerFile();
}
break;
case R.id.play2:
if (!mediaPlayer2.isPlaying()) {
mediaPlayer2.start();
}
break;
case R.id.pause2:
if (mediaPlayer2.isPlaying()) {
mediaPlayer2.pause();
}
break;
case R.id.reset2:
if (mediaPlayer2.isPlaying()) {
mediaPlayer2.reset();
iniMediaPlayerInternet();
}
break;
case R.id.play3:
if (!mediaPlayer3.isPlaying()) {
mediaPlayer3.start();
Log.d(TAG, "播放");
}else {
Log.d(TAG, "播放shibai2");
}
break;
case R.id.pause3:
if (mediaPlayer3.isPlaying()) {
mediaPlayer3.pause();
}
break;
case R.id.reset3:
if (mediaPlayer3.isPlaying()) {
mediaPlayer3.reset();
iniMediaPlayercontentUri();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
}
}
private void iniView() {
palyButton = (Button) findViewById(R.id.play);
pauseButton = (Button) findViewById(R.id.pause);
resetButton = (Button) findViewById(R.id.reset);
palyButton.setOnClickListener(this);
pauseButton.setOnClickListener(this);
resetButton.setOnClickListener(this);
palyButton2 = (Button) findViewById(R.id.play2);
pauseButton2 = (Button) findViewById(R.id.pause2);
resetButton2 = (Button) findViewById(R.id.reset2);
palyButton2.setOnClickListener(this);
pauseButton2.setOnClickListener(this);
resetButton2.setOnClickListener(this);
palyButton3 = (Button) findViewById(R.id.play3);
pauseButton3 = (Button) findViewById(R.id.pause3);
resetButton3 = (Button) findViewById(R.id.reset3);
palyButton3.setOnClickListener(this);
pauseButton3.setOnClickListener(this);
resetButton3.setOnClickListener(this);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.minminaya.mediaplayer.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="file文件形式"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="play"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="pause"/>
<Button
android:id="@+id/reset"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="reset"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="网络流形式"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/play2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="play"/>
<Button
android:id="@+id/pause2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="pause"/>
<Button
android:id="@+id/reset2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="reset"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容提供器(本机设备音乐)形式"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/play3"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="play"/>
<Button
android:id="@+id/pause3"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="pause"/>
<Button
android:id="@+id/reset3"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="reset"/>
</LinearLayout>
</LinearLayout>
![2017-03-16 22_17_16-MediaPlayer - [E__workspace_as20160914_MediaPlayer] - [app] - ..._app_src_main_r.png](http://upload-images.jianshu.io/upload_images/3515789-9e2280b5885e9424.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#使用锁
CPU
> To ensure that the CPU continues running while your [MediaPlayer](https://developer.android.google.cn/reference/android/media/MediaPlayer.html)
is playing, call the [setWakeMode()](https://developer.android.google.cn/reference/android/media/MediaPlayer.html#setWakeMode(android.content.Context, int))
method when initializing your [MediaPlayer](https://developer.android.google.cn/reference/android/media/MediaPlayer.html)
. Once you do, the [MediaPlayer](https://developer.android.google.cn/reference/android/media/MediaPlayer.html)
holds the specified lock while playing and** releases **the lock when paused or stopped:
mMediaPlayer = new MediaPlayer();
// ... other initialization here ...
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
WIFI
> However, the wake lock acquired in this example guarantees only that the CPU remains awake. If you are streaming media over the network and you are using Wi-Fi, you probably want to hold a [WifiLock](https://developer.android.google.cn/reference/android/net/wifi/WifiManager.WifiLock.html)
as well, which you must acquire and release manually. So, when you start preparing the [MediaPlayer](https://developer.android.google.cn/reference/android/media/MediaPlayer.html)
with the remote URL, you should create and acquire the Wi-Fi lock.
WifiLock wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
wifiLock.acquire();
停止播放释放锁
wifiLock.release();
#播放错误,监听处理
在某种情况下播放时可能会报错,为了处理这种情况可以设置MediaPlayer.OnErrorListener监听,来恢复之前的状态,记得先释放它
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
---
#注意事项
哈哈哈,我又忘记了
- 注意释放内存,不然会GG
- 使用contentProvider加载外部音频放在子线程里,不然ANR
---
#在服务里实现后台放歌
[Service 与 MediaPlayer学习后音乐播放器的实现](//www.greatytc.com/p/836a3d7a8247)
---
参考自
[Google-Mediaplayer](https://developer.android.google.cn/guide/topics/media/mediaplayer.html)
[Mediaplayer参考](https://developer.android.google.cn/reference/android/media/MediaPlayer.html)