-
Notifications
You must be signed in to change notification settings - Fork 247
KSYMediaPlayerBasicExample
xinbaicheng edited this page Sep 6, 2016
·
2 revisions
本文档给出里播放SDK核心类KSYMediaPlayer的基本调用示例,协助用户快速集成。 本文档涉及的基本定义如下
public class VideoPlayerActivity extends Activity{
// 播放器的对象
private KSYMediaPlayer ksyMediaPlayer;
// 播放SDK提供的监听器
// 播放器在准备完成,可以开播时会发出onPrepared回调
private IMediaPlayer.OnPreparedListener mOnPreparedListener;
// 播放完成时会发出onCompletion回调
private IMediaPlayer.OnCompletionListener mOnCompletionListener;
// 播放器遇到错误时会发出onError回调
private IMediaPlayer.OnErrorListener mOnErrorListener;
private IMediaPlayer.OnInfoListener mOnInfoListener;
private IMediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangeListener;
private IMediaPlayer.OnSeekCompleteListener mOnSeekCompletedListener;
// SurfaceView需在Layout中定义,此处不在赘述
private SurfaceView mVideoSurfaceView;
private SurfaceHolder mSurfaceHolder;
}
KSYMediaPlayer
对象的创建采用Builder模式
public class VideoPlayerActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
ksyMediaPlayer = new KSYMediaPlayer.Builder(this.getApplicationContext()).build();
}
}
KSYMediaPlayer
提供了相应的监听器和设置接口,建议用户全部都设置,需要强调的是IMediaPlayer.OnPreparedListener为必须设置的接口。
public class VideoPlayerActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
ksyMediaPlayer = new KSYMediaPlayer.Builder(this.getApplicationContext()).build();
ksyMediaPlayer.setOnCompletionListener(mOnCompletionListener);
ksyMediaPlayer.setOnPreparedListener(mOnPreparedListener);
ksyMediaPlayer.setOnInfoListener(mOnInfoListener);
ksyMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangeListener);
ksyMediaPlayer.setOnErrorListener(mOnErrorListener);
ksyMediaPlayer.setOnSeekCompleteListener(mOnSeekCompletedListener);
}
}
下面将以IMediaPlayer.OnCompletionListener
为例,展示其用法
private IMediaPlayer.OnCompletionListener mOnCompletionListener = new IMediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(IMediaPlayer mp) {
// 播放完成,用户可选择释放播放器
if(ksyMediaPlayer != null) {
ksyMediaPlayer.stop();
ksyMediaPlayer.release();
}
}
};
prepareAsync
为异步接口,其功能是建立连接、读取数据、创建解码器等准备播放的工作
public class VideoPlayerActivity extends Activity{
String mVideoUrl = "http://xxxxx"; // mVideoUrl为视频的播放地址
protected void onCreate(Bundle savedInstanceState) {
try {
ksyMediaPlayer.setDataSource(mVideoUrl);
ksyMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
}
KSYMediaPlayer
提供了setDisplay
和setSurface
两个接口设置视频渲染所需的Surface
public class VideoPlayerActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
mSurfaceHolder = mVideoSurfaceView.getHolder();
mSurfaceHolder.addCallback(mSurfaceCallback);
}
private final SurfaceHolder.Callback mSurfaceCallback = new Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(ksyMediaPlayer != null) {
ksyMediaPlayer.setDisplay(holder);
ksyMediaPlayer.setScreenOnWhilePlaying(true);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 此处非常重要,必须调用!!!
if(ksyMediaPlayer != null) {
ksyMediaPlayer.setDisplay(null);
}
}
}
}
当收到onPrepared回调时,表明可以开始播放
private IMediaPlayer.OnPreparedListener mOnPreparedListener = new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer mp) {
if(ksyMediaPlayer != null) {
// 设置视频伸缩模式,此模式为裁剪模式
ksyMediaPlayer.setVideoScalingMode(KSYMediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
// 开始播放视频
ksyMediaPlayer.start();
}
}
}
if(ksyMediaPlayer != null)
ksyMediaPlayer.stop();
if(ksyMediaPlayer != null)
ksyMediaPlayer.release();
ksyMediaPlayer = null;