Android Recyclerview MediaPlayer上一个项目声音停止,单击新的后发布

时间:2016-06-05 17:50:02

标签: android audio android-recyclerview media-player android-mediaplayer

在我的应用程序中,我有recyclerview项目,当我点击它时每个项目都会发出声音。 问题是,当我点击几个项目时,它们都会在同一时间播放声音,直到它们完成。 让我说我点击10项,所有10个播放声音在同一时间和一段时间后应用程序崩溃,我得到这个错误: E / MediaPlayer:错误(1,-19)。 我通过使用以下代码实现自动释放来避免应用程序崩溃:



    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mediaPlayer.release();

                        }
                    });




但那不是我想要的,在同一时间听到几个声音很烦人。我想要的是当我点击新的声音时停止以前的声音,我尝试实施媒体播放器停止和释放,但没有成功。

这是Google Play中的应用,因此您可以自行检查:https://play.google.com/store/apps/details?id=org.pero.androidd.animalSoundsQuiz

这是我的适配器代码,我播放声音。在这段代码中有部分MediaPlayer.OnCompletionListener(),我在声音结束后释放声音以避免 E / MediaPlayer:错误(1,-19)错误,但这不是我想要的应用程序。我想点击新的声音后停止以前的声音。



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 com.pero.androidd.animalsoundsforchildren.R;

import java.util.List;


public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {

    private List<Zivotinje> mZivotinje;


    @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
        //Dohvacamo context svakog views
        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);


            //Handdaling clicks
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                                        
                    final MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), mItem.getmSound());
                    mediaPlayer.start();

                    
                    //releace media player after sound finish
                    // BUT I DO NOT WANT TIS, I WANT TO STOP PREVIOUS SOUND WHEN I CLICK NEW ONE
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mediaPlayer.release();

                        }
                    });
                }
            });

        }


    }


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


}
&#13;
&#13;
&#13;

编辑编辑编辑编辑编辑编辑:

我找到解决方案,我不知道这是最聪明的方法,但它对我很有用:

我所做的是我制作了静态媒体播放器变量,这样我可以从ViewHolder访问媒体播放器,并在我点击新声音时停止以前的声音。希望它有所帮助。

这是整个代码:

&#13;
&#13;
public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {

    private static MediaPlayer mediaPlayer; //THIS IS THE CHANGE I DONE

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


//handaling clicks 
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                        mediaPlayer.release();
                        mediaPlayer = null;
                    }


                    mediaPlayer = MediaPlayer.create(v.getContext(), mItem.getmSound());
                    mediaPlayer.start();

                }
            });

        }


    }

    private List<Zivotinje> mZivotinje;

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


}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您首先要检查是否有正在播放的声音,如果有声音,您应该在用户决定开始播放新声音时将其停止。

<div class="panel panel-default">
  <div class="panel-heading clearfix">
    <h4 class="panel-title pull-left" style="padding-top: 7.5px;">Panel Title</h4>
    <div class="btn-group pull-right">
      <button type="button" class="btn btn-default">Button A</button>
      <button type="button" class="btn btn-default">Button B</button>
      <button type="button" class="btn btn-default">Button C</button>
    </div>
  </div>
  <div class="panel-body">
    Panel content.
  </div>
</div>

itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mediaplayer.isPlaying ()) { if (mediaplayer != null){ mediaplayer.stop (); } } else { if (mediaplayer != null) { mediaplayer.start (); } } } }); 中,如果用户收听整个声音片段,则会释放声音。

setOnCompletionListner