0

I just wanted to play some audio file(.mp3 for instance) in my Recyclerview Adapter class, I implemented it pretty easily, but I don't know why it is not playing as well as not showing any error for trace, have a look on code below, and guide me please:

 override fun onBindViewHolder(holder: PriceAdapter.PriceHolder, pos: Int) {
    holder.qt!!.text = questionsList[pos].priceText

    holder.qt!!.setOnClickListener(View.OnClickListener {
        if (questionsList[pos].price == questionsList[pos].priceNow) {

      //This is where I wanted to implement my mediaplayer:

     var mediaPlayer: MediaPlayer? =MediaPlayer.create(it.context,R.raw.sound_file_1)
     mediaPlayer?.start()
     }
   }

It is not playing anything, rest are good, I have the raw mp3 file in res/raw/sound_file_1.mp3, what is possibly wrong with this?

Abdur Rahman
  • 894
  • 1
  • 11
  • 27
Rahul Singh
  • 243
  • 3
  • 15
  • Create a Singleton class that contains start , stop ,pause method and for each View use music path and pass it to start function and then check if media.is already playing any music stop it and play given new path. – Atif AbbAsi Apr 29 '19 at 05:48
  • Would you care to write that for me, considering me as confused? – Rahul Singh Apr 29 '19 at 05:49
  • start doing a little bit RND for your better understanding.. https://www.tutorialspoint.com/how-to-use-android-media-player-singleton https://stackoverflow.com/questions/30743351/how-to-use-a-mediaplayer-singleton https://stackoverflow.com/questions/28380525/android-one-mediaplayer-instance-singleton – Atif AbbAsi Apr 29 '19 at 05:55
  • @RahulSingh so everytime onclick gets exceuted you will be playing same single file lik that – Jeeva Apr 29 '19 at 06:30

1 Answers1

0

According to Android docs on MediaPlayer implementation, MediaPlayer should be prepared before start playing.

For reference MediaPlayer state diagram:

enter image description here

A MediaPlayer object must first enter the Prepared state before playback can be started.

Please check if you have performed these steps.

What I feel you can do it as.

Instead of creating the MediaPlayer in the onBindViewHolder() of RecyclerView pass it as a parameter from where the adapter is getting created, this will be efficient, because each time list is scrolled MediaPlayer object will get created, which I think is not a correct way to call. Open for discussion.

Mohammed Rampurawala
  • 3,033
  • 2
  • 22
  • 32
  • https://developer.android.com/guide/topics/media/mediaplayer if you go to this link you will find this comment in code snippet saying that // no need to call prepare(); create() does that for you, Since I'm new dev, I will appreciate if you code some snippet for me for better understanding, will you? – Rahul Singh Apr 29 '19 at 05:57
  • 1
    Can you pass the MediaPlayer in a constructor of Adapter. Because It was getting acquired by multiple list items so it was creating a problem. – Mohammed Rampurawala Apr 29 '19 at 06:24
  • Yes, After passing in constructor? – Rahul Singh Apr 29 '19 at 06:31
  • @RahulSingh as mohamed says try to create single static instance for media player and just call the play method to play ur raw file – Jeeva Apr 29 '19 at 06:32
  • Yes, I'm trying that, I'll update in no time, please wait. – Rahul Singh Apr 29 '19 at 06:35
  • After passing it to constructer mediaPlayer.create() method is not being shown, why is that? – Rahul Singh Apr 29 '19 at 06:54
  • You should implicitly specify the constructor value as MediaPlayer. Make sure that is done – Mohammed Rampurawala Apr 29 '19 at 06:55
  • Consider the code above and implement it for me, I relatively new to the coding, these terms are not clear for me without proper demonstration. – Rahul Singh Apr 29 '19 at 07:17