问题描述
我尝试找到解决方案,但找不到.
I try to find solution but could not find it.
我的问题是,我在recycleview中有很多物品.每个项目都有图像,当我单击该图像时,该图像就会开始发出声音.问题是当我开始新声音时,先前的声音不会停止,它会一直播放直到声音结束.我单击15后,20种声音的应用程序崩溃了,我收到此错误消息:
My problem is that i have lots of items in recycleview. Each item has image and when i click that image it start sound for that image.Problem is when i start new sound previous sound do not stop, its keep playing until sound finish. After i click 15, 20 sounds app get crashed and i receive this error message:
E/MediaPlayer:错误(1,-19)
E/MediaPlayer: error (1, -19)
当我单击新的声音时,我需要以某种方式实现上一声音的释放和停止.我尝试找到解决方案,但找不到它用于recycleview.
Somehow i need to implement release and stop of the previous sound when i click on the new one. I try to find solution but i could not find it for recycleview.
这是我的适配器代码:
import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {
@Override
public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) {
Zivotinje zivotinjeIme = mZivotinje.get(position);
// Set item views based on the data model
TextView textView = viewHolder.nameTextView;
textView.setText(zivotinjeIme.getmAnimal_name());
//THIS IS WHERE I HANDALE IMAGE VIEW. Using glide for images
ImageView slika_source = viewHolder.imageView;
Glide.with(slika_source.getContext())
.load((Integer) zivotinjeIme.getmAnimal_slika())
.into(slika_source);
viewHolder.setItem(mZivotinje.get(position));
}
@Override
public int getItemCount() {
return mZivotinje.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView nameTextView;
private Zivotinje mItem;
public void setItem(Zivotinje item) {
this.mItem = item;
}
public ViewHolder(final View itemView) {
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id);
imageView = (ImageView) itemView.findViewById(R.id.slika_id);
//this is click event for each item and here i play sound
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), mItem.getmSound());
mediaPlayer.start();
}
});
}
}
private List<Zivotinje> mZivotinje;
public ZivotinjeAdapter(List<Zivotinje> animals) {
mZivotinje = animals;
}
}
推荐答案
添加此内容:
@Override
public void onClick(View v) {
stopPlaying(); <-------
MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(),mItem.getmSound());
mediaPlayer.start();}
和这个:
.......
public ZivotinjeAdapter(List<Zivotinje> animals) {
mZivotinje = animals;
}
private void stopPlaying() {
if(mediaPlayer != null && mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
这篇关于Android如何在Recycleview中启动新声音时停止媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!