Skip to content Skip to sidebar Skip to footer

Live Streaming Through Mediaplayer In Android

How can I stream an online URL using MediaPlayer?

Solution 1:

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

MediaPlayermediaPlayer=newMediaPlayer();

mediaPlayer.setOnErrorListener(newMediaPlayer.OnErrorListener() {
    publicbooleanonError(MediaPlayer mp, int what, int extra) {
        mp.reset();
        returnfalse;
    }
});

mediaPlayer.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {
    publicvoidonPrepared(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

Solution 2:

Working for me in API 28

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

Please Remember to add permissions to manifest if using INTERNET

<uses-permissionandroid: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

Solution 3:

You may use this:

MediaPlayermediaPlayer= MediaPlayer.create(this, Uri.parse("YOUR URL HERE"));
    mediaPlayer.start();

Solution 4:

publicclassMainActivityextendsActionBarActivity {
    StringvidAddress="your http link";
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VideoViewvidView= (VideoView)findViewById(R.id.myVideo);
        UrividUri= Uri.parse(vidAddress);
        vidView.setVideoURI(vidUri);
        MediaControllervidControl=newMediaController(this);
        vidControl.setAnchorView(vidView);
        vidView.setMediaController(vidControl);
        vidView.start();


    }


    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        returntrue;
    }

    @OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.intid= item.getItemId();

        //noinspection SimplifiableIfStatementif (id == R.id.action_settings) {
            returntrue;
        }

        returnsuper.onOptionsItemSelected(item);
    }
}

Solution 5:

The MediaPlayer is buggy if you are streaming audio from remote URL. It works sometimes but hangs mostly for remote.

Have tried below which hangs

   mediaPlayer = MediaPlayer.create(this, Uri.parse("YOUR URL HERE"));
        mediaPlayer.start();

also tried mediaPlayer.prepareAsync(); and implement onprepared listener. Doesnt work.. Device used is Samsung galaxy mini

Have been trying to read the URL content manually and update the MediaPlayer datasource with the buffered content. Works moslty, but yet to figure out how to switch URLs etc..

The example here is for VideoPlayer but should be same concept for audio player as well

http://davanum.wordpress.com/2007/12/29/android-videomusic-player-sample-from-local-disk-as-well-as-remote-urls/

Edit

Ah just found this http://launch-code.blogspot.co.uk/2012/01/android-play-audio-asyncplayer.html Seems to wrap the calls to MediaPlayer nicely : http://www.netmite.com/android/mydroid/frameworks/base/media/java/android/media/AsyncPlayer.java

Post a Comment for "Live Streaming Through Mediaplayer In Android"