英文:
Can I pause / stop ExoPlayer from a different Activity / Fragment even though I initialize it in my Adapter class?
问题
以下是翻译好的内容:
我想知道的是,如果我已经在我的 Adapter 类中为 ExoPlayer 和 PlayerView 编写了代码,因为我需要这个 holder,因为我有多个视图类型(图片和视频)。我想知道是否可能在另一个 Activity 或 Fragment 中暂停播放,并且从另一个 Activity 或 Fragment 恢复播放,或者是否必须直接在 Adapter 类中进行操作?
我需要的是,如果 ExoPlayer 在播放视频时用户从屏幕切换到另一个 Activity / Fragment,或者完全关闭应用程序,播放器能够自动暂停。
我有哪些选项?
public class ViewHolder extends RecyclerView.ViewHolder {
PlayerView mPlayerView;
SimpleExoPlayer mExoPlayer;
ViewHolder(@NonNull View itemView) {
super(itemView);
mPlayerView = itemView.findViewById(R.id.exo_player);
mRelativeLayout = itemView.findViewById(R.id.relative_layout_one);
mLocation.setOnClickListener(v -> {
Intent intent = new Intent(mContext, MapsActivityUser.class);
intent.putExtra("postid", mPostList.get(getAdapterPosition()).getPostid());
intent.putExtra("text_event", mPostList.get(getAdapterPosition()).getText_event());
intent.putExtra("text_location", mPostList.get(getAdapterPosition()).getText_location());
intent.putExtra("text_date_time", mPostList.get(getAdapterPosition()).getText_date_time());
mContext.startActivity(intent);
});
}
public void setVideo(String videoUrl) {
try {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(mContext).build();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
mExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
Uri videoUri = Uri.parse(videoUrl);
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("posts");
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoUri, dataSourceFactory, extractorsFactory, null, null);
mPlayerView.setPlayer(mExoPlayer);
mExoPlayer.prepare(mediaSource);
mExoPlayer.setPlayWhenReady(false);
} catch (Exception e) {
Log.e("ViewHolder", "ExoPlayer error: " + e.toString());
}
}
}
如果您需要进一步的帮助,请随时提问。
英文:
So what I would like to know is if I have written the code for the ExoPlayer
and PlayerView
in my Adapter
class, because I need the holder because I have multiple view types (images and videos), and so I was wondering is it possible to pause it and resume it from another Activity or Fragment, or do I have to do it directly in the Adapter class?
What I need is for the Player to pause automatically when the user navigates away from the screen to another Activity / Fragment
if the ExoPlayer is in the middle of playing a video, or when they close the app entirely.
What options do I have?
public class ViewHolder extends RecyclerView.ViewHolder {
PlayerView mPlayerView;
SimpleExoPlayer mExoPlayer;
ViewHolder(@NonNull View itemView) {
super(itemView);
mPlayerView = itemView.findViewById(R.id.exo_player);
mRelativeLayout = itemView.findViewById(R.id.relative_layout_one);
mLocation.setOnClickListener(v -> {
Intent intent = new Intent(mContext, MapsActivityUser.class);
intent.putExtra("postid", mPostList.get(getAdapterPosition()).getPostid());
intent.putExtra("text_event", mPostList.get(getAdapterPosition()).getText_event());
intent.putExtra("text_location", mPostList.get(getAdapterPosition()).getText_location());
intent.putExtra("text_date_time", mPostList.get(getAdapterPosition()).getText_date_time());
mContext.startActivity(intent);
});
}
public void setVideo(String videoUrl) {
try {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(mContext).build();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
mExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
Uri videoUri = Uri.parse(videoUrl);
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("posts");
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoUri, dataSourceFactory, extractorsFactory, null, null);
mPlayerView.setPlayer(mExoPlayer);
mExoPlayer.prepare(mediaSource);
mExoPlayer.setPlayWhenReady(false);
} catch (Exception e) {
Log.e("ViewHolder", "ExoPlayer error: " + e.toString());
}
}
}
答案1
得分: 0
你可以尝试将你的 ExoPlayer 放入一个 service 中,这样你就可以从任何地方对其执行任何操作。
如果你希望你的服务保持持续运行,可以在 onStartCommand 中返回 this。
英文:
You can try to put your ExoPlayer in a service so you can do any actions on it from anywhere.
If you wanna your service to stick, return this on onStartCommand
专注分享java语言的经验与见解,让所有开发者获益!
评论