How to Play the online streaming radio in Android

2019-04-08 21:56发布

I am developing one application where i want to play live stream radio. I have an url using which i will stream the radio and play. I have a play button by clicking which i want to play the radio. For that, i have written some code which is not at all working. Here is my code:

mp = new MediaPlayer();
try {

    mp.setOnPreparedListener(this);
    Log.d("Testing", "start111");
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    String url="xxxxxx";
    mp.setDataSource(url);
    mp.prepareAsync();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
    Log.d("Testing", "Exception ::: 1111 "+e.getMessage());
} catch (IllegalStateException e) {
    Log.d("Testing", "Exception ::: 2222 "+e.getMessage());
    e.printStackTrace();
} catch (IOException e) {
    Log.d("Testing", "IOException ::: 3333 "+e.getMessage());
    e.printStackTrace();
}

Can anyone please help me??

3条回答
Root(大扎)
2楼-- · 2019-04-08 22:23

Try this.

public class RadioStream extends Activity {

    private final static String stream = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p";
    Button play;
    MediaPlayer mediaPlayer;
    boolean started = false;
    boolean prepared = false;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_stream);

        play = (Button) findViewById(R.id.play);
        play.setEnabled(false);
        play.setText("Loading..");
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (started) {
                    mediaPlayer.pause();
                    started = false;
                    play.setText("Play");
                } else {
                    mediaPlayer.start();
                    started = true;
                    play.setText("Pause");
                }

            }
        });

        new PlayTask().execute(stream);
    }

    @Override
    protected void onPause() {
        super.onPause();
       /* if(started)
            mediaPlayer.pause();*/

    }

    @Override
    protected void onResume() {
        super.onResume();
        /*if(started)
            mediaPlayer.start();*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // mediaPlayer.release();
    }

    private class PlayTask extends AsyncTask<String, Void, Boolean> {

        @Override
        protected Boolean doInBackground(String... strings) {

            try {
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.prepare();
                prepared = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            play.setEnabled(true);
            play.setText("Play");

        }
    }
}
查看更多
Deceive 欺骗
3楼-- · 2019-04-08 22:25

Please do try the code below and call the given method at the on create of your activity or at the onclick listener of your button. Remember to handle the stop and start button the imgV is an imageView for my button.

 private MediaPlayer player;
private void startMediaPlayer() {
    String url = "http:yoururl.com"; // your URL here
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
    } catch (IllegalArgumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (SecurityException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IllegalStateException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if(isPlaying){
        try {
            mediaPlayer.prepareAsync();
            progress.setVisibility(View.VISIBLE);
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
    }
    mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
        }
    });
}

boolean isPlaying = true;
 private void startPlaying() {
    isPlaying = true;
    mediaPlayer.prepareAsync();
    mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
        }
    });
    imgV.setImageResource(R.drawable.stop);
}

private void stopPlaying() {
    if (mediaPlayer.isPlaying()) {
        isPlaying = false;
        mediaPlayer.stop();
        mediaPlayer.release();
        initializeMediaPlayer();
    }
    imgV.setImageResource(R.drawable.play);
}
查看更多
再贱就再见
4楼-- · 2019-04-08 22:27

You can find good information regarding radio streaming. Github radio streaming example

and also there is a question in SOF which can also be helpful Stackoverflow radio streaming example

Hope it will help. thanks

查看更多
登录 后发表回答