我在RecyclerView中使用SimpleExoPlayer,有时在调用时出现错误
simplePlayer.setPlayWhenReady(true);
视频播放器保持空白,出现问题的时间并不总是相同(我没有找到确切的模拟方法)
异常(exception):
java.lang.IllegalStateException: Handler (android.os.Handler) {edbfbdd} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:545)
at android.os.Handler.enqueueMessage(Handler.java:662)
at android.os.Handler.sendMessageAtTime(Handler.java:631)
at android.os.Handler.sendMessageDelayed(Handler.java:601)
at android.os.Handler.sendMessage(Handler.java:538)
at android.os.Message.sendToTarget(Message.java:418)
at com.google.android.exoplayer2.ExoPlayerImplInternal.setPlayWhenReady(ExoPlayerImplInternal.java:183)
at com.google.android.exoplayer2.ExoPlayerImpl.setPlayWhenReady(ExoPlayerImpl.java:245)
at com.google.android.exoplayer2.SimpleExoPlayer.updatePlayWhenReady(SimpleExoPlayer.java:1188)
at com.google.android.exoplayer2.SimpleExoPlayer.setPlayWhenReady(SimpleExoPlayer.java:896)
我的代码在自定义 View 中运行(在回收者 View 适配器中更新):
MyView h = this;
h.playerView = new PlayerView(getContext());
h.playerView.setVisibility(View.GONE);
h.postFrameLayout.addView(h.playerView, 0);
h.postFrameLayout.getLayoutParams().width = swidth;
h.postFrameLayout.getLayoutParams().height = (int) (swidth );
h.playerView.getLayoutParams().width = swidth;
h.playerView.getLayoutParams().height = (int) (swidth );
if (player != null){
player.release();
}
DefaultLoadControl loadControl = new DefaultLoadControl.Builder()
.setBufferDurationsMs(1100, 2000, 1000, 1000).createDefaultLoadControl();
final SimpleExoPlayer simplePlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
h.playerView.setUseController(false);
h.playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH);
h.playerView.setPlayer(simplePlayer);
simplePlayer.addListener(new Player.EventListener() {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == Player.STATE_ENDED) {
h.videoPlay.setVisibility(View.VISIBLE);
h.postImage.setVisibility(View.VISIBLE);
simplePlayer.setPlayWhenReady(false);
simplePlayer.seekTo(0);
}
}
});
Uri uri = Uri.parse(videoUrl);
final MediaSource audioSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
simplePlayer.prepare(audioSource);
h.player = simplePlayer;
h.videoPlay.setVisibility(View.VISIBLE);
h.videoPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayArticles(false, post.getPhotoItems(), h);
h.videoPlay.setVisibility(View.GONE);
h.postImage.setVisibility(View.GONE);
h.playerView.setVisibility(View.VISIBLE);
simplePlayer.setPlayWhenReady(true);
}
});
我从窗口分离时释放播放器(当回收者 View 在我的自定义 View 上调用onDetachedFromWindow()时):
if (player != null) {
videoPlay.setVisibility(View.VISIBLE);
postImage.setVisibility(View.VISIBLE);
player.release();
player = null;
}
启动播放器时是否有办法避免此问题或重试?
最佳答案
当我尝试使用 ..exoplayer2.ui.PlayerView 类时,我遇到了同样的问题。 ..exoplayer2.ui.PlayerControlView 没有问题。
这是我的代码:
public static void setUp(Context context){
ExoPlayerTool.context = context;
player = new SimpleExoPlayer.Builder(context).build();
dataSourceFactory = new DefaultDataSourceFactory(context,
Util.getUserAgent(context, "SingerApp"));
}
public static void setSource(String link){
Uri uri = Uri.parse(link);
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
player.addListener(playerListener);
player.prepare(videoSource);
player.setPlayWhenReady(true);
}
我一次调用
setup()
函数,每次我想更改源代码时都调用setSource()
函数。当我想关闭音乐时,我打电话给
player.release();
,并在释放setSource()
之后,在player.prepare(videoSource);
行上给出相同的错误。通过在每次源更改中创建新的 SimpleExoPlayer ,我能够解决此问题。
代码看起来像这样:
public static void setSource(String link){
Uri uri = Uri.parse(link);
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
player.release();
player = new SimpleExoPlayer.Builder(context).build();
player.addListener(playerListener);
player.prepare(videoSource);
player.setPlayWhenReady(true);
}
我不知道该解决方案的优化程度,但总比不发布播放器好。