前言
vlc for android 是一款强大的android播放器,支持较多的视频格式,当然,也支持网络流媒体格式。
下面通过一段示例工程来演示.
开始
mLibVLC=VLCInstance.getLibVlcInstance(getApplicationContext());
注:这里需要静态的Context,一般有两种做法,一种就是获取Application的,另一种如下所示.
public class VLCApplication extends Application {
private static VLCApplication sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public static Context getAppContext() {
return sInstance;
}
}
然后在AndroidManifest中声明这个Application:
android:name=".VLCApplication"
3.EnventHandler
EventHandler em = EventHandler.getInstance();
em.addHandler(mVlcHandler);
private Handler mVlcHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg == null || msg.getData() == null)
return;
switch (msg.getData().getInt("event")) {
case EventHandler.MediaPlayerTimeChanged:
break;
case EventHandler.MediaPlayerPositionChanged:
break;
case EventHandler.MediaPlayerPlaying:
mHandler.removeMessages(HANDLER_BUFFER_END);
mHandler.sendEmptyMessage(HANDLER_BUFFER_END);
break;
case EventHandler.MediaPlayerBuffering:
break;
case EventHandler.MediaPlayerLengthChanged:
break;
case EventHandler.MediaPlayerEndReached:
endReached();
break;
}
}
};
4.surfaceView的Holder ->callbcak
surfaceView = (SurfaceView) findViewById(R.id.play_view);
surfaceHolder = surfaceView.getHolder(); surfaceHolder.setFormat(PixelFormat.RGBA_8888);
surfaceHolder.addCallback(this);
注:callBack方法
public void surfaceChanged(SurfaceHolder surfaceholder, int format, int width, int height)
{
surfaceHolder = surfaceholder;
if (mLibVLC != null) {
mLibVLC.attachSurface(surfaceholder.getSurface(), this);//, width, height
}
if (width > 0) {
mVideoHeight = height;
mVideoWidth = width;
}
}
//callback video view destroyed
public void surfaceDestroyed(SurfaceHolder surfaceholder)
{
mLibVLC.detachSurface();
System.out.println("surfaceDestroyed called");
}
//callback video view created
public void surfaceCreated(SurfaceHolder holder)
{
if (mLibVLC != null) {
surfaceHolder = holder;
mLibVLC.attachSurface(holder.getSurface(), this);
}
}
@Override
public void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {
mVideoHeight = height;
mVideoWidth = width;
mVideoVisibleHeight = visible_height;
mVideoVisibleWidth = visible_width;
mSarNum = sar_num;
mSarDen = sar_den;
mHandler.removeMessages(HANDLER_SURFACE_SIZE);
mHandler.sendEmptyMessage(HANDLER_SURFACE_SIZE);
}
5.播放、暂停控制
/**
* 点击事件
*/
private void pause()
{
if (mLibVLC.isPlaying()) {
mLibVLC.pause();
}
}
private void play()
{
if (!mLibVLC.isPlaying())
{
mLibVLC.play();
}
}
6.全屏、屏幕旋转
重写onConfigurationChanged方法,在此方法中处理surfaceView的大小.
public void onConfigurationChanged(Configuration newConfig) {
}
注:监听屏幕旋转需要权限,需在AndroidManifest中加入
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION">uses-permission>
其次,对应的Acitivity也需添加:
android:configChanges="orientation|keyboard">