可以使用以下代码播放后台音频:
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 = MediaPlayer.create(this, R.raw.yoursoundfile); player.setLooping(true); player.setVolume(100,100);
} public int onStartCommand(Intent intent, int flags, int startId) { player.start(); return startId; } public IBinder onUnBind(Intent arg0) { // TODO Auto-generated method stub return null; }
public void onStop() { player.stop(); player.release(); }
public void onPause() { player.stop(); player.release(); }
@Override public void onDestroy() { player.stop(); player.release(); }
@Override public void onLowMemory() {
} }
public class AlarmReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { Intent service1 = new Intent(context, BackgroundSoundService.class); context.startService(service1); }
}