Reputation: 1896
I have a web radio stream which i want to implement using MediaPlayer as a service the Main Activity has 2 buttons, play & stop which set the service as the intent then start or stop the service depending on the button
RadioPlayer service
public class RadioPlayer extends Service implements MediaPlayer.OnPreparedListener{
public static String ACTION_PLAY = "ACTION_PLAY";
private MediaPlayer mediaplayer = null;
private boolean isPlaying = false;
public int onStartCommand(Intent intent, int flags, int startId){
Log.d("SERVICE", "Started");
if(intent.getBooleanExtra(ACTION_PLAY, false)){
play();
}
return Service.START_STICKY;
}
private void play(){
Log.d("SERVICE", "play()");
if(!isPlaying){
isPlaying = true;
try{
Log.d("SERVICE", "1");
mediaplayer = new MediaPlayer();
Log.d("SERVICE", "2");
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.d("SERVICE", "3");
mediaplayer.setDataSource("http://X.X.X.X");
Log.d("SERVICE", "4");
mediaplayer.prepareAsync();
Log.d("SERVICE", "5");
} catch(Exception e){
e.printStackTrace();
}
}
}
public void onDestory(){
Log.d("SERVICE", "Stop");
stop();
}
private void stop(){
Log.d("SERVICE", "stop()");
if(isPlaying){
isPlaying = false;
if(mediaplayer != null){
mediaplayer.stop();
mediaplayer.release();
mediaplayer = null;
}
//stopForeground(true);
}
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("SERVICE", "onPrepared()");
mediaplayer.start();
//i have also attempted mp.start() with no cigar
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
onPrepared(MediaPlayer mp) does not get called (& i have waited a good amount of time for the stream to load up). Have i missed something that i would need for a service to successfully implement onPreparedListener?
In terms of Log cat output, the only thing that occurs when executed that i don't recognize is AwesomePlayer saying the data source is supressed...?
06-19 16:45:04.090: D/pre-SERVICE(22731): clicked()
06-19 16:45:04.090: D/SERVICE(22731): Started
06-19 16:45:04.090: D/SERVICE(22731): play()
06-19 16:45:04.090: D/SERVICE(22731): 1
06-19 16:45:04.110: W/qdhwcomposer(327): Excessive delay reading vsync: took 66 ms
06-19 16:45:04.110: D/SERVICE(22731): 2
06-19 16:45:04.110: D/SERVICE(22731): 3
06-19 16:45:04.110: I/AwesomePlayer(331): setDataSource_l(URL suppressed)
06-19 16:45:04.110: D/SERVICE(22731): 4
06-19 16:45:04.110: D/SERVICE(22731): 5
06-19 16:45:04.190: W/qdhwcomposer(327): Excessive delay reading vsync: took 49 ms
UPDATE After trying it with just .onPrepare i have managed to get the service to work, however if i were to do this async style, the question still stands
Upvotes: 0
Views: 1236
Reputation: 1896
It seem's this issue is subjective. i did manage to get sucsessful stream asynchronously using this code
mediaplayer.prepareAsync();
mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp)}
mp.start();
} });
Upvotes: 0
Reputation: 71
It's seems you don't set your onCompletionListener with your player.
Try to add mediaplayer.setOnCompletionListener(this)
after mediaplayer.prepareAsync()
.
Upvotes: 1