How to play online radio in android

后端 未结 3 951
无人及你
无人及你 2020-12-13 11:03

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 on

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 11:39

    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();
    

提交回复
热议问题