
Serete Itebete
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
http://developer.android.com/reference/android/media/MediaPlayer.html AudioDemo.java
Code:
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.xmlLayout file
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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
Code:
<?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
Sourcehttp://marakana.com/static/tutorials/AudioDemo.zip
Edited 2 times. Last edit by Carl J on Jul 21, 2010 at 8:58:20 AM (about 11 weeks ago).

Paul Jackson
Uni
Hi Serete,
Thanks a lot for your code. However, I tried it using a robotrock.mp3 and robotrock.wav recorded by myself, and psted within the raw folder, but it did not work in none of the cases.
Your code seems fine. I tried 2 different audio formats but none of them worked. I can't play sound. Could you please help? Thanks

Serete Itebete
Paul,
Thanks for the information on your own recorded mp3 format. In my case I used the .mp3 and .m4a music files-as-is and it worked fine and dropped them as you in the res/raw folder.
There are quite a number of formats that are supported by Android Platform and here is a link to page
http://developer.android.com/guide/appendix/media-formats.html and since I dont know too much about audio formats and their details, I can only direct you to the link above, unless someone else has more to say.
Thanks

Marko Gargenta
Marakana, Inc.
Also, make sure you do not specify the extension when referencing the resource in your Java code. So, for example, if your music file is /res/raw/braincandy.mp3, you reference it as R.raw.braincandy without any extension. Android auto-magically figures out what extension to use.
Hope this helps!
Marko

Paul Jackson
Uni
Thanks a lot Serete and Marko,
The volume of my phone was too low to hear the sound, but now I can hear, which means your code is right. However, I still have a problem, since it only plays for a very brief time (less than a second) and then it stops. I'm not sure if it could be my phone not supporting the audio. I'm going to keep trying. Thanks a lot.

Cooper Archard
UMF
The error you are having where the music will only play for a second or two is in the OnCreate where it says "player.release()" comment this out and it will work fine.... At least it did in my case running 1.6

A Member
Org
I see an inconsistent behavior in the GUI between the phone and the emulator. I don't see the buttons on the phone. I see them only on the Emulator. Do you have any idea why this happens?

Marko Gargenta
Marakana, Inc.
No clue. There should be just one button. The layout is simple enough that it shouldn't behave differently on various devices (physical or virtual).

Rohan R
NA
Thx a lot...very useful information.. After so many try on different example on different site, this was the best example.. thx Marko and Serete.. please keep us update on new things.
Thx
rohn
Edited one time. Last edit by Rohan R on May 6, 2010 at 8:29:00 AM (about 11 weeks ago).

Carl J
Hdsl
Confirmative the code from this tutorial is working. Many thanks! However, so fare I could not succeed using RAW for music files when using Netbeans. Resources like text files works fine. Anyway, when I loaded the music file directly from the sd card of the device, then everything worked fine.
Edited 3 times. Last edit by Carl J on Jul 21, 2010 at 9:34:36 AM (about 11 weeks ago).