/**
* 视频⽂件地址
*/
private String mPath = "";
private SurfaceView surfaceView;
private VideoPlayerListener listener;
private Context mContext;
public VideoPlayerIJK(@NonNull Context context) {
super(context);
initVideoView(context);
}
public VideoPlayerIJK(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initVideoView(context);
}
public VideoPlayerIJK(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr);
initVideoView(context);
}
private void initVideoView(Context context) {
mContext = context;
//获取焦点,不知道有没有必要~。~
setFocusable(true);
}
/**
* 设置视频地址。
* 根据是否第⼀次播放视频,做不同的操作。
*
* @param path the path of the video.
*/
public void setVideoPath(String path) {
if (TextUtils.equals("", mPath)) {
//如果是第⼀次播放视频,那就创建⼀个新的surfaceView
mPath = path;
createSurfaceView();
} else {
//否则就直接load
mPath = path;
load();
}
}
/**
* 新建⼀个surfaceview
*/
private void createSurfaceView() {
/
/⽣成⼀个新的surface view
surfaceView = new SurfaceView(mContext);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT
, LayoutParams.MATCH_PARENT, Gravity.CENTER);
surfaceView.setLayoutParams(layoutParams);
this.addView(surfaceView);
}
/**
* surfaceView的
*/
private class LmnSurfaceCallback implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//surfaceview创建成功后,加载视频
load();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
/**
* 加载视频
*/
private void load() {
//每次都要重新创建IMediaPlayer
createPlayer();
try {
mMediaPlayer.setDataSource(mPath);
} catch (IOException e) {
e.printStackTrace();
}
//给mediaPlayer设置视图
mMediaPlayer.Holder());
mMediaPlayer.prepareAsync();
}
/**
* 创建⼀个新的player
*/
private void createPlayer() {
if (mMediaPlayer != null) {
视频播放器十大排名
mMediaPlayer.stop();
mMediaPlayer.setDisplay(null);
}
IjkMediaPlayer ijkMediaPlayer = new IjkMediaPlayer();
ijkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG);
//开启硬解码 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) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论