+ Reply to Thread
Results 1 to 2 of 2

Thread: Streaming radio on android

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    30

    Streaming radio on android

    I am trying to create an application that stream a radio via URL , how can i do that ?

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    27
    Okay inside Java use that :
    Code:
    import android.app.Activity;
    import android.os.Bundle;
    import java.io.IOException;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnBufferingUpdateListener;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;
    
    public class myMain extends Activity implements OnClickListener {
    
        private ProgressBar pbar;
    
        private Button play;
    
        private Button stop;
    
        private MediaPlayer mplayer;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            initializeUIElements();
    
            initializeMediaPlayer();
        }
    
        private void initializeUIElements() {
    
            pbar = (ProgressBar) findViewById(R.id.progressBar1);
            pbar.setMax(100);
            pbar.setVisibility(View.INVISIBLE);
    
            play = (Button) findViewById(R.id.play);
            play.setOnClickListener(this);
    
            stop = (Button) findViewById(R.id.stop);
            stop.setEnabled(false);
            stop.setOnClickListener(this);
    
        }
    
        public void onClick(View v) {
            if (v == play) {
                startPlaying();
            } else if (v == stop) {
                stopPlaying();
            }
        }
    
        private void startPlaying() {
            stop.setEnabled(true);
            play.setEnabled(false);
    
            pbar.setVisibility(View.VISIBLE);
    
            mplayer.prepareAsync();
    
            mplayer.setOnPreparedListener(new OnPreparedListener() {
    
                public void onPrepared(MediaPlayer mp) {
                    mplayer.start();
                }
            });
    
        }
    
        private void stopPlaying() {
            if (mplayer.isPlaying()) {
                mplayer.stop();
                mplayer.release();
                initializeMediaPlayer();
            }
    
            play.setEnabled(true);
            stop.setEnabled(false);
            pbar.setVisibility(View.INVISIBLE);
        }
    
        private void initializeMediaPlayer() {
            mplayer = new MediaPlayer();
            try {
                mplayer.setDataSource("http://Website or IP:PORT");
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            mplayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
    
                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    pbar.setSecondaryProgress(percent);
                    Log.i("Buffering", "" + percent);
                }
            });
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            if (mplayer.isPlaying()) {
                mplayer.stop();
            }
        }
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts