RecyclerView 在触发 activity onStop 时释放 MediaPlayer
RecyclerView release MediaPlayer when activity onStop is triggered
我正在使用 TextView 和 ImageButton 视图创建一个简单的应用程序,我正在使用 RecyclerView 来显示这些对的列表,所以我写了我的自定义适配器和视图持有者。但是我有一个问题,当用户转到主屏幕时如何释放 MediaPlayer。因为我在适配器内部初始化 MediaPlayer 并且我不知道如何在 activity 停止时触发释放。这是我的代码:
Activity class:
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_list);
List<Word> words = new ArrayList<>();
words.add(new Word("one", R.raw.number_one));
words.add(new Word("two", R.raw.number_two));
words.add(new Word("three", R.raw.number_three));
words.add(new Word("four", R.raw.number_four));
words.add(new Word("five", R.raw.number_five));
LinearLayoutManager numbersListLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
RecyclerView listView = findViewById(R.id.word_list);
listView.setLayoutManager(numbersListLayoutManager);
listView.setAdapter(new WordAdapter(this, words));
}
}
适配器class:
public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {
private Context context;
private List<Word> words;
private MediaPlayer mediaPlayer;
public WordAdapter(Context context, List<Word> words) {
this.context = context;
this.words = words;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View viewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(viewItem);
}
@Override
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
stopPlaying();
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.englishTranslation.setText(this.words.get(position).getDefaultTranslation());
holder.itemView.setOnClickListener((view) -> {
stopPlaying();
mediaPlayer = MediaPlayer.create(this.context, this.words.get(position).getAudioResourceId());
mediaPlayer.setOnCompletionListener((mp) -> {
stopPlaying();
});
mediaPlayer.start();
});
}
@Override
public int getItemCount() {
return words.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView englishTranslation;
ImageButton playButton;
public ViewHolder(View wordView) {
super(wordView);
this.englishTranslation = wordView.findViewById(R.id.english_text_view);
this.playButton = wordView.findViewById(R.id.play_button);
}
}
private void stopPlaying() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
}
我应该将 MediaPlayer 对象传递给适配器并从 activity class 控制它的状态吗?或者是否可以从适配器内部进行?
@Override
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
stopPlaying();
}
您在这里设置停止播放器。
当ViewHolder
在屏幕上不可见时触发
您应该在 Activity 中包含您的播放器实例,并通过来自适配器的接口回调(侦听器模式)控制它。
此外,使用单个媒体播放器而不是内置多个媒体播放器性能会更好VH
回调应如下所示:
interface PlayableItemCallback {
void onItemVisible(int position);
void onPlay(int position);
void onStop(int position);
}
在您的 activity 中实现它并使用它来控制您的 MediaPlayer 并将其传递给适配器。 Activity代码:
public class NumbersActivity extends AppCompatActivity implements PlayableItemCallback {
MediaPlayer mediaPlayer;
Adapter adapter = Adapter(this)
// .............
@Override
public void onItemVisible(int position) {
// do stuff with player
}
@Override
public void onPlay(int position) {
// do stuff with player
}
@Override
public void onStop(int position) {
// do stuff with player
}
}
您可以使用stopPlaying
方法停止媒体播放器。要使其工作,您必须创建适配器的实例,当 onStop()
调用时,只需从适配器调用方法即可!
创建适配器实例
private WordAdapter mWordAdapter;
mWordAdapter = new WordAdapter(this, words)
listView.setAdapter(mWordAdapter)
覆盖 activity
中的 onStop 方法
@Override
protected void onStop() {
super.onStop();
if(mWordAdapter!=null){
mWordAdapter.stopPlaying()
}
}
创建 stopPlaying
方法 public 以便您可以通过对象访问它。
public void stopPlaying() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
我正在使用 TextView 和 ImageButton 视图创建一个简单的应用程序,我正在使用 RecyclerView 来显示这些对的列表,所以我写了我的自定义适配器和视图持有者。但是我有一个问题,当用户转到主屏幕时如何释放 MediaPlayer。因为我在适配器内部初始化 MediaPlayer 并且我不知道如何在 activity 停止时触发释放。这是我的代码:
Activity class:
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_list);
List<Word> words = new ArrayList<>();
words.add(new Word("one", R.raw.number_one));
words.add(new Word("two", R.raw.number_two));
words.add(new Word("three", R.raw.number_three));
words.add(new Word("four", R.raw.number_four));
words.add(new Word("five", R.raw.number_five));
LinearLayoutManager numbersListLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
RecyclerView listView = findViewById(R.id.word_list);
listView.setLayoutManager(numbersListLayoutManager);
listView.setAdapter(new WordAdapter(this, words));
}
}
适配器class:
public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {
private Context context;
private List<Word> words;
private MediaPlayer mediaPlayer;
public WordAdapter(Context context, List<Word> words) {
this.context = context;
this.words = words;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View viewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(viewItem);
}
@Override
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
stopPlaying();
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.englishTranslation.setText(this.words.get(position).getDefaultTranslation());
holder.itemView.setOnClickListener((view) -> {
stopPlaying();
mediaPlayer = MediaPlayer.create(this.context, this.words.get(position).getAudioResourceId());
mediaPlayer.setOnCompletionListener((mp) -> {
stopPlaying();
});
mediaPlayer.start();
});
}
@Override
public int getItemCount() {
return words.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView englishTranslation;
ImageButton playButton;
public ViewHolder(View wordView) {
super(wordView);
this.englishTranslation = wordView.findViewById(R.id.english_text_view);
this.playButton = wordView.findViewById(R.id.play_button);
}
}
private void stopPlaying() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
}
我应该将 MediaPlayer 对象传递给适配器并从 activity class 控制它的状态吗?或者是否可以从适配器内部进行?
@Override
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
stopPlaying();
}
您在这里设置停止播放器。
当ViewHolder
在屏幕上不可见时触发
您应该在 Activity 中包含您的播放器实例,并通过来自适配器的接口回调(侦听器模式)控制它。
此外,使用单个媒体播放器而不是内置多个媒体播放器性能会更好VH
回调应如下所示:
interface PlayableItemCallback {
void onItemVisible(int position);
void onPlay(int position);
void onStop(int position);
}
在您的 activity 中实现它并使用它来控制您的 MediaPlayer 并将其传递给适配器。 Activity代码:
public class NumbersActivity extends AppCompatActivity implements PlayableItemCallback {
MediaPlayer mediaPlayer;
Adapter adapter = Adapter(this)
// .............
@Override
public void onItemVisible(int position) {
// do stuff with player
}
@Override
public void onPlay(int position) {
// do stuff with player
}
@Override
public void onStop(int position) {
// do stuff with player
}
}
您可以使用stopPlaying
方法停止媒体播放器。要使其工作,您必须创建适配器的实例,当 onStop()
调用时,只需从适配器调用方法即可!
创建适配器实例
private WordAdapter mWordAdapter;
mWordAdapter = new WordAdapter(this, words)
listView.setAdapter(mWordAdapter)
覆盖 activity
中的 onStop 方法 @Override
protected void onStop() {
super.onStop();
if(mWordAdapter!=null){
mWordAdapter.stopPlaying()
}
}
创建 stopPlaying
方法 public 以便您可以通过对象访问它。
public void stopPlaying() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}