4

How can I stream an online URL using MediaPlayer?

Zoe
  • 27,060
  • 21
  • 118
  • 148
user1051599
  • 371
  • 2
  • 7
  • 18
  • 1
    "i tried such a long but same result" - what result? – user370305 Nov 18 '11 at 05:23
  • 1
    please show what you have tried so far. – Lalit Poptani Nov 18 '11 at 05:23
  • Hi Lalit u can see what i tried on this below heading on stack overflow... Android http live Streaming URL using mediaplayer – user1051599 Nov 18 '11 at 05:55
  • Getting errors like Mediaplayer error(1,-1002), start state is 0 and error(-38, 0) why.? – user1051599 Nov 18 '11 at 05:56
  • If you're using http live streaming (HLS/pantos-http-live-streaming-07), it's only supported in Android 3.0 and higher. You can see google's release notes here: http://developer.android.com/sdk/android-3.0-highlights.html You need to provide more information in your question... – vipw Nov 18 '11 at 09:03

5 Answers5

6

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

William Seemann
  • 3,440
  • 10
  • 44
  • 78
3

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

user3826696
  • 1,045
  • 12
  • 13
1

You may use this:

    MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse("YOUR URL HERE"));
    mediaPlayer.start();
Fostah
  • 11,398
  • 10
  • 46
  • 55
Navdroid
  • 1,541
  • 3
  • 25
  • 52
0

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

MountainRock
  • 594
  • 1
  • 9
  • 24
0
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);
    }
}