Android how to stop media player when new sound st

2019-07-23 03:19发布

I try to find solution but could not find it.

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: error (1, -19)

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.

Here is my Adapter code:

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




}

1条回答
做个烂人
2楼-- · 2019-07-23 03:47

Add this:

@Override
public void onClick(View v) {
stopPlaying();  <-------            
MediaPlayer mediaPlayer =   MediaPlayer.create(v.getContext(),mItem.getmSound());
mediaPlayer.start();}

and this:

.......

public ZivotinjeAdapter(List<Zivotinje> animals) {
    mZivotinje = animals;
}

private void stopPlaying() {

    if(mediaPlayer != null && mediaPlayer.isPlaying())
    {
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.release();
        mediaPlayer = null;
    }
}
查看更多
登录 后发表回答