[Android多媒体技术] 播放Raw/Assets音视频方法
前言
本文介绍如何通过系统MediaPlayer
,IjkPlayer
,ExoPlayer
分别播放安卓项目下的Raw或Assets文件夹中的音视频文件。
在某些情况下,我们会把一些音视频文件,如Mp3,Mp4等,直接放在安装包中的Raw或者Assets文件夹里,这些音视频文件可能作为特定场景的提示音,或者视频片头等等。关于Raw和Assets资源文件,这里不作过多讨论,总的来讲,他们都是被打包进APK中的文件,不会被编译成二进制,程序可以直接访问,无需额外的权限。
先说明一下本文代码的构建环境和使用的播放内核版本:
- Java 1.7
- Android Studio 3.1.2
- Gradle 4.4
- IjkPlayer 0.8.8
- ExoPlayer 2.8.3
效果演示
Raw/Assets资源文件访问方式
在项目文件夹中的位置:
Raw文件访问方式
-
Raw
文件位于res/raw目录下,Raw文件会被映射到R.java文件中,所以访问的时候直接使用资源ID即可,如
R.raw.raw_video
或者获得该文件的AssetFileDescriptor:
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.raw_video);
Assets文件访问方式
-
Assets
文件夹下的文件不会被映射到R.java中,访问的时候需要AssetManager类。
AssetManager am = getAssets();
try {
AssetFileDescriptor afd= am.openFd(fileName);
} catch (IOException e) {
e.printStackTrace();
}
AssetFileDescriptor可以理解成访问Raw/Assets文件的一个入口,或者说是一把钥匙。
Raw/Assets文件还有其他的访问方式,比如通过ContentResolver
,又或者直接开启一个InputStream去读取文件,这应该是播放器内核需要做的事情,我们只需要给播放器提供以上的信息即可。
·········································································································
下面直接上代码
通过系统MediaPlayer播放音视频
- Raw文件
//实例化播放内核
android.media.MediaPlayer mediaPlayer = new android.media.MediaPlayer();
//获得播放源访问入口
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.raw_video); // 注意这里的区别
//给MediaPlayer设置播放源
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
//设置准备就绪状态监听
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// 开始播放
mediaPlayer.start();
}
});
//准备播放
mediaPlayer.prepareAsync();
- Assets 文件
//实例化播放内核
android.media.MediaPlayer mediaPlayer = new android.media.MediaPlayer();
//获得播放源访问入口
AssetManager am = getAssets();
try {
AssetFileDescriptor afd = am.openFd("assets_video.mp4");// 注意这里的区别
//给MediaPlayer设置播放源
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IOException e) {
e.printStackTrace();
}
//设置准备就绪状态监听
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// 开始播放
mediaPlayer.start();
}
});
//准备播放
mediaPlayer.prepareAsync();
代码中每一步都有注释,可以说非常详细了。简单总结一下,无论播放Raw文件还是Assets文件,我们首先获得AssetFileDescriptor,然后设置给MediaPlayer。
·········································································································
通过IjkPlayer播放音视频
- Raw 文件
//实例化播放内核
tv.danmaku.ijk.media.player.IjkMediaPlayer ijkPlayer = new tv.danmaku.ijk.media.player.IjkMediaPlayer();
//获得播放源访问入口
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.raw_video); // 注意这里的区别
//构建IjkPlayer能识别的IMediaDataSource,下面的RawDataSourceProvider实现了IMediaDataSource接口
RawDataSourceProvider sourceProvider = new RawDataSourceProvider(fd);
//给IjkPlayer设置播放源
ijkPlayer.setDataSource(sourceProvider);
//设置准备就绪状态监听
ijkPlayer .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// 开始播放
ijkPlayer.start();
}
});
//准备播放
ijkPlayer.prepareAsync();
- Assets 文件
//实例化播放内核
tv.danmaku.ijk.media.player.IjkMediaPlayer ijkPlayer = new tv.danmaku.ijk.media.player.IjkMediaPlayer();
//获得播放源访问入口
AssetManager am = getAssets();
try {
AssetFileDescriptor afd = am.openFd("assets_video.mp4");// 注意这里的区别
//构建IjkPlayer能识别的IMediaDataSource,下面的RawDataSourceProvider实现了IMediaDataSource接口
RawDataSourceProvider sourceProvider = new RawDataSourceProvider(fd);
//给IjkPlayer设置播放源
ijkPlayer.setDataSource(sourceProvider);
} catch (IOException e) {
e.printStackTrace();
}
//设置准备就绪状态监听
ijkPlayer .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// 开始播放
ijkPlayer.start();
}
});
//准备播放
ijkPlayer.prepareAsync();
补充下,其中的
RawDataSourceProvider
实现了IMediaDataSource
接口,IMediaDataSource
是IjkPlayer
包中的接口,实现了IMediaDataSource
接口的类可以设置给IjkPlayer作为播放源。就像下面这样:
import tv.danmaku.ijk.media.player.misc.IMediaDataSource;
public class RawDataSourceProvider implements IMediaDataSource {
private AssetFileDescriptor mDescriptor;
private byte[] mMediaBytes;
public RawDataSourceProvider(AssetFileDescriptor descriptor) {
this.mDescriptor = descriptor;
}
@Override
public int readAt(long position, byte[] buffer, int offset, int size) {
if (position + 1 >= mMediaBytes.length) {
return -1;
}
int length;
if (position + size < mMediaBytes.length) {
length = size;
} else {
length = (int) (mMediaBytes.length - position);
if (length > buffer.length)
length = buffer.length;
length--;
}
// 把文件内容copy到buffer中;
System.arraycopy(mMediaBytes, (int) position, buffer, offset, length);
return length;
}
@Override
public long getSize() throws IOException {
long length = mDescriptor.getLength();
if (mMediaBytes == null) {
InputStream inputStream = mDescriptor.createInputStream();
mMediaBytes = readBytes(inputStream);
}
return length;
}
@Override
public void close() throws IOException {
if (mDescriptor != null)
mDescriptor.close();
mDescriptor = null;
mMediaBytes = null;
}
//读取文件内容
private byte[] readBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
}
小结一下,我们首先获取到Raw/Assets文件的
AssetFileDescriptor
,然后用它去构建一个IMediaDataSource
,最后设置给IjkPlayer
。
·········································································································
通过ExoPlayer播放音视频
- ExoPlayer播放器内核的实例化有点复杂,这里单独写
//实例化播放内核
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter());
DefaultTrackSelector mTrackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
boolean preferExtensionDecoders = true;
boolean useExtensionRenderers = true;//是否开启扩展
@DefaultRenderersFactory.ExtensionRendererMode int extensionRendererMode = useExtensionRenderers
? (preferExtensionDecoders ? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF;
DefaultRenderersFactory rendererFactory = new DefaultRenderersFactory(mAppContext, extensionRendererMode);
DefaultLoadControl loadControl = new DefaultLoadControl();
//工厂方法获得播放器实例
om.google.android.exoplayer2.SimpleExoPlayer exoPlayer =
ExoPlayerFactory.newSimpleInstance(rendererFactory, mTrackSelector, loadControl, null);
- Raw 文件
//构建Raw文件播放源--RawResourceDataSource
DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(R.raw.raw_video));
RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(this);
try {
rawResourceDataSource.open(dataSpec);
} catch (RawResourceDataSource.RawResourceDataSourceException e) {
e.printStackTrace();
}
//构建ExoPlayer能识别的播放源--MediaSource
String url = rawDataSource.getUri().toString();
MediaSource mediaSource = ExoSourceManager.newInstance(mAppContext, getHeaders()).getMediaSource(
url, false, false, MediaPlayerManager.instance().isLooping(), null
);
//给ExoPlayer设置播放源,并准备播放
exoPlayer.prepare(mediaSource);
//让ExoPlayer准备好后就开始播放
exoPlayer.setPlayWhenReady(true);
- Assets 文件
//构建ExoPlayer能识别的播放源--MediaSource
String url = "file:///android_asset/" + "assets_video.mp4";
MediaSource mediaSource = ExoSourceManager.newInstance(mAppContext, getHeaders()).getMediaSource(
url, false, false, MediaPlayerManager.instance().isLooping(), null
);
//给ExoPlayer设置播放源,并准备播放
exoPlayer.prepare(mediaSource);
//让ExoPlayer准备好后就开始播放
exoPlayer.setPlayWhenReady(true);
小结:ExoPlayer实例化的时候复杂一点,需要按照官方文档一步步来。播放Raw文件的时候,需要先构建出
RawResourceDataSource
,这是ExoPlayer为了播放Raw音视频文件提供的类,然后获取其中的Uri构建MediaSource
,最后设置给ExoPlayer。播放Assets文件就更简单了,形如String url = "file:///android_asset/" + "filename"
的Assets文件播放地址可以直接用来构建MediaSource
。
项目地址:https://github.com/maiwenchang/ArtPlayer 欢迎各位Star~~