I'm developing an app that plays a background music. The music is managed by a service that runs in background. In the Main activity, in the onCreate() method, the service starts with:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Background music service.
Intent musicServiceIntent = new Intent(this, BackgroundSoundService.class);
startService(musicServiceIntent);
final BackgroundSoundService bss = new BackgroundSoundService();
buttonStart = (Button) v.findViewById(R.id.button_start);
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bss.onStart();
}
});
buttonPause = (Button) v.findViewById(R.id.button_pause);
buttonPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bss.onPause();
}
});
buttonStop = (Button) v.findViewById(R.id.button_stop);
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bss.onStop();
}
});
}
}
}
The service is the following code: BackgroundSoundService.java
public class BackgroundSoundService extends Service {
private static final String TAG = null;
static MediaPlayer mMediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.background_music);
mMediaPlayer.setVolume(1.0f, 1.0f);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
playMusic();
}
});
playMusic();
}
public void playMusic() {
mMediaPlayer.start();
}
/**
*
* @param arg0 -
* @return -
*/
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
/**
*
*/
public void onStart() {
Log.d("A", "onStart()");
if(!mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is not playing");
playMusic();
} else {
Log.d("A", "mMediaPlayer is playing");
}
}
/**
*
*/
public void onPause() {
Log.d("A", "onPause()");
if(mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is playing");
mMediaPlayer.pause();
} else {
Log.d("A", "mMediaPlayer is not playing");
}
}
/**
*
*/
public void onStop() {
Log.d("A", "onStop()");
if(mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is playing");
mMediaPlayer.stop();
} else {
Log.d("A", "mMediaPlayer is not playing");
}
}
/**
*
*/
@Override
public void onDestroy() {
Log.d("A", "onDestroy()");
mMediaPlayer.stop();
mMediaPlayer.release();
}
/**
*
*/
@Override
public void onLowMemory() {
}
}
Now, MainActivity has three buttons to control the music. SART Button, starts music, PAUSE Button, put the sound in pause and STOP Button, stops music. Each one executes the methos onStart(), on Pause() and onStop() in the BackgroundSoundService. The problem is if I press the START Button after the STOP Button, the log show me this error:
E/MediaPlayer: start called in state 0
E/MediaPlayer: error (-38, 0)
I try solve this implementing:
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
But did not work. Any suggestions? Thank you.