java - 移动到下一个片段时如何停止当前片段媒体播放器?
问题描述
我有两个带有 tabLayout 的片段,当我想在另一个片段中播放声音时,我试图停止片段的当前声音这是我的适配器类
public class Adapter extends RecyclerView.Adapter<Adapter.holder> {
public Context context;
String data[];
int images[];
int sound[];
holder h;
int playPosition = -1;
public Adapter() {
}
//constructor
public Adapter(String[] data, int[] images, int[] sound, Context context) {
this.data = data;
this.images = images;
this.sound = sound;
this.context = context;
h = null;
playPosition = -1;
}
//class holder
public class holder extends RecyclerView.ViewHolder {
AppCompatImageView imageView;
TextView textView;
MediaPlayer mPlayer;
public holder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view);
imageView = itemView.findViewById(R.id.img_view);
}
}
@NonNull
@Override
public holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.singleview, parent, false);
return new holder(view);
}
@Override
public void onBindViewHolder(@NonNull final holder holder, final int position) {
holder.textView.setText(data[position]);
holder.imageView.setImageResource(images[position]);
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
if (holder.mPlayer != null) {
holder.mPlayer.release();
holder.mPlayer = null;
}
holder.mPlayer = MediaPlayer.create(context, sound[position]);
// stops the previous sound
if (h != null && playPosition != -1 && position != playPosition) {
h.mPlayer.stop();
h.mPlayer = MediaPlayer.create(context, sound[playPosition]);
}
holder.mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
holder.mPlayer.start();
Toast.makeText(context, "started", Toast.LENGTH_SHORT).show();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
mediaPlayer.release();
holder.mPlayer = null;
Toast.makeText(context, "Finished", Toast.LENGTH_SHORT).show();
// holder.playIcon.setVisibility(View.VISIBLE);
}
});
}
});
} catch (Exception e) {
e.printStackTrace();
}
h = holder;
playPosition = position;
}
});
}
@Override
public int getItemCount() {
return data.length;
}
}
这是我的片段
public class NatureForest extends Fragment {
RecyclerView recyclerView;
public NatureForest() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main, container, false);
String tab2Text[] = {"Forest forest","Forest creek","Forest leaves","Forest birds",
"Forest waterfall","Forest wind","Forest fire",
"Forest grasshopper", "Forest frogs"};
int tab2Images[] = {R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2,
R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2,R.drawable.ic_rain2};
int sound[]={R.raw.sounds,R.raw.sounds,R.raw.sounds,R.raw.sounds,R.raw.sounds,
R.raw.sounds,R.raw.sounds,R.raw.sounds,R.raw.sounds,R.raw.sounds,
R.raw.sounds};
recyclerView =view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
recyclerView.setAdapter(new Adapter(tab2Text,tab2Images,sound,getContext()));
return view;
}
}
在这里,如果播放一个片段中的声音,同时从第二个片段播放另一个声音,则不会停止前一个片段的声音。
解决方案
暂停()
:系统调用此方法作为用户离开片段的第一个指示。这通常是您应该提交任何应该在当前用户会话之外持久化的更改的地方
只需覆盖此方法并暂停该片段中的媒体播放器实例
推荐阅读
- xml - 删除 XSLT 映射生成的元素的名称空间前缀
- scala - ZonedDateTime 的 Flink 序列化
- php - 我在使用 twilio 语音时遇到了这个致命错误
- html - 使按钮、选择和输入都具有相同的高度
- javascript - 在 mocha 中出现“超过 2000 毫秒的超时”错误
- php - php从服务器下载文件不起作用
- sql-server - 批量插入返回错误,“文件无法打开”
- c# - 在 ITextSharp 的下一行自动创建表格
- xml - 如何在包含 < > 而不是 '<' 和 '>' 的 xml 上编写 xslt
- linux - 在 CentOS 上使用 Realtek ALC886 声卡将两个音频输入作为单独的线路