自從上次做完視頻播放器調(diào)研以后,心里就知道喉前,肯定以后這塊東西都是我做没酣,果不其然,公司對(duì)視頻播放這塊不斷的優(yōu)化卵迂。我就悲催的無(wú)限填坑裕便,話說(shuō)英語(yǔ)差,看國(guó)外文檔真的很吃力见咒。
簡(jiǎn)單講一下項(xiàng)目中遇到的問(wèn)題偿衰。
- 創(chuàng)建和基本使用
這個(gè)不多講,最簡(jiǎn)單使用就是布局里
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:surface_type="texture_view"
app:use_controller="false" />
接下來(lái)創(chuàng)建軌道论颅,播放器等哎垦,如果你播放的時(shí)候發(fā)現(xiàn)有時(shí)候黑屏囱嫩。這是可能你的VideoCache不是單例模式恃疯。 下面是我自己寫(xiě)的Manger和VideoCache
public class ExoPlayerManger {
private static final String TAG = "ExoPlayerManger";
private Context mContext;
private BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// 創(chuàng)建軌道選擇工廠
private TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
// 創(chuàng)建軌道選擇器實(shí)例
private TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
private SimpleExoPlayer simpleExoPlayer;
private DataSource.Factory dataSourceFactory;
private String mVideoUrl;
private SimpleCache simpleCache;
private Uri playVideoUri;
private ExtractorMediaSource mediaSource;
/**
* @param context 傳入context
*/
public void setBuilderContext(Context context) {
mContext = context;
dataSourceFactory = new DefaultDataSourceFactory(mContext, "seyed");
}
/**
* @param videoUrl 傳入視頻路徑
*/
public void setVideoUrl(String videoUrl) {
this.mVideoUrl = videoUrl;
simpleCache = VideoCache.getInstance(mContext);
playVideoUri = Uri.parse(mVideoUrl);
}
/**
* @return 返回exoPlayer對(duì)象
*/
public SimpleExoPlayer create() {
try {
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
dataSourceFactory = new CacheDataSourceFactory(simpleCache, dataSourceFactory);
mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(playVideoUri);
simpleExoPlayer.prepare(mediaSource);
} catch (Exception e) {
}
return simpleExoPlayer;
}
}
/**
* @author :leo on 2018/12/17 17:58
* <p>
* 方法用途 :視頻緩存單例模式
*/
public class VideoCache {
private static SimpleCache sDownloadCache;
/**
* @param context
* @return
*/
public static SimpleCache getInstance(Context context) {
if (sDownloadCache == null) {
sDownloadCache = new SimpleCache(new File(getMediaCacheFile(context), "StoryCache"), new LeastRecentlyUsedCacheEvictor(512 * 1024 *1024));
}
return sDownloadCache;
}
public static File getMediaCacheFile(Context context) {
String directoryPath = "";
String childPath = "exoPlayer";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// 外部?jī)?chǔ)存可用
directoryPath = File.separator + context.getExternalFilesDir(childPath).getAbsolutePath();
} else {
directoryPath = File.separator + context.getFilesDir().getAbsolutePath() + File.separator + childPath;
}
File file = new File(directoryPath);
//判斷文件目錄是否存在
if (!file.exists()) {
file.mkdirs();
}
return file;
}
}
緊接著 只需要
ExoPlayerManger exoPlayerManger = new ExoPlayerManger();
exoPlayerManger.setBuilderContext(mContext);
exoPlayerManger.setVideoUrl(playVideoUrl);
simpleExoPlayer = exoPlayerManger.create();
//設(shè)置音量 測(cè)試期間設(shè)置為0
simpleExoPlayer.setVolume(10);
videoView.setPlayer(simpleExoPlayer);
//賦值給顯示時(shí)間
simpleExoPlayer.addListener(this);
//開(kāi)啟播放
simpleExoPlayer.setPlayWhenReady(true);
播放器有個(gè)監(jiān)聽(tīng) Player.EventListener ,這就講幾個(gè)狀態(tài)
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (isFullScreen) {
fullScreenDialog.setPlayState(playbackState);
}
switch (playbackState) {
//緩沖狀態(tài)
case 2:
break;
//播放狀態(tài)
case 3:
break;
//播放完成
case 4:
default:
break;
}
- 如何播放raw下文件
RawResourceDataSource.buildRawResourceUri(R.raw.login_bg_video);
ExoPlayerManger exoPlayerManger = new ExoPlayerManger();
exoPlayerManger.setBuilderContext(getContext());
//設(shè)置從raw下讀取的文件路徑
exoPlayerManger.setVideoUrl(RawResourceDataSource.buildRawResourceUri(R.raw.login_bg_video).toString());
simpleExoPlayer = exoPlayerManger.create();
simpleExoPlayer.setVolume(0);
simpleExoPlayer.setRepeatMode(1);
playerView.setPlayer(simpleExoPlayer);
// simpleExoPlayer.prepare(audioSource);
simpleExoPlayer.setPlayWhenReady(true);
- 列表中點(diǎn)擊切換到全屏
這里有很多方法墨闲,我選擇的方法不是最好的。科學(xué)上網(wǎng)看了很多老外寫(xiě)的例子枫虏,大部分都是彈出一個(gè)Dialog懒鉴,將item中的播放PlayerView,remove出來(lái)瞻离,放到Dialog里面腾仅。然后更改PlayerView的布局大小就可以了。
但是這里會(huì)有一個(gè)卡頓問(wèn)題套利,老外同學(xué)們基本沒(méi)講推励,如果你按照直接removeView然后addView to Dialog, 我測(cè)試基本會(huì)卡1-5s鹤耍。這時(shí)候可以做一個(gè)騷操作,就是向前或者向后seekto一下验辞,可以基本做到秒開(kāi)稿黄。當(dāng)你切換窗口的時(shí)候,可以將PlayerView 還回去就可以了跌造,記得設(shè)置原來(lái)的寬高和大小杆怕。
//從當(dāng)前布局移除播放view
ViewGroup parent = (ViewGroup) videoView.getParent();
if (parent != null) {
parent.removeView(videoView);
}
//加入到Dialog
ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();
layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
videoView.setLayoutParams(layoutParams);
rlShow.addView(playerView);
其實(shí)也可以嘗試使用,simpleExoPlayer.setVideoTextureView();切換畫(huà)布壳贪,但是我在測(cè)試的時(shí)候陵珍,會(huì)有卡頓,有時(shí)間的同學(xué)可以試試违施,希望你有別的辦法可以給我留言撑教,謝謝。還有很多細(xì)節(jié)的東西醉拓,等我想起來(lái)再補(bǔ)上伟姐,連續(xù)加班,今天才有時(shí)間寫(xiě)點(diǎn)東西亿卤,就先到這里 愤兵。
最近公司大佬提出建議,說(shuō)來(lái)回切換全屏的時(shí)候有卡頓經(jīng)過(guò)一番百度谷姐排吴,終于找到解決辦法秆乳。
使用TextureView 作為ExoPlayer播放畫(huà)布。
TextureView 在切換狀態(tài)的時(shí)候會(huì)經(jīng)歷銷毀重建過(guò)程钻哩。
那么之前TextureView中播放的SurfaceTexture也會(huì)銷毀屹堰。
建議在從列表到全屏之前調(diào)用TextureView.getSurfaceTexture()
保存當(dāng)前狀態(tài),在設(shè)置完新的寬高后街氢,將保存的SurfaceTexture重新設(shè)置進(jìn)去扯键。
這里會(huì)有一個(gè)坑,就是設(shè)置進(jìn)去也會(huì)卡在那里珊肃。
在調(diào)用TextureView.getSurfaceTexture()之前荣刑,給TextureView設(shè)置setSurfaceTextureListener 并在onSurfaceTextureDestroyed方法中返回false
在onSurfaceTextureAvailable中重新調(diào)用textureView.setSurfaceTexture(mSurfaceTexture);
建議try一下,因?yàn)榭赡軙?huì)報(bào)SurfaceTexture IsReleased 異常伦乔。
簡(jiǎn)單代碼如下
windowTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
//獲取當(dāng)前緩存的幀
SurfaceTexture surfaceTexture = windowTextureView.getSurfaceTexture();
//跳轉(zhuǎn)全屏
fullScreenDialog.setFullScreenRes(windowTextureView, surfaceTexture);
//在全屏頁(yè)面中
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (mSurfaceTexture == null) {
mSurfaceTexture = surface;
}
try {
textureView.setSurfaceTexture(mSurfaceTexture);
} catch (Exception e) {
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
經(jīng)過(guò)測(cè)試基本可以實(shí)現(xiàn)無(wú)卡頓切換厉亏。