How to play online radio in android

…衆ロ難τιáo~ 提交于 2019-12-17 22:36:44

问题


I am working on android application in which i have play online radio streaming. i have gone through the media player classes but i don't think is there any method to online streaming of radio. If any know about this please help me.

Thank You. Vikram


回答1:


Vikram,

You should be able to achieve this using the MediaPlayer; however, depending on your format it may be difficult. For example, if you're trying to play an online radio stream that uses .pls, or .m3u, you would have to parse that file, and pull out the true URLs to use.

Beyond that, you should be able to use MediaPlayer's create method with a URL to start streaming playback. Keep in mind that if the streams URL redirects (which it likely does) you may have to resolve the URL. A simple way to do this is use HttpURLConnection to open a connection, then connect(), then getURL(). You'll likely need a string url, so call toExternalForm() on the result from getURL().

Additionally, If things aren't working for you with MediaPlayer via URL, you might have to come up with your own buffering mechanism to get the data from the server. That being the case, you can try this tutorial: http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/




回答2:


From what I've read, you should just be able to do:

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(streamingURL);
mediaPlayer.prepare();
mediaPlayer.start();

to get basic functionality I believe, but I haven't tested it myself.




回答3:


the easiest way to play a radio channel in android is to use the built in MediaPlayer, however when the datasource is from web the prepare() method takes a long time to execute and you should use prepareAsync() instead to avoid blocking the ui:

    player = new MediaPlayer();
    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
                player.start();
            }
        }
    });

    try {
        player.setDataSource(currentChannelUrl);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    } catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        return;
    }
    player.prepareAsync();


来源:https://stackoverflow.com/questions/4625547/how-to-play-online-radio-in-android

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