使用ijk播放器实现视频的播放
ijkplayer播放器是Bilibili开源的一款播放器 github地址:https://github.com/Bilibili/ijkplayer;ijkplayer 是一款做视频直播的框架, 基于ffmpeg, 支持 Android 和 iOS.实现了跨平台功能,API易于集成;编译配置可裁剪,方便控制安装包大小;支持硬件加速解码,更加省电;提供Android平台下应用弹幕集成的解决方案,此方案目前已用于美拍和斗鱼 APP。
/**
* 基于 七牛ikj的播放器组件
*/
public class ShortVideoView extends FrameLayout {
private PLVideoView plVideoView;
public ShortVideoView(Context context) {
super(context);
init(context);
}
public ShortVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ShortVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
initView();
}
/**
* 初始化播放器控件
*/
private void initView() {
plVideoView = new PLVideoView(getContext());
LayoutParams layoutParams= new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
initVideoView();
addView(plVideoView,layoutParams);
}
private void initVideoView()
{
AVOptions options = new AVOptions();
options.setInteger(AVOptions.KEY_PREPARE_TIMEOUT, 10 * 1000);
options.setInteger(AVOptions.KEY_GET_AV_FRAME_TIMEOUT, 10 * 1000);
options.setInteger(AVOptions.KEY_START_ON_PREPARED, 0);
plVideoView.setAVOptions(options);
plVideoView.setDisplayAspectRatio(PLVideoView.ASPECT_RATIO_PAVED_PARENT);
}
public void play(String url)
{
plVideoView.setVideoPath(url);
plVideoView.start();
}
public void resume()
{
plVideoView.start();
}
public void pause()
{
plVideoView.pause();
}
public void release()
{
plVideoView.stopPlayback();
}
}
/**
* video的事件处理
*/
public class VideoPresent implements VideoEvent.Presenter {
private VideoEvent.View view;
private Context context;
private ScreenBroadcastReceiver screenReceiver = new ScreenBroadcastReceiver();
public VideoPresent(VideoEvent.View view, Context context) {
this.view = view;
this.context = context;
}
@Override
public void getVideoUrl(String postId) {
view.updateView(postId);
}
@Override
public void registerListerent() {IntentFilter filter = new IntentFilter();
//屏幕开启 filter.addAction(Intent.ACTION_SCREEN_ON); //锁定屏幕 filter.addAction(Intent.ACTION_SCREEN_OFF); //解锁 filter.addAction(Intent.ACTION_USER_PRESENT);
context.registerReceiver(screenReceiver, filter);}
public class VideoEvent { public interface Presenter{ void getVideoUrl(String postId); void registerListerent(); void releaseListerent(String postId); } public interface View{ void updateView(String entity); void screenOn(); void screenOff(); void userPresent(); }}
Fragment中调用
postId = "http://xx.mp4";mvideoPresent = new VideoPresent(this,getContext());mvideoPresent.registerListerent();mvideoPresent.getVideoUrl(postId);
//------------处理短视频功能-------------------@Overridepublic void updateView(String entity) { mshortVideoView.play(entity);}@Overridepublic void screenOn() {}@Overridepublic void screenOff() { mshortVideoView.pause();}@Overridepublic void userPresent() { mshortVideoView.resume();}@Overridepublic void onDestroy() { super.onDestroy(); mshortVideoView.release(); mvideoPresent.releaseListerent(postId);}//------------处理短视频功能 结束-------------------
记得加上权限
<uses-permission android:name="android.permission.WAKE_LOCK"/>