android视频播放总结
全屏原理
操作系统的横竖屏变化,但不让他从新加载页面
活动注册要这么写
<activity
android:name=".CourseDetailVideo.CourseDetailVideoActivity"
android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
android:launchMode="singleTop"
android:windowSoftInputMode="stateAlwaysHidden"></activity>
主函数先获取屏幕长宽高
screenHeight=getWindowManager().getDefaultDisplay().getHeight();
screenWidth=getWindowManager().getDefaultDisplay().getWidth();
全屏前要先滑动到最上面
nestedScrollView.fullScroll(NestedScrollView.FOCUS_UP);
全屏的具体切换
private final int flagsFullScreen = WindowManager.LayoutParams.FLAG_FULLSCREEN;
private void goFullScreen(){
nestedScrollView.fullScroll(NestedScrollView.FOCUS_UP);
getWindow().addFlags(flagsFullScreen);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
frameLayoutControl.setBackgroundColor(Color.BLACK);
frameLayoutVideo.setLayoutParams(new
LinearLayout.LayoutParams(screenHeight,screenWidth));
defaultTimeBar.setLayoutParams(new
LinearLayout.LayoutParams(getPixelsFromDp(timeLenFull),-1));
frameLayoutBottom.setVisibility(View.INVISIBLE);
linearLayoutIntroduce.setVisibility(View.INVISIBLE);
imageButtonBack.setOnClickListener(goNormalListener);
imageButtonFullScreen.setVisibility(View.INVISIBLE);
textViewSpeed.setVisibility(View.VISIBLE);
isFullScreen=true;
}
private void goNormal(){
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= (~flagsFullScreen);
getWindow().setAttributes(attrs);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
frameLayoutControl.setBackgroundColor(Color.TRANSPARENT);
frameLayoutVideo.setLayoutParams(new LinearLayout.LayoutParams
(screenWidth,getPixelsFromDp(videoHeight)));
defaultTimeBar.setLayoutParams(new
LinearLayout.LayoutParams(getPixelsFromDp(timeLen),-1));
frameLayoutBottom.setVisibility(View.VISIBLE);
linearLayoutIntroduce.setVisibility(View.VISIBLE);
imageButtonBack.setOnClickListener(backListener);
imageButtonFullScreen.setVisibility(View.VISIBLE);
textViewSpeed.setVisibility(View.INVISIBLE);
isFullScreen=false;
}
注意在全屏的时候别忘了改变其他组件的属性
生成exoplayer
dataSourceFactory = new DefaultDataSourceFactory(context,
Util.getUserAgent(context,"CourseDetailVideoExoPlayer"));
MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(url));
player=new SimpleExoPlayer.Builder(context).build();
player.setPlayWhenReady(true);
player.prepare(mediaSource);
playerView.setPlayer(player);
exoplayer在生命周期函数中的变化
@Override
protected void onDestroy() {
super.onDestroy();
if(player!=null){
player.release();
}
}
@Override
protected void onPause() {
super.onPause();
if(player!=null){
player.setPlayWhenReady(false);
}
exoplayer切换播放时要先release
if(player!=null){
player.release();
player=null;
}
exoplayer改变倍速
PlaybackParameters playbackParameters=new PlaybackParameters(1f,1f);
if(player!=null)player.setPlaybackParameters(playbackParameters);
|