Aim:
Aim: Write a program to play songs using services
The android.media.MediaPlayer package provides a simple and flexible way to play audio and video files within an Android application.
It allows you to play media from different sources, such as local files, network streams, and more.
To play local audio files using the MediaPlayer class, you typically use the following steps:
Create a new instance of the MediaPlayer class.
Set the data source for the MediaPlayer using one of several methods, such as setDataSource() or setDataSource(Context, Uri).
Prepare the MediaPlayer for playback using prepare() or prepareAsync().
Start the playback using start().
Pause, resume, or stop the playback using pause(), resume(), or stop(), respectively.
Release the MediaPlayer using release() when you are done with it.
In the previous code example, we used the openRawResourceFd() method to get an AssetFileDescriptor for the current song file, set the data source using setDataSource(), prepare the MediaPlayer using prepare(), and start the playback using start().
We also handled the end of the song by incrementing the song index and calling playSong() again to play the next song.
Note that the MediaPlayer class is not suitable for streaming media, as it loads the entire media file into memory before playback.
For streaming media, you should use the android.media.MediaPlayer package's android.media.MediaDataSource API or a third-party library such as ExoPlayer.
Process:
Add the following permissions to the AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Create a new activity class named MainActivity that contains buttons to play, pause, and stop the music.
Add the following layout code to activity_main.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
<Button
android:id="@+id/pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
<Button
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
Add local music files to the res/raw folder in the res directory.
In the MainActivity class, implement the playSong(), pauseSong(), and stopSong() methods using the android.media.MediaPlayer package to play, pause, and stop the music.
In the playSong() method, use the getResources().openRawResourceFd() method to get an AssetFileDescriptor for the current song file, set the data source using mMediaPlayer.setDataSource(), prepare the MediaPlayer using mMediaPlayer.prepare(), and start the playback using mMediaPlayer.start().
In the stopSong() method, stop the playback using mMediaPlayer.stop(), release the MediaPlayer using mMediaPlayer.release(), and set mMediaPlayer to null.
In the onDestroy() method, release the MediaPlayer using mMediaPlayer.release(), and set mMediaPlayer to null.
Run the app and click the "Play" button to play the first song. Click the "Pause" button to pause the song and click the "Stop" button to stop the song and reset the song index.
