所以我有一个安卓应用程序,它读取一些存储在Firebase实时数据库中的Firebase存储下载URLs,我还使用一个RecyclerView来渲染一些ExoPlayers来播放这些视频。我还使用了一个RecyclerView来渲染一些播放这些视频的ExoPlayers。问题是,如果我滚动的速度非常快,就没有足够的时间来渲染所有的东西,程序就会抛出一个异常。 我想我有太多的ExoPlayers被保留,而我需要在前进的过程中删除它们。我该怎么做呢?
E/ExoPlayerImplInternal: Renderer error: index=0, type=video, format=Format(1, null, null, video/avc, null, -1, null, [640, 480, 29.997072], [-1, -1]), rendererSupport=YES
com.google.android.exoplayer2.ExoPlaybackException: com.google.android.exoplayer2.mediacodec.MediaCodecRenderer$DecoderInitializationException: Decoder init failed: OMX.Exynos.avc.dec, Format(1, null, null, video/avc, null, -1, null, [640, 480, 29.997072], [-1, -1])
at com.google.android.exoplayer2.BaseRenderer.createRendererException(BaseRenderer.java:359)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.maybeInitCodec(MediaCodecRenderer.java:563)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.onInputFormatChanged(MediaCodecRenderer.java:1254)
at com.google.android.exoplayer2.video.MediaCodecVideoRenderer.onInputFormatChanged(MediaCodecVideoRenderer.java:756)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.readToFlagsOnlyBuffer(MediaCodecRenderer.java:814)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:712)
at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:599)
at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:329)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:237)
at android.os.HandlerThread.run(HandlerThread.java:67)
这是RecyclerView的代码。
FirebaseRecyclerOptions<MediaObject> options =
new FirebaseRecyclerOptions.Builder<MediaObject>()
.setQuery(databaseReference.orderByChild("title"), MediaObject.class)
.build();
FirebaseRecyclerAdapter<MediaObject, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<MediaObject, ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ViewHolder viewHolder, int i, @NonNull MediaObject mediaObject) {
viewHolder.setVideo(getApplication(), mediaObject.getTitle().replace(".mp4", ""), mediaObject.getMedia_url());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_view_extended_videolist, parent, false);
return new ViewHolder(view);
}
};
firebaseRecyclerAdapter.startListening();
recycleView.setAdapter(firebaseRecyclerAdapter);
ViewHolder代码
public class ViewHolder extends RecyclerView.ViewHolder {
private View mView;
private SimpleExoPlayer exoPlayer;
private PlayerView mExoPlayerView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setVideo(final Application ctx, String title, String url) {
TextView mTextView = mView.findViewById(R.id.titleTv);
mExoPlayerView = mView.findViewById(R.id.exoplayer_view);
mTextView.setText(title);
try {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(ctx).build();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
exoPlayer = ExoPlayerFactory.newSimpleInstance(ctx);
Uri video = Uri.parse(url);
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("2020-04-12");
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(video, dataSourceFactory, extractorsFactory, null, null);
mExoPlayerView.setPlayer(exoPlayer);
exoPlayer.prepare(mediaSource);
exoPlayer.setPlayWhenReady(false);
}catch (Exception e){
Log.d("ViewHolder", "exoplayer error " + e.toString());
}
}
}
我最后只用了exoPlayer.release()