0

I am playing audio files(MP3) by clicking on Item view, and the previous file stop automatically but the problem is that after tapping 3rd item of recyclerview the 1st one does not play sound on its click and the same problem happened on some other clicks of the list. I have added full Adapter class

public class RingToneAdapter extends RecyclerView.Adapter<RingToneAdapter.RingToneViewHolder> {
       //removed declared varaible for the sake of post to edit
       static final int[] resID = {R.raw.a48, R.raw.funny_hen};

        public RingToneAdapter(Context rcntx, List<RingTone_Items> ringtonelist) {
            this.rcntx = rcntx;
            this.ringtonelist = ringtonelist;
        }

        @NonNull
        @Override
        public RingToneViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            view = LayoutInflater.from(rcntx).inflate(R.layout.ringtone_values, viewGroup, false);
            RingToneViewHolder ringToneViewHolder = new RingToneViewHolder(view);
            return ringToneViewHolder;
        }

        //playing sounds on recycler view

        @Override
        public void onBindViewHolder(@NonNull final RingToneViewHolder ringToneViewHolder, final int i) {
            final RingTone_Items ringTone_items = ringtonelist.get(i);
            ringToneViewHolder.rtv.setText(ringTone_items.getRintonetv());
            if (mSelectedItem == i) { 
                 ringToneViewHolder.iconplay.setImageResource(R.drawable.ic_pause_black_24dp);
            } else {
                ringToneViewHolder.iconplay.setImageResource(R.drawable.ic_play_arrow_black_24dp); 
            }

            ringToneViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {              
                    if (mp != null && mp.isPlaying()) {
                        mp.stop();
                        mp.reset();
                        mp = null;
                        ringToneViewHolder.iconplay.setImageResource(R.drawable.ic_play_arrow_black_24dp);

                    }
                    //Intent it = new Intent(rcntx, ViewPager_Data.class);
                    Intent it = new Intent(rcntx, AndroidViewPagerExample.class);
                    it.putExtra("POS",i);
                    it.putExtra("name",ringTone_items.getRintonetv());

                    rcntx.startActivity(it);
                }
            });
            ringToneViewHolder.iconplay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if (mSelectedItem == i) {
                        mSelectedItem = -1;
                       oldpossssssss = i;
                    } else {
                        mSelectedItem = i;
                    }
                    notifyDataSetChanged();
                    if (mp != null && mp.isPlaying()) {
                        mp.stop();
                        mp.reset();
                        mp = null;

                        if (oldpossssssss == i) {
                        } else {
                           mp = new MediaPlayer();
                           mp = MediaPlayer.create(rcntx, resID[i]);
                           mp.start();
                       }
                  } else {
                        mp = new MediaPlayer();
                        mp = MediaPlayer.create(rcntx, resID[i]);
                        mp.start();
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return ringtonelist.size();
        }

        class RingToneViewHolder extends RecyclerView.ViewHolder {
            private TextView rtv, hello, close;
            private ImageView iconplay;


            public RingToneViewHolder(@NonNull View itemView) {
                super(itemView);
                rtv = itemView.findViewById(R.id.ringtitle);
                iconplay = itemView.findViewById(R.id.playicon);

            }

        }

How can I manage this smoothly according to click the play media file accordingly. Where is I am doing wrong please help me Thanks.

Marat
  • 6,142
  • 6
  • 39
  • 67
Sara
  • 11
  • 4
  • Can you post the whole adapter code? – Tanveer Munir Feb 12 '19 at 12:23
  • trying to edit whole adapter code but it gives me error...1st time I uploaded there so thats why these problems occurred – Sara Feb 12 '19 at 12:36
  • no problem resolve the errors and follow the instruction according to error and post it otherwise no one can understand your problem and look at the answer given below also. – Tanveer Munir Feb 12 '19 at 12:39
  • @TanveerMunir Now I have edited full adapter class – Sara Feb 13 '19 at 05:17
  • ok I have to look at it. – Tanveer Munir Feb 13 '19 at 07:43
  • please use this method to unselect the previous track before initializing the `null` use this method `deselectTrack()` – Tanveer Munir Feb 13 '19 at 09:54
  • added mp.deselectTrack(oldpossssss) but on 2nd click app crashes and gives error of "MediaPlayer.native_invoke(Native Method) – Sara Feb 13 '19 at 10:33
  • its take `resource ID` which means previous track ID that's why its crash. – Tanveer Munir Feb 13 '19 at 10:37
  • before `reset` and `null` deselect the previous track. Not give the item position. – Tanveer Munir Feb 13 '19 at 10:38
  • could u implement this line of code according to my code...???will be thankful to you – Sara Feb 13 '19 at 10:40
  • I will but first, you have to add the files its activity code and XML of activity and list Item and model code. Then I will try. – Tanveer Munir Feb 13 '19 at 10:47
  • Because its some kind of things are missing in adapter code. – Tanveer Munir Feb 13 '19 at 10:59
  • `resID` this array should be equal size of list items size @Faiza – Tanveer Munir Feb 13 '19 at 11:18
  • tried deselectedTrack method with resId but it not resolved my problem – Sara Feb 13 '19 at 11:20
  • I post my answer please have to look and let me know its working or not. – Tanveer Munir Feb 13 '19 at 11:56
  • not working....using this method mediaplayer play sound either I stop it or play it – Sara Feb 13 '19 at 12:07
  • I update my answer please have to look. – Tanveer Munir Feb 13 '19 at 12:28
  • also look at this [answer](https://stackoverflow.com/questions/29905696/mp3-file-read-from-raw-folder) to change the audio track in `MediaPlayer` – Tanveer Munir Feb 13 '19 at 13:00
  • tried your updated ans but not worked for me...:-(...by the way thank u soo much for your time – Sara Feb 13 '19 at 16:07
  • no problem its a community by the way can you update the question how many list items you're showing some kind of screenshot of your list because your Mp3 array has only two source file. Your Ringtone_Items class attributes? Basically your adapter code not enough to resolve your problem can you share tha main activity and Ringtone_Item class code. – Tanveer Munir Feb 13 '19 at 19:42
  • Your list size should be equal resID so please share its activity class code and Ringtone_Item class. – Tanveer Munir Feb 13 '19 at 19:45
  • my list size is 20 and there are also 20 raw files...all code is doing well... when I tapped 1st iconplay button it plays sound...same for 2nd and for all in listview...but when I tapped 1st iconplay button after tapping 3rd one...it does not play any sound...I think problem is only with 1st position...I think main problem occur due to position that mselected=-1; – Sara Feb 14 '19 at 02:46
  • Also I try to upload screenshot of my listview...but it can't upload here because of reputation count – Sara Feb 14 '19 at 02:56
  • I don't think so this is a problem have you debug that? there is code missing in your question also that's why I can't implement by myself which things are going wrong. Although I update my answer please have a look. – Tanveer Munir Feb 14 '19 at 07:35

2 Answers2

1

I would suggest you to use singletone media player in your viewHolders. Firstly, using multiple prepared mediaPlayers are not very memory efficient. Secondly, it will allow you to resolve problems with played sound on the background because one mediaPlayer will play only one audio at a time.

Valerii
  • 395
  • 1
  • 2
  • 8
  • I am sorry sir but I don't understand what are u trying to say...I am beginner ..So I think thats why I can't understand – Sara Feb 12 '19 at 12:39
  • @FaizaZubair he is trying to say that create on one object of `new MediaPlayer()` use operations on it stop and play because it's not memory efficient. – Tanveer Munir Feb 12 '19 at 12:42
0
ringToneViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {              
                    if (mp != null && mp.isPlaying()) {
                        mp.stop();
                        mp.reset();
                        mp.release();
                        mp = null;
                        ringToneViewHolder.iconplay.setImageResource(R.drawable.ic_play_arrow_black_24dp);

                    }
                    //Intent it = new Intent(rcntx, ViewPager_Data.class);
                    Intent it = new Intent(rcntx, AndroidViewPagerExample.class);
                    it.putExtra("POS",i);
                    it.putExtra("name",ringTone_items.getRintonetv());

                    rcntx.startActivity(it);
                }
            });    
ringToneViewHolder.iconplay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       if (mSelectedItem == i) {
                           oldpossssssss = i;
                        } else {
                            mSelectedItem = i;
                        }
                        notifyDataSetChanged();
                            if (oldpossssssss == i) {
                              if(mp != null){
                                 if(mp.mp.isPlaying()){
                                    mp.pause();
                                  } else{
                                    mp.start();
                                  }
                              }
                            } else {
                               createMediaPlayer(i)
                           }
                    }
                });

Use this method when you create MediaPlayer

private void createMediaPlayer(int i)
{
    if (mp!=null)
    {
        if(mp.isPlaying())
        {
            mp.stop();
            mp.reset();
            mp.release();
            mp=null;
        }

    }
    mp = new MediaPlayer();
    mp = MediaPlayer.create(rcntx, resID[i]);
    mp.start();
}

May be its work.

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27