1.認識ijkplayer
最近公司準備開發(fā)一款視頻播放及直播的應用纺非,找了許多開源的框架蒂培,大部分都是基于ffmpeg開發(fā)的贝润。最開始準備用Vitamio框架開發(fā)的,相關的文章也比較豐富掂墓,結果對于非個人移動應用均需購買Vitamio使用授權谦纱。不過B站開源的ijkplayer也不錯,而且也不需要商業(yè)授權君编。
ijkplayer是一個基于FFmpeg的輕量級Android/iOS視頻播放器跨嘉。FFmpeg的是全球領先的多媒體框架,能夠解碼啦粹,編碼偿荷, 轉碼,復用唠椭,解復用跳纳,流,過濾器和播放大部分的視頻格式贪嫂。它提供了錄制寺庄、轉換以及流化音視頻的完整解決方案。它包含了非常先進的音頻/視頻編解碼庫libavcodec力崇,為了保證高可移植性和編解碼質(zhì)量斗塘,libavcodec里很多code都是從頭開發(fā)的。
2.環(huán)境配置
項目中引入ijkplayer環(huán)境有兩種方式亮靴。
2.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.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=<your sdk path>
# export ANDROID_NDK=<your ndk path>
# 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
目錄結構
3.播放器使用
可能是網(wǎng)絡的問題馍盟,使用Gradle導入會花費很長時間,如果遇到超時茧吊,還得重頭來一遍贞岭,太費時間了八毯。后來我就直接在Ubuntu下編譯后,在Android Studio下導入該項目瞄桨。我先介紹下Demo中利用ijkplayer播放視頻的過程话速。
3.1初始化播放器
IjkMediaPlayer.loadLibrariesOnce(null);
IjkMediaPlayer.native_profileBegin("libijkplayer.so");
3.2初始化IjkVideoView
//這里使用的是Demo中提供的AndroidMediaController類控制播放相關操作
mMediaController = new AndroidMediaController(this, false);
mMediaController.setSupportActionBar(actionBar);
mVideoView = (IjkVideoView) findViewById(R.id.video_view);
mVideoView.setMediaController(mMediaController);
3.3設置本地視頻文件位置或服務器地址,然后播放
mVideoView.setVideoPath(mVideoPath);
mVideoView.start();
3.4Activity銷毀時芯侥,需要釋放資源
@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();
}
4.自定義播放器
當然官方提供的Demo只是演示視頻播放的基本操作泊交,對于視頻播放的控制、全屏等操作柱查,還要自己動手做廓俭。
4.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;
4.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);
}
}
4.3屏幕方向切換
private void fullChangeScreen() {
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {// 切換為豎屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
4.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);
}
4.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);
}
5.相關資料
- 官網(wǎng)地址:https://github.com/Bilibili/ijkplayer
- 已編譯好的環(huán)境:http://download.csdn.net/detail/u010987039/9611675
- 測試用服務器地址:
http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8(可用)
http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8
http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8
http://playertest.longtailvideo.com/adaptive/oceans_aes/oceans_aes.m3u8 (AES encrypted)
http://playertest.longtailvideo.com/adaptive/captions/playlist.m3u8 (HLS stream with CEA-608 captions)
http://playertest.longtailvideo.com/adaptive/wowzaid3/playlist.m3u8 (with metadata)
http://content.jwplatform.com/manifests/vM7nH0Kl.m3u8
http://cdn-fms.rbs.com.br/hls-vod/sample1_1500kbps.f4v.m3u8
http://cdn-fms.rbs.com.br/vod/hls_sample1_manifest.m3u8(可用)
http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/appleman.m3u8 (LIVE TV可用)
http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch2/appleman.m3u8 (LIVE TV)
http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch3/appleman.m3u8 (LIVE TV)
https://dl.dropboxusercontent.com/u/7303267/website/m3u8/index.m3u8 (VOD) - [updated]
http://content.jwplatform.com/manifests/vM7nH0Kl.m3u8 (link protection, video not encrypted)
http://sample.vodobox.net/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8 (4K)
http://cdn3.viblast.com/streams/hls/airshow/playlist.m3u8 (4K)
6.更新
方便大家使用物赶,提供編譯好的各平臺so文件白指,再引入“ijkplayer-java”就可以直接使用。
http://download.csdn.net/detail/u010987039/9800324
百度網(wǎng)盤下載地址:https://pan.baidu.com/s/1eSst88U
QQ:631344199