MediaPlayer not looping a raw resource MP3 file (Kotlin)

62 views Asked by At

Can someone help me to loop my audio source? I'm new to using the MediaPlayer, but I cannot get it to loop the sound. The sound plays, then quits and never starts up again.

I have read countless articles, and tried adding listeners to detect errors and end of play, but they are not getting triggered.

Here is a sample of my Kotlin code

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Code for activity_main.xml
        setContentView(R.layout.activity_main)

        // Init
        var textView: TextView
        var txtSceneDesc: TextView
        var txtPlayerCmd: TextView
        var txtSceneText: String
        textView = findViewById(R.id.txtSceneDescription)
        textView.movementMethod = ScrollingMovementMethod()
        txtSceneDesc = findViewById(R.id.txtSceneDescription)
        txtPlayerCmd = findViewById(R.id.textInputEditText)

        var mediaPlayer: MediaPlayer? = null  // for playing sounds
        //val id = this.resources.getIdentifier("ocean_waves.mp3", "raw", this.packageName)
        val id = getResources().getIdentifier(
            "ocean_waves",
            "raw", getPackageName()
        );
        var r_Sound_Water: Int = id


        // Completion listener
        mediaPlayer?.setOnCompletionListener {
            Toast.makeText(this@MainActivity, "Playback Completed", Toast.LENGTH_SHORT).show()
            mediaPlayer?.reset()
            mediaPlayer?.start()
            // You can perform other actions here as needed.
        }

        // Error listener
        mediaPlayer?.setOnErrorListener { MediaPlayer, what, extra ->
            Toast.makeText(this@MainActivity, "Playback Error", Toast.LENGTH_SHORT).show()
            // Return true if the error was handled, false otherwise.
            true
        }

        if (mediaPlayer == null) {
            Toast.makeText(this@MainActivity, "Media player is null", Toast.LENGTH_SHORT).show()
            mediaPlayer = MediaPlayer.create(this@MainActivity, r_Sound_Water)
            mediaPlayer.isLooping = true
            mediaPlayer.start()
        } else {
            Toast.makeText(this@MainActivity, "Media Player is assigned", Toast.LENGTH_SHORT).show()
            mediaPlayer.start()
        }

        textView.text = "Run 29"

        // End code for activity_main.xml
    }
}
2

There are 2 answers

1
hata On BEST ANSWER

I think mediaPlayer.isLooping = true is enough to accomplish your intent.

For simplicity, below code should work without problem:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val mediaPlayer = MediaPlayer.create(this, R.raw.ocean_waves)
    mediaPlayer.isLooping = true
    mediaPlayer.start()
}

Please examine it for yourself.

Your problem's root may be other place.

1
dev.bmax On

MediaPlayer is a state machine. Properties such as isLooping and volume should be changed when the player is in the PREPARED state.

Try this:

mediaPlayer.setOnPreparedListener { mp -> mp.isLooping = true }
mediaPlayer.start()