Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live streaming through mediaplayer in android

How can I stream an online URL using MediaPlayer?

like image 438
user1051599 Avatar asked Dec 21 '22 06:12

user1051599


2 Answers

Basically, you need to do the following if you are using the Android MediaPlayer class:

MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    public boolean onError(MediaPlayer mp, int what, int extra) {
        mp.reset();
        return false;
    }
});

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }
});

try {
    mediaPlayer.setDataSource("http://someurl");
    mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
} catch (IOException e) {
}

Keep in mind the Android MediaPlayer class will only play supported formats: http://developer.android.com/guide/appendix/media-formats.html

like image 130
William Seemann Avatar answered Dec 28 '22 10:12

William Seemann


Working for me in API 28

   MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource("http://209.188.21.202:8016/stream");
            mediaPlayer.prepare();
            mediaPlayer.start();

Please Remember to add permissions to manifest if using INTERNET

    <uses-permission android:name="android.permission.INTERNET" />

Also careful with usesCleartextTraffic

Starting with Android 9.0 (API level 28), cleartext support is disabled by default.

In my case I add --> android:usesCleartextTraffic="true" in manifest

Reference -->

Android 8: Cleartext HTTP traffic not permitted

like image 28
user3826696 Avatar answered Dec 28 '22 09:12

user3826696