I'm trying to set up a background music for my app, I've already tried some examples that I found in internet but anyone works, my app doesn't display any background music.
Here is my service class code:
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
class MusicService extends IntentService {
MediaPlayer mediaPlayer;
public MusicService(){
super("MusicService");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mediaPlayer = MediaPlayer.create(this, R.raw.backtrackquiz);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
}
And here is where and how I declare it on my MenuActivity class :
@Override
protected void onStart() {
super.onStart();
if(playIntent == null){
playIntent = new Intent(this, MusicService.class);
startService(playIntent);
}
}