user1051599
user1051599

Reputation: 371

Live streaming through mediaplayer in android

How can I stream an online URL using MediaPlayer?

Upvotes: 4

Views: 23839

Answers (5)

user3826696
user3826696

Reputation: 1135

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

Upvotes: 3

public class MainActivity extends ActionBarActivity {
    String vidAddress ="your http link";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VideoView vidView = (VideoView)findViewById(R.id.myVideo);
        Uri vidUri = Uri.parse(vidAddress);
        vidView.setVideoURI(vidUri);
        MediaController vidControl = new MediaController(this);
        vidControl.setAnchorView(vidView);
        vidView.setMediaController(vidControl);
        vidView.start();


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Upvotes: 0

Navdroid
Navdroid

Reputation: 1549

You may use this:

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

Upvotes: 1

William Seemann
William Seemann

Reputation: 3530

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

Upvotes: 6

MountainRock
MountainRock

Reputation: 594

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

Upvotes: 0

Related Questions