前言
之前看了农民伯伯的文章,不得不佩服起来 这个万能的播放器 Vitamio,默默地为这些辛勤的开发者点个赞,
看农民伯伯博客链接 CSDN ~~Vitamio Github链接
自己使用经验
第一步使用之前:在github上下载下来Vitamio的依赖包,在AS中引用过来.不会引用 自行百度吧….
第二步:自己创建一个PlayVideoOnlineActivity一个播放视频的类,来做测试,记得Mainister注册下和加上权限配置.
@Override
protected void onCreate (Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
Vitamio.isInitialized(this );
setContentView(R.layout.activity_play_video);
playviedo();
}
这一步就是 中 在serView 之前 初始化 ,也可以在 Vitamio.isInitialized(this); 这句话初始化,看看里面的源代码
public static boolean isInitialized (Context ctx) {
vitamioPackage = ctx.getPackageName();
vitamioLibraryPath = ContextUtils.getDataDir(ctx) + "libs/" ;
File dir = new File(getLibraryPath());
if (dir.exists() && dir.isDirectory()) {
String[] libs = dir.list();
if (libs != null ) {
Arrays.sort(libs);
for (String L : getRequiredLibs()) {
if (Arrays.binarySearch(libs, L) < 0 ) {
Log.e("Native libs %s not exists!" , L);
return false ;
}
}
File lock = new File(getLibraryPath() + LIBS_LOCK);
BufferedReader buffer = null ;
try {
buffer = new BufferedReader(new FileReader(lock ));
int appVersion = ContextUtils.getVersionCode(ctx);
int libVersion = Integer.valueOf(buffer.readLine());
Log.i("isNativeLibsInited, APP VERSION: %d, Vitamio Library version: %d" , appVersion, libVersion);
if (libVersion == appVersion)
return true ;
} catch (IOException e) {
Log.e("isNativeLibsInited" , e);
} catch (NumberFormatException e) {
Log.e("isNativeLibsInited" , e);
} finally {
IOUtils.closeSilently(buffer);
}
}
}
return false ;
}
看了下源码这里是 在本地创建一个初始化lib文件夹
接下来就是给 VideoView进出初始化配置
mVideoView = (VideoView) findViewById(R.id .surface _view)
// mVideoView.setVideoURI (Uri.parse (path))
mVideoView.setVideoPath ("http://movie.ks.js.cn/flv/other/2014/06/20-2.flv" )
mVideoView.setMediaController (new MediaController(this))
//视频播放器的准备,此时播放器已经准备好了,此处可以设置一下播放速度,播放位置等等
mVideoView.setVideoLayout (VideoView.VIDEO _LAYOUT_STRETCH,0 )
mVideoView.requestFocus ()
这里是初始化配置,设置网络地址地方.不得不说 这个万能播放器支持那么多格式,有这么多格式 自动解析.
还是看看源码里是怎么写的:
public void setVideoURI(Uri uri, Map < String , String > headers) {
mUri = uri;
mHeaders = headers;
mSeekWhenPrepared = 0 ;
openVideo();
requestLayout();
invalidate();
}
找啊找阿,终于找到匹配各种格式地方了
public void setDataSource (Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
if (context == null || uri == null )
throw new IllegalArgumentException();
String scheme = uri.getScheme();
if (scheme == null || scheme.equals("file" )) {
setDataSource(FileUtils.getPath(uri.toString()));
return ;
}
try {
ContentResolver resolver = context.getContentResolver();
mFD = resolver.openAssetFileDescriptor(uri, "r" );
if (mFD == null )
return ;
setDataSource(mFD.getParcelFileDescriptor().getFileDescriptor());
return ;
} catch (Exception e) {
closeFD();
}
setDataSource(uri.toString(), headers);
}
这里应该是设置 视频源,
在下一层就是 调本地C的方法
/**
* Sets the data source (file-path or http/rtsp/mms URL) to use.
*
* @param path the path of the file, or the http/rtsp/mms URL of the stream you
* want to play
* @param keys AVOption key
* @param values AVOption value
* @throws IllegalStateException if it is called in an invalid state
*/
private native void _setDataSource (String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, IllegalStateException;
这里支持 http.rtsp.mms流协议传输,这个MeidiaPlayer就调底层so库的方法了…JNI编程不太了解~
返回来看下 之前的
mVideoView.setMediaController(new MediaController(this ));
这里就是 设置 媒体的控制器 进度条 以及时间之类的,一开始跑不起来 原来漏了这一句
其他东西 就是一些监听器,更高级的用法还是看农民伯伯的文章后续在研究研究
有什么说错的地方 还望网友指教~~