I made a service that play background music, but when the music finished I want to replay it again. Which method can I use in my service?
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
player开发者_JAVA技巧 = MediaPlayer.create(this, R.raw.idil);
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TODO
}
public IBinder onUnBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}
Just use MediaPlayer.setLooping()
method.
Sample code:
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.idil);
player.setVolume(100,100);
player.setLooping(true);
}
Set OnCompletionListener via setOnCompletionListener. And it he callback you can start it again.
精彩评论