Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
659 B
1
Indexable
Never
object MediaPlayerManager {
    private var mediaPlayer: MediaPlayer? = null

    fun playMusic(context: Context, resId: Int) {
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(context, resId)
            mediaPlayer?.isLooping = true
            mediaPlayer?.start()
        } else {
            if (!mediaPlayer!!.isPlaying) {
                mediaPlayer!!.start()
            }
        }
    }

    fun stopMusic() {
        mediaPlayer?.let {
            if (it.isPlaying) {
                it.stop()
                it.reset()
                it.release()
                mediaPlayer = null
            }
        }
    }
}
Leave a Comment