I can’t customize the pause button in Android Kotlin. Can maybe some help me?

  android, kotlin

Hello everyone, Im still super noob in Android and kotlin and cant customize the pause button which will stop the sound in my application, so my pauseButton just dont want to work. mb someone can help me? Im using MediaPlayer and audioManager and my sound(alarm sound) is in res/raw


lateinit var bindingClass : ActivityMainBinding
lateinit var pendingIntent: PendingIntent

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
     bindingClass = ActivityMainBinding.inflate(layoutInflater)
        setContentView(bindingClass.root)
        val audioManager: AudioManager = getSystemService(AUDIO_SERVICE) as AudioManager

        bindingClass.imageButton.setOnClickListener {
            val mediaPlayer = MediaPlayer.create(this, R.raw.raw)
            mediaPlayer.start()
            val myIntent = Intent([email protected], MyAlarmService::class.java)
            pendingIntent = PendingIntent.getService([email protected], 0, myIntent, 0)
            val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
            val calendar: Calendar = Calendar.getInstance()
            calendar.timeInMillis = System.currentTimeMillis()
            calendar.add(Calendar.SECOND, 3)
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
            Toast.makeText(baseContext, "Starting Service Alarm", Toast.LENGTH_LONG).show()
        }
        bindingClass.pauseButton.setOnClickListener {
            val mediaPlayer = MediaPlayer.create(this, R.raw.raw)
            if(mediaPlayer.isPlaying)
                mediaPlayer.stop()
            val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
            alarmManager.cancel(pendingIntent)
            Toast.makeText(baseContext, "Service Cancelled", Toast.LENGTH_LONG).show()
        }
        bindingClass.soundButton.setOnClickListener {

            val maxVolume = audioManager.mediaMaxVolume
            val randomIndex = Random().nextInt(((maxVolume - 0) + 1) + 0)
            audioManager.setMediaVolume(randomIndex)
            toast("Max: $maxVolume / Current: ${audioManager.mediaCurrentVolume}")
        }
    }
    fun playSound(view: View) {}
    fun pauseSound(view: View) {}
}
fun AudioManager.setMediaVolume(volumeIndex:Int) {
    this.setStreamVolume(
            AudioManager.STREAM_MUSIC,
            volumeIndex,
            AudioManager.FLAG_SHOW_UI
    )
}
val AudioManager.mediaMaxVolume:Int
    get() = this.getStreamMaxVolume(AudioManager.STREAM_MUSIC)

val AudioManager.mediaCurrentVolume:Int
    get() = this.getStreamVolume(AudioManager.STREAM_MUSIC)

fun Context.toast(message: String) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}```

Source: Android Questions

LEAVE A COMMENT