Playing audio

Probably the most basic of needs for multimedia on a cell phone is the ability to play audio files, whether new ringtones, MP3s, or quick audio notes. Android's Media Player is easy to use. At a high level all you need to do to play back an MP3 file is follow these steps:

1 Put the MP3 in the res/raw directory in a project (note that you can also use a URI to access files on the network or via the internet).

2 Create a new instance of the MediaPlayer and reference your MP3 by calling MediaPlayer.create().

3 Call the MediaPlayer methods prepare() and start().

Let's work through a simple example to demonstrate exactly how simple this is. First create a new project called MediaPlayer Example with an Activity called MediaPlay-erActivity. Now create a new folder under res/ called raw. This is where we will store our MP3s. For this example we will use a ringtone for the game Halo 3, which you can retrieve from MediaPlayer.create. Download the Halo 3 theme song (and any other MP3s), and put them in the raw directory. Next, create a simple Button for the music player, as in listing 10.1.

Listing 10.1 main.xml for MediaPlayer Example

<?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="Simple Media Player" />

<Button android:id="@+id/playsong" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Halo 3 Theme Song" />

</LinearLayout>

Next we need to fill out our MediaPlayerActivity class, as in listing 10.2. Listing 10.2 MediaPlayerActivity.java public class MediaPlayerActivity extends Activity {

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

Set the view and a button to play an MP3

Set the view and a button to play an MP3

Button mybutton = (Button) findViewByld(R.id.playsong); 1

mybutton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) { MediaPlayer mp = MediaPlayer.create(MediaPlayerActivity.this, R.raw.halotheme);

mp.setOnCompletionListener (new OnCompletionListener (){ public void onCompletion(MediaPlayer arg0) {

Get the context and then play the MP3

As you can see, playing back an MP3 is easy. In listing 10.2 all we did was use a view that we created in listing 10.1 and map the button, playsong, to mybutton, which we then bound to the setOn-ClickListener() O- Inside the listener we created the MediaPlayer instance Q using the create(Context context, int resourceid) method, which simply takes our context and a resource ID for our MP3. Finally we set the setOnCompletionListener, which will perform some task on completion. For the moment we do nothing, but you might want to change a button's state or provide a notification to a user that the song is over or ask if the user would like to play another song. If so, you would use this method.

Now if you compile the application and run it, you should see something like figure 10.2. Click the button, and you should hear the Halo 3 song played back in the emulator via your speakers. You can also control the volume of the playback with the volume switches on the side of the Android Emulator phone visualization.

Now that we have looked at how to play an audio file, let's see how we can play a video file.

0 0

Post a comment