Skip to content

Instantly share code, notes, and snippets.

@omaraflak
Last active April 24, 2016 23:57
Show Gist options
  • Save omaraflak/4b842154471a3e7882c20a776990ac5c to your computer and use it in GitHub Desktop.
Save omaraflak/4b842154471a3e7882c20a776990ac5c to your computer and use it in GitHub Desktop.
MediaPlayer Android from URL
// DON'T FORGET IN MANIFEST: <uses-permission android:name="android.permission.INTERNET" />
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("http://server.com/music.mp3");
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment