8

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??

Jonas
  • 121,568
  • 97
  • 310
  • 388
Arindam Mukherjee
  • 2,255
  • 11
  • 48
  • 83

3 Answers3

6

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

Community
  • 1
  • 1
Sandeep
  • 2,573
  • 3
  • 21
  • 28
4

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

        }
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Yogesh Srivastava
  • 311
  • 1
  • 4
  • 8
  • I've edited your answer to fix the code formatting, but you need to provide some comments or something so that people will have some idea of what you're doing. Posting just a block of code is, in itself, not a good answer. – Wai Ha Lee Nov 24 '16 at 14:37
  • hii, I tried this everything is fine loading will be done it will change to play, When i click on play it will be changed to pause but no sound or streaming is not going on, could you help me to solve this – Madhusudhan Aradya Jun 10 '20 at 06:11
0

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);
}
Iyke Perry
  • 341
  • 4
  • 2