Search code examples
androidkotlinandroid-mediaplayerplayback

Change speed of audio file in kotlin


I am making a metronome app in Android Studio, using Kotlin, and need to be able to change the speed of the audio file playing.

Min SDK: 23 Target SDK: 32

This is what I've currently got but changing the playBackSpeed variable doesn't change the playback speed.

val play = findViewById<Button>(R.id.play)
val wavTest = MediaPlayer.create(this, R.raw.woodblock_high44_wav)

var playBackSpeed = 3f

play.setOnClickListener {
    wavTest.isLooping = true
    wavTest.playbackParams = PlaybackParams().setSpeed(playBackSpeed)
    wavTest.start()

    wavTest.playbackParams.speed = playBackSpeed
}

Any help or advice would be appreciated.


Solution

  • I find changing the speed of MediaPlayer works fine, but to make a metronome app I think you should refer to this source code, I think these are good directions to make a metronome app.

    Visit link 1: https://github.com/o4oren/android-kotlin-metronome

    Visit link 2: https://github.com/zachurchill-root/metronome-kotlin