I'm trying to implement the tutorial from this video that implements a player that streams a file on the web: http://www.youtube.com/watch?v=-Xh4zlHoARM but it seems that onStartCommand() function never gets called. Read somewhere else that it may be because the onCreate() function doesn't exit or something like that but not sure what I can do with that information. When I run the app, the activity opens up and upon clicking the play button, nothing occurs.
For debugging, I put a dummy toast message and it works in service onCreate() but never prints anything in onStartCommand() or any of the functions that are subsequently called such as onPrepared, etc which makes me thing the problem is that onStartCommand isn't getting called.
Putting my code for the activity and service. the activity and service are both mentioned
Activity:
package com.example.irfansproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class StreamMusic extends Activity implements OnClickListener {
private Button buttonPlayStop;
Intent serviceIntent;
Boolean boolMusicPlaying = false;
String strAudioLink="cairnomount.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.streammusic);
try{
serviceIntent = new Intent(this,myPlayService.class);
initViews();
buttonPlayStop.setOnClickListener(this);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
buttonPlayStop = (Button) findViewById(R.id.bStreamMusic);
}
private void initViews() {
// TODO Auto-generated method stub
buttonPlayStop = (Button) findViewById(R.id.bStreamMusic);
buttonPlayStop.setBackgroundResource(R.drawable.bplay);
}
/*private void setListeners() {
// TODO Auto-generated method stub
}*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonPlayStopClick();
}
private void buttonPlayStopClick() {
// TODO Auto-generated method stub
if (!boolMusicPlaying) {
buttonPlayStop.setBackgroundResource(R.drawable.bpause);
playAudio();
boolMusicPlaying = true;
} else {
buttonPlayStop.setBackgroundResource(R.drawable.bplay);
stopAudio();
boolMusicPlaying = false;
}
}
private void stopAudio() {
// TODO Auto-generated method stub
try{
stopService(serviceIntent);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void playAudio() {
// TODO Auto-generated method stub
serviceIntent.putExtra("sentAudioLink", strAudioLink);
try{
startService(serviceIntent);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
Service:
package com.example.irfansproject;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.IBinder;
import android.widget.Toast;
public class myPlayService extends Service implements OnCompletionListener,
OnPreparedListener, OnErrorListener, OnSeekCompleteListener, OnInfoListener, OnBufferingUpdateListener {
private MediaPlayer mediaPlayer = new MediaPlayer();
private String sntAudioLink;
@Override
public void onCreate() {
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.reset();
Toast.makeText(this,"poop ",Toast.LENGTH_SHORT).show();
}
public int onSartCommand(Intent intent, int flags, int startId) {
sntAudioLink = intent.getExtras().getString("sentAudioLink");
mediaPlayer.reset();
if (!mediaPlayer.isPlaying()) {
try {
mediaPlayer.setDataSource("http://www.tonycuffe.com/mp3/"+sntAudioLink);
// prepare media player
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// Start Stick return means service will explicitly continue until user
// ends it
return START_STICKY;
}
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
}
@Override
public void onSeekComplete(MediaPlayer mp) {
// TODO Auto-generated method stub
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
switch (what) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
Toast.makeText(this,
"Media Error: Not valid for Progressive Playback " + extra,
Toast.LENGTH_SHORT).show();
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
Toast.makeText(this, "Media Error: Server Died!! " + extra,
Toast.LENGTH_SHORT).show();
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
Toast.makeText(this, "Media Error: Unknown " + extra,
Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
playMedia();
}
private void playMedia() {
// TODO Auto-generated method stub
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
private void stopMedia() {
// TODO Auto-generated method stub
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
stopMedia();
stopSelf();
}
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
return false;
}
}
You misspelled "Start".
public int onSartCommand(Intent intent, int flags, int startId) {
You should be able to add the @Override annotation without an error.