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 'androidx.media:media:1.3.0'

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:

<Button
android:id="@+id/buttonSeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seek" />

<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
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.media.MediaPlayer
import android.os.Bundle
import android.widget.Button
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity

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

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

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

// 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(R.id.buttonSeek)
buttonSeek.setOnClickListener {
val targetPosition = calculateTargetPosition() // Replace this with your desired position
mediaPlayer.seekTo(targetPosition)
}

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() {
super.onDestroy()
mediaPlayer.release()
}

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.