Live streaming through mediaplayer in android

余生长醉 提交于 2019-12-09 06:56:56

问题


Can any body help how to stream a live url in android mediaplayer...i tried such a long but same result....plz help if any body did before


回答1:


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




回答2:


You may use this:

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



回答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);
    }
}



回答4:


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




回答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



来源:https://stackoverflow.com/questions/8178040/live-streaming-through-mediaplayer-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!