Android Audio Play Push Example Kotlin

06-Dec-2024

Android Audio Play Push Example Kotlin

Android Audio Play Push Example Kotlin



1/  Create a new project .

main_activity.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#A09A9A"
android:padding="16dp"
>


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#6E6A6A"
android:layout_centerInParent="true"


>
<!-- Progress Bar -->
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="20dp"
/>

<RelativeLayout
android:id="@+id/timeMethod"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/seekBar"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"

>
<!-- Current Time -->
<TextView
android:id="@+id/currentTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="00:04"
android:textColor="#FFF"
android:textSize="14sp" />

<!-- Total Time -->
<TextView
android:id="@+id/totalTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="03:31"
android:textColor="#FFF"
android:textSize="14sp" />

</RelativeLayout>



<!-- Control Buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/timeMethod"
android:layout_marginTop="16dp"
android:gravity="center"
android:orientation="horizontal">



<!-- Previous Button -->
<ImageButton
android:id="@+id/prevButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@android:color/transparent"
android:src="@drawable/skip_previous"
android:tint="#FFF" />

<!-- Play/Pause Button -->
<ImageButton
android:id="@+id/playPauseButton"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@android:color/transparent"
android:src="@drawable/pause_on"
android:tint="#FFF" />

<!-- Next Button -->
<ImageButton
android:id="@+id/nextButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@android:color/transparent"
android:src="@drawable/skip_next"
android:tint="#FFF" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>



1/  Create a directory

>>>res>new>Directory>raw



Put your mp3 audio file in raw directory




3/ MainActivity.kt


package com.audioplayer.audioplayer

import android.media.MediaPlayer
import android.os.Bundle
import android.os.Handler
import android.widget.ImageButton
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
private lateinit var mediaPlayer: MediaPlayer
private lateinit var playPauseButton: ImageButton
private lateinit var nextButton: ImageButton
private lateinit var prevButton: ImageButton
private lateinit var seekBar: SeekBar
private lateinit var currentTimeTextView: TextView
private lateinit var totalTimeTextView: TextView
private val handler = Handler()

private var audioList = listOf(
R.raw.ek_jenpe, // Replace with your actual file names
R.raw.kew_na_januk,
R.raw.song
)
private var currentAudioIndex = 0 // Current audio index


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize UI elements
playPauseButton = findViewById(R.id.playPauseButton)
nextButton = findViewById(R.id.nextButton)
prevButton = findViewById(R.id.prevButton)
seekBar = findViewById(R.id.seekBar)
currentTimeTextView = findViewById(R.id.currentTime)
totalTimeTextView = findViewById(R.id.totalTime)

// Initialize MediaPlayer
initializeMediaPlayer()



playPauseButton.setOnClickListener {
if (mediaPlayer.isPlaying) {
mediaPlayer.pause()
playPauseButton.setImageResource(R.drawable.pause_on) // Update to play icon
} else {
mediaPlayer.start()
playPauseButton.setImageResource(R.drawable.play_on) // Update to pause icon
}
}

// Next button functionality
nextButton.setOnClickListener {
playNextAudio()
}

// Previous button functionality
prevButton.setOnClickListener {
playPreviousAudio()
}


// SeekBar progress
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser) {
mediaPlayer.seekTo(progress)
}
currentTimeTextView.text = formatTime(mediaPlayer.currentPosition)
}

override fun onStartTrackingTouch(seekBar: SeekBar?) {}
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
})

// Update SeekBar and time
val updateSeekBar = object : Runnable {
override fun run() {
seekBar.progress = mediaPlayer.currentPosition
currentTimeTextView.text = formatTime(mediaPlayer.currentPosition)
handler.postDelayed(this, 1000)
}
}
handler.post(updateSeekBar)

// On completion, play next audio
mediaPlayer.setOnCompletionListener {
playNextAudio()
}
}

private fun initializeMediaPlayer() {
// Release old MediaPlayer if exists
if (::mediaPlayer.isInitialized) {
mediaPlayer.release()
}

// Initialize new MediaPlayer with the current audio
mediaPlayer = MediaPlayer.create(this, audioList[currentAudioIndex])
mediaPlayer.setOnPreparedListener {
seekBar.max = mediaPlayer.duration
totalTimeTextView.text = formatTime(mediaPlayer.duration)
mediaPlayer.start()
playPauseButton.setImageResource(R.drawable.play_on)
}
}

private fun playNextAudio() {
currentAudioIndex = (currentAudioIndex + 1) % audioList.size
initializeMediaPlayer()
}

private fun playPreviousAudio() {
currentAudioIndex = if (currentAudioIndex - 1 < 0) {
audioList.size - 1
} else {
currentAudioIndex - 1
}
initializeMediaPlayer()
}

// Helper function to format time
private fun formatTime(ms: Int): String {
val seconds = (ms / 1000) % 60
val minutes = (ms / 1000) / 60
return String.format("%02d:%02d", minutes, seconds)
}

override fun onDestroy() {
super.onDestroy()
mediaPlayer.release()
handler.removeCallbacksAndMessages(null)
}
}



Comments