Android Tutorial

(avery) #1

By : Ketan Bhimani


72 

The playMusicFromWeb() method should look something like this:

public void playMusicFromWeb() {
try {
Uri file =
Uri.parse(“http://www.perlgurl.org/podcast/archives



  • “/podcasts/PerlgurlPromo.mp3”);
    mp = MediaPlayer.create(this, file);
    mp.start();
    }
    catch (Exception e) {
    Log.e(DEBUG_TAG, “Player failed”, e);
    }
    }


And finally, you want to cleanly exit when the application shuts
down.To do this, you need to override the onStop() method and
stop the MediaPlayer object and release its resources.

The onStop() method should look something like this:

protected void onStop() {
if (mp != null) {
mp.stop();
mp.release();
}
super.onStop();
}


Now, if you run MyFirstAndroidApp in the emulator (and you have
an Internet connection to grab the data found at the URI location),
your application plays the MP3.When you shut down the
application, the MediaPlayer is stopped and released appropriately.

Adding Location-Based Services to Your Application

Your application knows how to say Hello, but it doesn’t know where
it’s located. Now is a good time to become familiar with some
simple location-based calls to get the GPS coordinates.
Free download pdf