android视频播放现在大多数使用IjkPlayer,不过此库使用难度大
经过实际使用,推荐JieCaoVideoPlayer开源库,简单易用
- 使用
dependencies {
compile 'fm.jiecao:jiecaovideoplayer:4.7.0'
}```
布局 layout:
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="@+id/jc_player"
android:layout_width="match_parent"
android:layout_height="200dp"/>
Activity:
@BindView(R.id.jc_player)
JCVideoPlayerStandard mPLayer;
//增加手机感应,自动全屏/小屏
JCVideoPlayer.JCAutoFullscreenListener sensorListener;
SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
ButterKnife.bind(this);
mSensorManager=(SensorManager)getSystemService (SENSOR_SERVICE);
sensorListener=new JCVideoPlayer.JCAutoFullscreenListener(){
@Override
public void onSensorChanged(SensorEvent event) {
super.onSensorChanged(event);
float x = event.values[SensorManager.DATA_X];
float y =event.values[SensorManager.DATA_Y];
if (x < -10) { //direction right }
else if (x > 5) { //direction left
if (JCVideoPlayerManager.getFirst() != null) {
JCVideoPlayerManager.getFirst() .autoFullscreenLeft();
//mPLayer.startWindowFullscreen();
}
}
else if (y > 5) {
if (JCVideoPlayerManager.getFirst() != null) {
JCVideoPlayerManager.getFirst() .autoQuitFullscreen();
//mPLayer.autoQuitFullscreen();
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
super.onAccuracyChanged(sensor, accuracy);
}
};
mPLayer.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5a caaf51d105342e3.f20.mp4",
JCVideoPlayerStandard.SCREEN_LAYOUT_LIST, "嫂子闭眼睛");
// mPLayer.startWindowFullscreen();//直接全屏播放
}
@Override
protected void onResume()
{
super.onResume();
Sensor accelerometerSensor
=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager .registerListener(sensorListener,accelerometerSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onBackPressed()
{
if (JCVideoPlayer.backPress()) {
return;
}
super.onBackPressed();
}
@Override
protected void onPause()
{
super.onPause();
JCVideoPlayer.releaseAllVideos();
mSensorManager.unregisterListener(sensorListener);
}
是不是使用很简单方便,以后播放视频再也不用烦了