In this demo, we work with the audio media player API to play a raw media file and can also be used for streaming media.
Most importantly is the creation of the media player instance MediaPlayer player = MediaPlayer.create(this.context, media source)
. Use of the various player instance methods like player.play()
or player.pause()
to do as they suggest to play and pause the media playback.
The AudioDemo is straight forward creating the layout and listening to the button click events and response accordingly. When the audiodemo starts note the change of the button change from "Play" to "Stop" and when the demo is paused a return of button text to "Play" Added a toast message to notify the user what is going on.
Note:
The onPause() method is overrided to give playerback control to this instance.
In the res folder, created a raw folder to keep the raw media files in this case robotrack.mp3 (called by R.raw.robotrack)and other types of raw media files. Feel free to drop any type of audio media files but dont forget to change the reference of the files in the Audio Activity.
Always release the media instance back to system in this i.e. player.release();
A good resource about the various media player states, methods and corresponding state diagrams is https://developer.android.com/reference/android/media/MediaPlayer.html
AudioDemo.java
package org.example.audio;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class AudioDemo extends Activity implements OnClickListener {
private static final String TAG = "AudioDemo";
private static final String isPlaying = "Media is Playing";
private static final String notPlaying = "Media has stopped Playing";
MediaPlayer player;
Button playerButton;
public void onClick(View v) {
Log.d(TAG, "onClick: " + v);
if (v.getId() == R.id.play) {
playPause();
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
player = MediaPlayer.create(this, R.raw.robotrock);
player.setLooping(false); // Set looping
// Get the button from the view
playerButton = (Button) this.findViewById(R.id.play);
playerButton.setText(R.string.stop_label);
playerButton.setOnClickListener(this);
// Begin playing selected media
demoPlay();
// Release media instance to system
player.release();
}
@Override
public void onPause() {
super.onPause();
player.pause();
}
// Initiate media player pause
private void demoPause(){
player.pause();
playerButton.setText(R.string.play_label);
Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
Log.d(TAG, notPlaying);
}
// Initiate playing the media player
private void demoPlay(){
player.start();
playerButton.setText(R.string.stop_label);
Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
Log.d(TAG, isPlaying);
}
// Toggle between the play and pause
private void playPause() {
if(player.isPlaying()) {
demoPause();
} else {
demoPlay();
}
}
}
main.xml
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/play"
android:text="@string/play_label"></Button>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AudioDemo</string>
<string name="app_name">Audio Demo</string>
<string name="play_label">Play</string>
<string name="stop_label">Stop</string>
</resources>
Output

Source
https://www.protechtraining.com/static/tutorials/AudioDemo.zip