PrepareAsyncO Preparing

Appportunity

Build your own Android App Dev Empire

Get Instant Access

prepareAsyncO

Looping = true && playback completes stopO

Looping = true && playback completes prepareAsyncO

Stopped

seekToQ/pause()

Paused

Looping = false && onCompletlonO invoked on OnCompletionListener startO (note: from beginning)

seekToQ/pause()

Stopped

Paused

Looping = false && onCompletlonO invoked on OnCompletionListener

Figure 6-1. MediaPlayer state diagram from Android API Reference startO (note: from beginning)

seekToO PlaybackCompleted~~^)

Figure 6-1. MediaPlayer state diagram from Android API Reference

Here is a full MediaPlayer example that uses prepareAsync and implements several listeners to keep track of its state.

package com.apress.proandroidmedia.ch06.audiohttpasync;

import java.io.IOException; import android.app.Activity; import android.media.MediaPlayer;

import android.media.MediaPlayer.OnBufferingUpdateListener;

import android.media.MediaPlayer.OnCompletionListener;

import android.media.MediaPlayer.OnErrorListener;

import android.media.MediaPlayer.OnInfoListener;

import android.media.MediaPlayer.OnPreparedListener;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

In this version of our HTTP audio player, we are implementing several interfaces. Two of them, OnPreparedListener and OnCompletionListener, will help us keep track of the state of the MediaPlayer so that we don't attempt to play or stop audio when we shouldn't.

public class AudioHTTPPlayer extends Activity implements OnClickListener, OnErrorListener, OnCompletionListener, OnBufferingUpdateListener, OnPreparedListener {

MediaPlayer mediaPlayer;

The interface for this activity has start and stop Buttons, a TextView for displaying the status, and a TextView for displaying the percentage of the buffer that has been filled.

Button stopButton, startButton;

TextView statusTextView, bufferValueTextView;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

In the onCreate method, we set the stopButton and startButton to be disabled. They'll be enabled or disabled throughout the running of the application. This is to illustrate when the methods they trigger are and aren't available.

stopButton = (Button) this.findViewById(R.id.EndButton);

startButton = (Button) this.findViewById(R.id.StartButton);

stopButton.setOnClickListener(this);

startButton.setOnClickListener(this);

stopButton.setEnabled(false);

startButton.setEnabled(false);

bufferValueTextView = (TextView) this.findViewById(R.id.BufferValueTextView); statusTextView = (TextView) this.findViewById(R.id.StatusDisplayTextView); statusTextView.setText("onCreate");

After we instantiate the MediaPlayer object, we register the activity to be the OnCompletionListener, the OnErrorListener, the OnBufferingUpdateListener, and the OnPreparedListener.

mediaPlayer = new MediaPlayer();

mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnErrorListener(this); mediaPlayer.setOnBufferingUpdateListener(this); mediaPlayer.setOnPreparedListener(this);

statusTextView.setText("MediaPlayer created"); Next we call setDataSource with the URL to the audio file. try {

mediaPlayer.setDataSource(

"http://www.mobvcasting.com/android/audio/goodmorningandroid.mp3");

statusTextView.setText("setDataSource done"); statusTextView.setText("calling prepareAsync");

Last, we'll call prepareAsync, which will start the buffering of the audio file in the background and return. When the preparation is complete, our activity's onCompletion method will be called due to our activity being registered as the OnCompletionListener for the MediaPlayer.

mediaPlayer.prepareAsync();

Log.v("AUDIOHTTPPLAYER",e.getMessage());

What follows is the implementation of the onClick method for the two Buttons. When the stopButton is pressed, the MediaPlayer's pause method will be called. When the startButton is pressed, the MediaPlayer's start method is called.

public void onClick(View v) { if (v == stopButton) { mediaPlayer.pause(); statusTextView.setText("pause called"); startButton.setEnabled(true); } else if (v == startButton) { mediaPlayer.start(); statusTextView.setText("start called"); startButton.setEnabled(false); stopButton.setEnabled(true);

If the MediaPlayer enters into an error state, the onError method will be called on the object that is registered as the MediaPlayer's OnErrorListener. The following onError method shows the various constants that are specified in the MediaPlayer class.

public boolean onError(MediaPlayer mp, int what, int extra) { statusTextView.setText("onError called");

case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK: statusTextView.setText(

"MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra);

"ERROR","MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra); break;

case MediaPlayer.MEDIA_ERROR_SERVER_DIED:

statusTextView.setText("MEDIA ERROR SERVER DIED " + extra); Log.v("ERROR","MEDIA ERROR SERVER DIED " + extra); break;

case MediaPlayer.MEDIA_ERROR_UNKNOWN:

statusTextView.setText("MEDIA ERROR UNKNOWN " + extra); Log.v("ERROR","MEDIA ERROR UNKNOWN " + extra); break;

return false;

When the MediaPlayer completes playback of an audio file, the onCompletion method of the object registered as the OnCompletionListener will be called. This indicates that we could start playback again.

public void onCompletion(MediaPlayer mp) {

statusTextView.setText("onCompletion called");

stopButton.setEnabled(false);

startButton.setEnabled(true);

While the MediaPlayer is buffering, the onBufferingUpdate method of the object registered as the MediaPlayer's onBufferingUpdateListener is called. The percentage of the buffer that is filled is passed in.

public void onBufferingUpdate(MediaPlayer mp, int percent) { bufferValueTextView.setText("" + percent + "%");

When the prepareAsync method finishes, the onPrepared method of the object registered as the OnPreparedListener will be called. This indicates that the audio is ready for playback, and therefore, in the following method, we are setting the startButton to be enabled.

public void onPrepared(MediaPlayer mp) {

statusTextView.setText("onPrepared called"); startButton.setEnabled(true);

Here is the Layout XML associated with the foregoing activity: <?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:text="Status" android:id="@+id/TextView01"^ android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

<TextView android:text="Unknown" android:id="@+id/StatusDisplayTextView"^ android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:text="0%" android:id="@+id/BufferValueTextView"^

android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content"^ android:text="Start" android:id="@+id/StartButton"></Button>

<Button android:layout_width="wrap_content" android:layout_height="wrap_content"^ android:id="@+id/EndButton" android:text="Stop"></Button> </LinearLayout>

As just shown, the MediaPlayer has a nice set of capabilities for handling audio files that are available online via HTTP.

Was this article helpful?

0 0

Responses

Post a comment