HTTP Audio Playback

The simplest case to explore would simply be to play an audio file that lives online and is accessible via HTTP.

One such file would be this, which is available on my server: http://www.mobvcasting.com/android/audio/goodmorningandroid.mp3

Here is an example activity that uses the MediaPlayer to illustrate how to play audio available via HTTP.

package com.apress.proandroidmedia.ch06.audiohttp;

import java.io.IOException;

import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log;

public class AudioHTTPPlayer extends Activity { MediaPlayer mediaPlayer;

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

When our activity is created, we do a generic instantiation of a MediaPlayer object by calling the MediaPlayer constructor with no arguments. This is a different way of using the MediaPlayer than we have previously seen and requires us to take some additional steps before we can play the audio.

mediaPlayer = new MediaPlayer();

Specifically, we need to call the setDataSource method, passing in the HTTP location of the audio file we would like to play. This method can throw an IOException, so we have to catch and deal with that as well.

mediaPlayer.setDataSource(

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

Following that we call the prepare method and then the start method, after which the audio should start playing.

mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) {

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

Running this example, you will probably notice a significant lag time from when the application loads to when the audio plays. The length of the delay is due to the speed of the data network that the phone is using for its Internet connection (among other variables).

If we add Log or Toast messages throughout the code, we would see that this delay happens between the call to the prepare method and the start method. During the running of the prepare method, the MediaPlayer is filling up a buffer so that the audio playback can run smoothly even if the network is slow.

The prepare method actually blocks while it is doing this. This means that applications that use this method will likely become unresponsive until the prepare method is complete. Fortunately, there is a way around this, and that is to use the prepareAsync method. This method returns immediately and does the buffering and other work in the background, allowing the application to continue.

The issue then becomes one of paying attention to the state of the MediaPlayer object and implementing various callbacks that help us keep track of its state.

To get a handle on the various states that a MediaPlayer object may be in, it is helpful to look over the diagram from the MediaPlayer page on the Android API Reference, shown in Figure 6-1.

reset()

Was this article helpful?

0 0

Post a comment