android:ijkplayer播放器的集成及简单自定义播放器

关于ijkplayer

最近公司准备要开发的项目里涉及到播放器的功能模块,于是对Android播放器框架进行了预研。目前ijkplayer框架是为大多数公司及开发者青睐的框架。 ijkplayer是一个基于FFmpeg的轻量级Android/iOS视频播放器。FFmpeg的是全球领先的多媒体框架,能够解码,编码, 转码,复用,解复用,流,过滤器和播放大部分的视频格式。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。

环境配置

项目中引入ijkplayer环境有两种方式:

1.在Gradle中引入

# required
allprojects {
    repositories {
        jcenter()
    }
}

dependencies {
    # required, enough for most devices.
    compile 'tv.danmaku.ijk.media:ijkplayer-java:0.6.1'
    compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.6.1'

    # Other ABIs: optional
    compile 'tv.danmaku.ijk.media:ijkplayer-armv5:0.6.1'
    compile 'tv.danmaku.ijk.media:ijkplayer-arm64:0.6.1'
    compile 'tv.danmaku.ijk.media:ijkplayer-x86:0.6.1'
    compile 'tv.danmaku.ijk.media:ijkplayer-x86_64:0.6.1'

    # ExoPlayer as IMediaPlayer: optional, experimental
    compile 'tv.danmaku.ijk.media:ijkplayer-exo:0.6.1'
}

2.在Ubuntu下编译源码得到

Ubuntu需要安装homebrew, Git, yasm

# install homebrew, git, yasm
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install git
brew install yasm

# add these lines to your ~/.bash_profile or ~/.profile
# export ANDROID_SDK=
# export ANDROID_NDK=

# on Cygwin (unmaintained)
# install git, make, yasm

开始编译

git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-android
cd ijkplayer-android
git checkout -B latest k0.6.1

./init-android.sh

cd android/contrib
./compile-ffmpeg.sh clean
./compile-ffmpeg.sh all

cd ..
./compile-ijk.sh all

# Android Studio:
#     Open an existing Android Studio project
#     Select android/ijkplayer/ and import
#
#     define ext block in your root build.gradle
#     ext {
#       compileSdkVersion = 23       // depending on your sdk version
#       buildToolsVersion = "23.0.0" // depending on your build tools version
#
#       targetSdkVersion = 23        // depending on your sdk version
#     }
#
# Eclipse: (obselete)
#     File -> New -> Project -> Android Project from Existing Code
#     Select android/ and import all project
#     Import appcompat-v7
#     Import preference-v7
#
# Gradle
#     cd ijkplayer
#     gradle

录结构
在这里插入图片描述

播放器使用

可能是网络的问题,使用Gradle导入会花费很长时间,如果遇到超时,还得重头来一遍,太费时间了。后来我就直接在Ubuntu下编译后,在android Studio下导入该项目。我先介绍下Demo中利用ijkplayer播放视频的过程。

1.初始化播放器

IjkMediaPlayer.loadLibrariesOnce(null);
IjkMediaPlayer.native_profileBegin("libijkplayer.so");

2.初始化IjkVideoView

//这里使用的是Demo中提供的AndroidMediaController类控制播放相关操作
mMediaController = new AndroidMediaController(this, false);
mMediaController.setSupportActionBar(actionBar);
mVideoView = (IjkVideoView) findViewById(R.id.video_view);
mVideoView.setMediaController(mMediaController);

3.设置本地视频文件位置或服务器地址,然后播放

mVideoView.setVideoPath(mVideoPath);
mVideoView.start();

4.Activity销毁时,需要释放资源

@Override
public void onBackPressed() {
    mBackPressed = true;
    super.onBackPressed();
}
@Override
protected void onStop() {
    super.onStop();
    //点击返回或不允许后台播放时 释放资源
    if (mBackPressed || !mVideoView.isBackgroundPlayEnabled()) {
        mVideoView.stopPlayback();
        mVideoView.release(true);
        mVideoView.stopBackgroundPlay();
    } else {
        mVideoView.enterBackground();
    }
    IjkMediaPlayer.native_profileEnd();
}

自定义播放器

当然官方提供的Demo只是演示视频播放的基本操作,对于视频播放的控制、全屏等操作,还要自己动手做。

1.部分声明

private static final int SIZE_DEFAULT = 0;
private static final int SIZE_4_3 = 1;
private static final int SIZE_16_9 = 2;
private int currentSize = SIZE_16_9;
private IjkVideoView video;
private SeekBar seekBar;

2.视频播放比例

这里需要修改IjkVideoView部分代码后,才支持按比例播放

//修改相关代码
private static final int[] s_allAspectRatio = {
    IRenderView.AR_ASPECT_FIT_PARENT,
    IRenderView.AR_ASPECT_FILL_PARENT,
    IRenderView.AR_ASPECT_WRAP_CONTENT,
    IRenderView.AR_MATCH_PARENT,
    IRenderView.AR_16_9_FIT_PARENT,
    IRenderView.AR_4_3_FIT_PARENT
};
private int mCurrentAspectRatioIndex = 3;//0
private int mCurrentAspectRatio = s_allAspectRatio[3];//0
private int mCurrentRender = RENDER_TEXTURE_VIEW;
//增加下面方法
public IRenderView getmRenderView() {
    return mRenderView;
}
public int getmVideoWidth() {
   return mVideoWidth;
}
public int getmVideoHeight() {
   return mVideoHeight;
}

置视频播放比例
设置屏幕播放比例,当处于横屏时,屏幕宽度较大、高度较小,所以设置高度固定,而宽度按照比例缩放;当处于竖屏时,屏幕高度较大、宽度较小,所以设置宽度固定,而高度按照比例缩放:

public void setScreenRate(int rate) {
    int width = 0;
    int height = 0;
    if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {// 横屏
        if (rate == SIZE_DEFAULT) {
            width = video.getmVideoWidth();
            height = video.getmVideoHeight();
        } else if (rate == SIZE_4_3) {
            width = screenHeight / 3 * 4;
            height = screenHeight;
        } else if (rate == SIZE_16_9) {
            width = screenHeight / 9 * 16;
            height = screenHeight;
        }
    } else { //竖屏
        if (rate == SIZE_DEFAULT) {
            width = video.getmVideoWidth();
            height = video.getmVideoHeight();
        } else if (rate == SIZE_4_3) {
            width = screenWidth;
            height = screenWidth * 3 / 4;
        } else if (rate == SIZE_16_9) {
            width = screenWidth;
            height = screenWidth * 9 / 16;
        }
    }
    if (width > 0 && height > 0) {
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) video.getmRenderView().getView().getLayoutParams();
        lp.width = width;
        lp.height = height;
        video.getmRenderView().getView().setLayoutParams(lp);
    }
}

3.屏幕方向切换

private void fullChangeScreen() {
    if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {// 切换为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

4.全屏播放

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //重新获取屏幕宽高
    initScreenInfo();
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {//切换为横屏
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) video.getLayoutParams();
        lp.height = screenHeight;
        lp.width = screenWidth;
        video.setLayoutParams(lp);
    } else {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) video.getLayoutParams();
        lp.height = screenWidth * 9 / 16;
        lp.width = screenWidth;
        video.setLayoutParams(lp);
    }
    setScreenRate(currentSize);
}

5.播放进度

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    ....
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
         video.seekTo(seekBar.getProgress()*video.getDuration()/100);
         ...
    }
});
//视频开始播放时使用handle.sendMessageDelayed更新时间显示
private void refreshTime(){
    int totalSeconds = video.getCurrentPosition() / 1000;
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    String ti=hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds):String.format("%02d:%02d", minutes, seconds);
    time.setText(ti);
}

原文转载地址:https://www.cnblogs.com/zhujiabin/p/7211983.html

你可能感兴趣的:(Android)