Android Tutorial

(avery) #1
Android Tutorial 71

Important MediaPlayer and URI Parsing Methods


To add MP3 playback support to MyFirstAndroidApp, edit the file
MyFirst AndroidApp .java. First, you must add the appropriate
import statements for the MediaPlayer class.


import android.media.MediaPlayer;
import android.net.Uri;
Next, within the MyFirstAndroidApp class, declare a member
variable for your MediaPlayer object.


private MediaPlayer mp;


Now, create a new method called playMusicFromWeb() in your
class and make a call to this method in your onCreate()
method.The playMusicFromWeb() method creates a valid Uri
object, creates a MediaPlayer object, and starts the MP3 playing. If
the operation should fail for some reason, the method logs a
custom error with your logging tag.

Free download pdf