gpt4 book ai didi

java - 当 Activity onStop 被触发时,RecyclerView 释放 MediaPlayer

转载 作者:行者123 更新时间:2023-12-02 00:58:10 25 4
gpt4 key购买 nike

我正在创建一个简单的应用程序,其中包含一个 TextViewImageButton View ,我使用 RecyclerView 来显示这些 View 对的列表,以便我编写了自定义适配器和 View 支架。但我有一个问题,如何在用户进入主屏幕时释放 MediaPlayer 。因为我在适配器内初始化 MediaPlayer ,并且我不知道如果 Activity 停止如何触发释放。这是我的代码:

Activity 类别:

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));
}
}

适配器类:

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 类控制它的状态吗?或者是否可以从适配器内部执行此操作?

最佳答案

@Override
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
stopPlaying();
}

此处您要设置停止播放器。

ViewHolder在屏幕上不可见时触发

您应该将播放器实例放在 Activity 中,并通过适配器的接口(interface)回调(监听器模式)来控制它。

此外,使用单个媒体播放器而不是多个内部 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
}
}

关于java - 当 Activity onStop 被触发时,RecyclerView 释放 MediaPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61054751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com