Search code examples
androidhttp-live-streamingshoutcast

How to play shoutcast streaming in android 2.2


i am struggling to play an shoutcast link(.pls) i succeed with mp3 and i came to know shoutcast link is not working in android. is there any possibility to play this link in android http://stream.radiosai.net:8020/

private final static String RADIO_STATION_URL = "http://stream.radiosai.net:8002/";
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private Button buttonRecord;
private Button buttonStopRecord;
private MediaPlayer player;
private InputStream recordingStream;
private RecorderThread recorderThread;
private boolean isRecording = false;

/** 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() {
    playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
    playSeekBar.setMax(100);
    playSeekBar.setVisibility(View.INVISIBLE);

    buttonPlay = (Button) findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(this);

    buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
    buttonStopPlay.setEnabled(false);
    buttonStopPlay.setOnClickListener(this);

    buttonRecord = (Button) findViewById(R.id.buttonRecord);
    buttonRecord.setOnClickListener(this);

    buttonStopRecord = (Button) findViewById(R.id.buttonStopRecord);
    buttonStopRecord.setOnClickListener(this);
}

public void onClick(View v) {
    if (v == buttonPlay) {
        startPlaying();
    } else if (v == buttonStopPlay) {
        stopPlaying();
    } else if (v == buttonRecord) {
        recorderThread = new RecorderThread();
        recorderThread.start();

        buttonRecord.setEnabled(false);
        buttonStopRecord.setEnabled(true);
    } else if (v == buttonStopRecord) {
        stopRecording();
    }
}

private void startPlaying() {
    buttonStopPlay.setEnabled(true);
    buttonPlay.setEnabled(false);

    playSeekBar.setVisibility(View.VISIBLE);

    player.prepareAsync();

    player.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            player.start();
            player.pause();
            buttonRecord.setEnabled(true);
        }
    });

}

private void stopPlaying() {
    if (player.isPlaying()) {
        player.stop();
        player.release();
        initializeMediaPlayer();
    }

    buttonPlay.setEnabled(true);
    buttonStopPlay.setEnabled(false);
    playSeekBar.setVisibility(View.INVISIBLE);
    buttonRecord.setEnabled(false);
    buttonStopRecord.setEnabled(false);
    stopRecording();
}

private void initializeMediaPlayer() {
    player = new MediaPlayer();
    try {
        player.reset();
        player.setDataSource(RADIO_STATION_URL);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    if (player.isPlaying()) {
        player.stop();
    }
}

private void startRecording() {

    BufferedOutputStream writer = null;
    try {
        URL url = new URL(RADIO_STATION_URL);
        URLConnection connection = url.openConnection();
        final String FOLDER_PATH = Environment
                .getExternalStorageDirectory().getAbsolutePath()
                + File.separator + "Songs";

        File folder = new File(FOLDER_PATH);
        if (!folder.exists()) {
            folder.mkdir();
        }

        writer = new BufferedOutputStream(new FileOutputStream(new File(
                FOLDER_PATH + File.separator + "listen.pls")));
        recordingStream = connection.getInputStream();

        final int BUFFER_SIZE = 100;

        byte[] buffer = new byte[BUFFER_SIZE];

        while (recordingStream.read(buffer, 0, BUFFER_SIZE) != -1
                && isRecording) {
            writer.write(buffer, 0, BUFFER_SIZE);
            writer.flush();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            recordingStream.close();
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private void stopRecording() {
    buttonStopRecord.setEnabled(false);
    buttonRecord.setEnabled(true);
    try {
        isRecording = false;
        if (recordingStream != null) {
            recordingStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private class RecorderThread extends Thread {
    @Override
    public void run() {
        isRecording = true;
        startRecording();
    }

};
}

Solution

  • Try this code, detail is here:

    import android.app.Activity;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    public class SimpleMusicStream extends Activity implements
      MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
      MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener {
    
     private String TAG = getClass().getSimpleName();
     private MediaPlayer mp = null;
    
     private Button play;
     private Button pause;
     private Button stop;
    
     @Override
     public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);
    
      play = (Button) findViewById(R.id.play);
      pause = (Button) findViewById(R.id.pause);
      stop = (Button) findViewById(R.id.stop);
    
      play.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
        play();
       }
      });
    
      pause.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
        pause();
       }
      });
    
      stop.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
        stop();
       }
      });
     }
    
     private void play() {
      Uri myUri = Uri.parse("http://stream.radiosai.net:8002/");
      try {
       if (mp == null) {
        this.mp = new MediaPlayer();
       } else {
        mp.stop();
        mp.reset();
       }
       mp.setDataSource(this, myUri); // Go to Initialized state
       mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
       mp.setOnPreparedListener(this);
       mp.setOnBufferingUpdateListener(this);
    
       mp.setOnErrorListener(this);
       mp.prepareAsync();
    
       Log.d(TAG, "LoadClip Done");
      } catch (Throwable t) {
       Log.d(TAG, t.toString());
      }
     }
    
     @Override
     public void onPrepared(MediaPlayer mp) {
      Log.d(TAG, "Stream is prepared");
      mp.start();
     }
    
     private void pause() {
      mp.pause();
     }
    
     private void stop() {
      mp.stop();
    
     }
    
     @Override
     public void onDestroy() {
      super.onDestroy();
      stop();
    
     }
    
     public void onCompletion(MediaPlayer mp) {
      stop();
     }
    
     public boolean onError(MediaPlayer mp, int what, int extra) {
      StringBuilder sb = new StringBuilder();
      sb.append("Media Player Error: ");
      switch (what) {
      case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
       sb.append("Not Valid for Progressive Playback");
       break;
      case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
       sb.append("Server Died");
       break;
      case MediaPlayer.MEDIA_ERROR_UNKNOWN:
       sb.append("Unknown");
       break;
      default:
       sb.append(" Non standard (");
       sb.append(what);
       sb.append(")");
      }
      sb.append(" (" + what + ") ");
      sb.append(extra);
      Log.e(TAG, sb.toString());
      return true;
     }
    
     public void onBufferingUpdate(MediaPlayer mp, int percent) {
      Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
     }
    
        }
    

    main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <Button
      android:text="Play"
      android:id="@+id/play"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></Button>
     <Button
      android:text="Pause"
      android:id="@+id/pause"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></Button>
     <Button
      android:text="Stop"
      android:id="@+id/stop"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></Button>
    </LinearLayout>