使用IJKPlayer播放视频实现了一些播放视频的基本操作
Iijkplayer目前是很流行的音视频框架,集成我这里就不多说了,网上很多,大概就是编译ijkplayer的源码得到里边的so文件,添加到自己项目的libs文件夹中,gradlle依赖下资源。我这里是在IjkVideoView基础上开发的,自己实现了播放、暂停、旋转全屏播放、音量和亮度调节、进度条、拖动进度条播放功能,可以满足普通的播放视频的需求;Demo下载
友情提示我播放的是本地视频需要自己在sd卡目录下放一个视频文件,6.0以上是需要动态申请读写sd卡权限的,我代码里边没有加入,我在手机设置里自己手动开启的。
看下动态效果图:
先看下布局layout,IjkVideoView的高度必须要是>0否则不会创建view就不会创建MediaPalyer对象了。我建议不居中的IjkVideoView高度和宽度填充处理,之后IjkVideoView再根据视频的宽高按比例设置达到自适应的效果。如果想在video上实现滑动控制音量和亮度,我建议再加一层布局就可以自定义它实现手势滑动功能;
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.yangqiang.ijkplayertest.media.IjkVideoView
android:layout_gravity="center"
android:visibility="visible"
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<SeekBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="20dp" />
<RelativeLayout
android:layout_centerVertical="true"
android:layout_below="@+id/video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginLeft="30dp"
android:text="start"
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_toRightOf="@id/start"
android:layout_marginLeft="10dp"
android:text="pause"
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<LinearLayout
android:layout_gravity="center_vertical"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="音量调节"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/volumeprogress"
android:layout_width="match_parent"
android:layout_height="20dp" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_vertical"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="亮度调节"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/lightrogress"
android:layout_width="match_parent"
android:layout_height="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
以屏幕宽为标准来动态设置view的宽高:
IMediaPlayer.OnVideoSizeChangedListener mSizeChangedListener =
new IMediaPlayer.OnVideoSizeChangedListener() {
public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sarNum, int sarDen) {
mVideoWidth = mp.getVideoWidth();
mVideoHeight = mp.getVideoHeight();
Log.e("hhhhhh","h:"+mVideoWidth);
mVideoSarNum = mp.getVideoSarNum();
mVideoSarDen = mp.getVideoSarDen();
if (mVideoWidth != 0 && mVideoHeight != 0) {
if (mRenderView != null) {
//这两句没什么用,是针对sufaceview的
mRenderView.setVideoSize(mVideoWidth, mVideoHeight);
mRenderView.setVideoSampleAspectRatio(mVideoSarNum, mVideoSarDen);
//根据video的大小进行自适应设置
double frameLayoutHeight = 0;
if(screenWidth>mVideoWidth){
double s = (double)screenWidth/(double)mVideoWidth;
frameLayoutHeight = s*mVideoHeight;
Log.e("hhhhhhscreenWidth","h:"+frameLayoutHeight);
}else{
double s = (double)mVideoWidth/(double)screenWidth;
frameLayoutHeight = s*mVideoHeight;
Log.e("hhhhhhscreenWidth","h:"+frameLayoutHeight);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,(int)frameLayoutHeight);
setLayoutParams(layoutParams);
}
requestLayout();
}
}
};
播放和暂停功能:
//开始播放
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!mVideoView.isPlaying()){
Log.e("start","调用了start...");
mVideoView.start();
}
if(!isPFlag){
videoLength = mVideoView.getDuration();
progress.setMax(videoLength);
Log.e("length","pp:"+videoLength);
seekThread.start();
isPFlag = true;
progress.setEnabled(true);
}
}
});
//暂停播放
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mVideoView.isPlaying()){
Log.e("pause","pause...");
mVideoView.pause();
}
}
});
滑动seekbar更新视频进度,如何你自己做的时候发现seekTo方法报控制空指针错误,你需要if(mHudViewHolder!=null){
mHudViewHolder.updateSeekCost(ti);}因为mHudViewHolder是针对surfaceview的:
//进度条播放监听
progress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
seekPosition = i;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//停止滑动的时候才更新视频进度,提高用户体验
if(mVideoView!=null&&mVideoView.isPlaying()){
if(seekPosition>videoLength||seekPosition<0){
return;
}
mVideoView.seekTo(seekPosition);
}
}
});
private IMediaPlayer.OnSeekCompleteListener mSeekCompleteListener = new IMediaPlayer.OnSeekCompleteListener() {
@Override
public void onSeekComplete(IMediaPlayer mp) {
mSeekEndTime = System.currentTimeMillis();
long ti = mSeekEndTime - mSeekStartTime;
Log.e("titititi","tititi:"+ti);
if(mHudViewHolder!=null){
mHudViewHolder.updateSeekCost(ti);
}
}
};
播放进度条线程:
//播放进度条线程
public class SeekThread implements Runnable{
@Override
public void run() {
while(true){
if(mVideoView!=null&&mVideoView.isPlaying()){
progress.setProgress(mVideoView.getCurrentPosition());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
横屏时候全屏播放:
//横竖屏监听
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {//切换为横屏
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mVideoView.getLayoutParams();
//这地方一定要把屏幕宽度给lp的高度
lp.height = screenWidth;
lp.width = screenHeight;
mVideoView.setLayoutParams(lp);
}else{
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mVideoView.getLayoutParams();
lp.height = screenWidth * 9 / 16;
lp.width = screenWidth;
mVideoView.setLayoutParams(lp);
}
}
调整音量和亮度:
//调节声音和亮度相关
public void intVolumeAndLightService(){
volumeprogress = (SeekBar) findViewById(R.id.volumeprogress);
volumeprogress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
volumeSlide(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
lightrogress = (SeekBar) findViewById(R.id.lightrogress);
lightrogress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
BrightnessUtils.changeAppBrightness(MainActivity.this,i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//设置最大值
volumeprogress.setMax(mMaxVolume);
//设置当前音量
volumeprogress.setProgress(mVolume);
//设置当前亮度最大值
lightrogress.setMax(255);
//设置当前亮度
int mLight = BrightnessUtils.getSystemBrightness(this);
lightrogress.setProgress(mLight);
}
//// 变更声音
private void volumeSlide(int current) {
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, current, 0);
}
在activity销毁时释放资源:
@Override
protected void onDestroy() {
super.onDestroy();
mVideoView.stopPlayback();
}
这样一些基本操作就可以满足了,有些代码没有优化,但逻辑还是清晰的,IjkVideoView里边的代码看似多实际上好多都会针对surfaceview的,忽略就好;
没积分的给我发邮件965949332@qq.com我给你发过去。Demo下载地址
来源:CSDN
作者:huidawoxiaozi
链接:https://blog.csdn.net/u012911421/article/details/88970933