Skip to main content

How to seek to a specific position in audio playback using MediaPlayer in Kotlin Android

How to seek to a specific position in audio playback using MediaPlayer in Kotlin Android.

Here's a step-by-step tutorial on how to seek to a specific position in audio playback using MediaPlayer in Kotlin for Android:

Step 1: Set up your project

First, create a new Android project in Kotlin using Android Studio. Make sure you have the necessary permissions to read and play audio files by adding the following line to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Step 2: Add the MediaPlayer dependency

Open your app-level build.gradle file and add the following dependency:

implementation ''

Step 3: Set up the layout

In your activity_main.xml file, add a Button for the user to trigger the seeking action. Also, add a SeekBar to visualize the current position and allow the user to seek to a specific position in the audio file:

android:text="Seek" />

android:progress="0" />

Step 4: Initialize the MediaPlayer

In your MainActivity.kt file, import the necessary classes and declare the MediaPlayer and SeekBar variables:

import android.os.Bundle
import android.widget.Button
import android.widget.SeekBar

class MainActivity : AppCompatActivity() {
private lateinit var mediaPlayer: MediaPlayer
private lateinit var seekBar: SeekBar

override fun onCreate(savedInstanceState: Bundle?) {

mediaPlayer = MediaPlayer.create(this, R.raw.audio_file)
seekBar = findViewById(

// Rest of the code goes here

Step 5: Implement the seek functionality

Inside the onCreate() method, add a click listener to the seek button and set up a seek bar listener to handle user interaction with the seek bar:

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

val buttonSeek: Button = findViewById(
buttonSeek.setOnClickListener {
val targetPosition = calculateTargetPosition() // Replace this with your desired position

seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser) {
mediaPlayer.seekTo(mediaPlayer.duration * progress / 100)

override fun onStartTrackingTouch(seekBar: SeekBar?) {
// No implementation needed

override fun onStopTrackingTouch(seekBar: SeekBar?) {
// No implementation needed

Step 6: Calculate the target position

Implement the calculateTargetPosition() function to calculate the desired position in milliseconds based on user input or any other logic you need:

private fun calculateTargetPosition(): Int {
// Replace this with your desired position calculation logic
return 5000 // Seek to 5 seconds into the audio file

Step 7: Release the MediaPlayer

To avoid memory leaks, make sure to release the MediaPlayer when the activity is destroyed:

override fun onDestroy() {

That's it! You have successfully implemented audio seeking using MediaPlayer in Kotlin for Android. You can now seek to a specific position in audio playback by clicking the seek button or dragging the seek bar.