//生成一个新的surface view
surfaceView = new SurfaceView(mContext);
surfaceView.getHolder().addCallback(new LmnSurfaceCallback());
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT
, LayoutParams.MATCH_PARENT, Gravity.CENTER);
surfaceView.setLayoutParams(layoutParams);
this.addView(surfaceView);
}
/**
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//surfaceview创建成功后,加载视频
load();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
/**
mMediaPlayer.prepareAsync();
}
/**
//开启硬解码 ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, “mediacodec”, 1);
mMediaPlayer = ijkMediaPlayer;
if (listener != null) {
mMediaPlayer.setOnPreparedListener(listener);
mMediaPlayer.setOnInfoListener(listener);
mMediaPlayer.setOnSeekCompleteListener(listener);
mMediaPlayer.setOnBufferingUpdateListener(listener);
mMediaPlayer.setOnErrorListener(listener);
}
}
public void setListener(VideoPlayerListener listener) {
this.listener = listener;
if (mMediaPlayer != null) {
mMediaPlayer.setOnPreparedListener(listener);
}
}
/**
public void start() {
if (mMediaPlayer != null) {
mMediaPlayer.start();
}
}
public void release() {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
public void pause() {
if (mMediaPlayer != null) {
mMediaPlayer.pause();
}
}
public void stop() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
}
}
public void reset() {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
}
}
public long getDuration() {
if (mMediaPlayer != null) {
return mMediaPlayer.getDuration();
} else {
return 0;
}
}
public long getCurrentPosition() {
if (mMediaPlayer != null) {
return mMediaPlayer.getCurrentPosition();
} else {
return 0;
}
}
public void seekTo(long l) {
if (mMediaPlayer != null) {
mMediaPlayer.seekTo(l);
}
}
}
我们这个控件继承自framelayout。这个控件负责存放一个surfaceView和一个IMediaPlayer 。
3,设置监听器,我自己定义了一个监听器,继承了IMediaPlayer的n个listener。
public abstract class VideoPlayerListener implements IMediaPlayer.OnBufferingUpdateListener, IMediaPlayer.OnCompletionListener, IMediaPlayer.OnPreparedListener, IMediaPlayer.OnInfoListener, IMediaPlayer.OnVideoSizeChangedListener, IMediaPlayer.OnErrorListener, IMediaPlayer.OnSeekCompleteListener {
}
当我们继承这个抽象类的时候,最重要的是在onPrepared()方法中,让视频开始播放:
ijkPlayer.setListener(new VideoPlayerListener() {
@Override
public void onBufferingUpdate(IMediaPlayer mp, int percent) {
}
@Override
public void onCompletion(IMediaPlayer mp) {
}
@Override
public boolean onError(IMediaPlayer mp, int what, int extra) {
return false;
}
@Override
public boolean onInfo(IMediaPlayer mp, int what, int extra) {
return false;
}
@Override
public void onPrepared(IMediaPlayer mp) {
// 视频准备好播放了,但是他不会自动播放,需要手动让他开始。
mp.start();
}
@Override
public void onSeekComplete(IMediaPlayer mp) {
}
@Override
public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sar_num, int sar_den) {
//在此可以获取到视频的宽和高
}
});
//加载native库
try {
IjkMediaPlayer.loadLibrariesOnce(null);
IjkMediaPlayer.native_profileBegin(“libijkplayer.so”);
} catch (Exception e) {
this.finish();
}
ijkPlayer.setListener(…)
ijkPlayer.setVideoPath(path);
记得在onStop()方法中关闭native库
IjkMediaPlayer.native_profileEnd();
这样就完成了一个简单的视频播放器了。
1,IjkMediaPlayer的setDataSource是不建议重新赋值的,每次更改视频源都需要:player.release() -> create new player -> player.setDataSource。
①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以点击我的【Github】免费下载,最后觉得有帮助、有需要的朋友可以点个赞
9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)免费下载**,最后觉得有帮助、有需要的朋友可以点个赞
[外链图片转存中…(img-ALpwVXNH-1646385511942)]
[外链图片转存中…(img-y04DNcuy-1646385511943)]
[外链图片转存中…(img-k2n255NL-1646385511944)]