Skip to content

Instantly share code, notes, and snippets.

@smamran
Last active October 5, 2015 13:21
Show Gist options
  • Select an option

  • Save smamran/5c7e61cc6dce812cac3f to your computer and use it in GitHub Desktop.

Select an option

Save smamran/5c7e61cc6dce812cac3f to your computer and use it in GitHub Desktop.
API Guides Media Playback
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare; create() does that for you
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
// Playing from a remote URL via HTTP streaming looks like this
String url = "http://.........."; // Your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
// mediaPlayer.prepareAsync() // preparing the media in the background
mediaPlayer.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment