STEP 1: Create an Empty Activity
STEP 2: Create a raw resource folder
Create a raw resource folder under the res folder and copy one of
the .mp3 file extension.
Right Click on "raw" folder ----> select "new" ---> click on "Android
Resource Directory" ----> Change resource type values to "raw" ---->
then finally click "OK".
Download required audio MP3 file (Size <500kb approximately) from
Internet , right click and Paste it in "raw" folder whatever newly created.
--------------------------------------------------------------------
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<TextView
android:id="@+id/headingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="MEDIA PLAYER"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/headingText"
android:layout_marginTop="16dp"
android:gravity="center_horizontal">
<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:backgroundTint="@color/colorPrimary"
android:text="STOP"
android:textColor="@android:color/white"
tools:ignore="ButtonStyle,TextContrastCheck" />
<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:backgroundTint="@color/colorPrimary"
android:text="PLAY"
android:textColor="@android:color/white"
tools:ignore="ButtonStyle,TextContrastCheck" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:text="PAUSE"
android:textColor="@android:color/white"
tools:ignore="ButtonStyle,TextContrastCheck" />
</LinearLayout>
</RelativeLayout>
MainActivity.kt
// As per your application (or) file name package name will displayed
package com.example.mediaplayer
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// create an instance of mediplayer for audio playback
val mediaPlayer: MediaPlayer =
MediaPlayer.create(applicationContext, R.raw.music)
// register all the buttons using their appropriate IDs
val bPlay: Button = findViewById(R.id.playButton)
val bPause: Button = findViewById(R.id.pauseButton)
val bStop: Button = findViewById(R.id.stopButton)
// handle the start button to
// start the audio playback
bPlay.setOnClickListener {
// start method is used to start
// playing the audio file
mediaPlayer.start()
}
// handle the pause button to put the
// MediaPlayer instance at the Pause state
bPause.setOnClickListener {
// pause() method can be used to
// pause the mediaplyer instance
mediaPlayer.pause()
}
// handle the stop button to stop playing
// and prepare the mediaplayer instance
// for the next instance of play
bStop.setOnClickListener {
// stop() method is used to completely
// stop playing the mediaplayer instance
mediaPlayer.stop()
// after stopping the mediaplayer instance
// it is again need to be prepared
// for the next instance of playback
mediaPlayer.prepare()
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter