以下代码块为初始化代码(详见Demo源码中DemoPlayer.java)
Player player = new Player(context, handler, url);
//设置全屏模式
player.setFullscreenMode(Player.FullscreenMode.FULLSCREEN);
Surface surface = new Surface(context, player);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
surface.setLayoutParams(params);
FrameLayout frameContainer = (FrameLayout) findViewById(R.id.framecontainer);
frameContainer.addView(surface);
SDK会通过初始化传入的handler
发送消息的形式通知应用播放是否成功以及播放结束等状态,应用可以使用下列代码获得播放状态信息:
handler = new Handler() {
public void handleMessage(Message msg) {
if (!Thread.currentThread().isInterrupted()) {
switch (msg.what) {
case Player.MSG_OPEN_OK:
Toast.makeText(getApplication(), "读取视频文件 " + fileName + " 成功!", Toast.LENGTH_LONG).show();
break;
case Player.MSG_OPEN_ERROR:
Toast.makeText(getApplication(), "读取视频文件 " + fileName + " 失败!", Toast.LENGTH_LONG).show();
break;
case Player.MSG_PLAYER_STOPPED:
Toast.makeText(getApplication(), "播放结束", Toast.LENGTH_LONG).show();
break;
case Player.MSG_READ_ERROR:
Toast.makeText(getApplication(), "读取数据错误", Toast.LENGTH_LONG).show();
break;
}
}
super.handleMessage(msg);
}
};
通过player.setFullscreenMode来设置播放器的全屏模式(三种全屏模式详见数据类型章节FullscreenMode介绍)。
横屏播放
在AndroidManifest.xml
配置播放器Activity
的android:screenOrientation=”landscape”
属性
竖屏播放
在AndroidManifest.xml
配置播放器Activity
的android:screenOrientation=”portrait”
属性