当我尝试从.mp4
中的本地文件(假定为ExoPlayer 2.10.6
)播放视频时,我得到了OutOfMemoryError
。我从firebase
数据库下载了视频,然后用Exoplayer
加载了视频。 fragment
与Exoplayer
存在两次,但我在Exoplayer
中释放了onPause
。
在这里初始化Exoplayer
,在displayVideo
中调用onResume
,随后在fragment
运行时调用:
private void displayVideo() {
releaseMediaPlayer();
exoPlayer = ExoPlayerFactory.newSimpleInstance(getcontext());
mMediaPlayerView.setPlayer(exoPlayer);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getcontext(),com.google.android.exoplayer2.util.Util.getUserAgent(getcontext(),getcontext().getString(R.string.app_name)));
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.fromFile(new File(getcontext().getExternalFilesDir(null),profilePicVideoname)));
exoPlayer.prepare(videoSource);
exoPlayer.setPlayWhenReady(true);
exoPlayer.setRepeatMode(Player.REPEAT_MODE_ONE);
mMediaPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_zoom);
}
在这里我释放Exoplayer
:
@Override
public void onPause() {
super.onPause();
releaseMediaPlayer();
}
@Override
public void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
if (exoPlayer != null) {
exoPlayer.release();
exoPlayer = null;
}
mMediaPlayerView.setPlayer(null);
}
firebase
中的视频文件为
编辑:
new File(getcontext().getExternalFilesDir(null),Helper.profilePicVideoname).length()
= 629440
错误消息非常没用:
E/art: Throwing OutOfMemoryError "Failed to allocate a 65548 byte allocation with 10220 free bytes and 9KB until OOM"
E/LoadTask: OutOfMemory error loading stream
java.lang.OutOfMemoryError: Failed to allocate a 65548 byte allocation with 19092 free bytes and 18KB until OOM
编辑2:
好的,没有视频循环,则不会抛出OutOfMemoryError
。没关系,如果我使用setRepeatMode
,LoopingMediaSource
或通过添加调用exoPlayer.prepare
的侦听器来循环播放视频。
唯一停止OutOfMemoryError
的事情是添加一个侦听器,并在每个循环中使用releaseMediaPlayer()
释放所有内容,然后重新实例化Exoplayer
,Mediasource
等上。但是,在每个循环之间都有一个黑框,因此使用CPU的方式不必要。内存消耗从180 MB急剧下降,其中发生OutOfMemory
到
为什么我不能按常规方式循环播放?我该怎么办才能消除黑框或优化CPU使用率?