我是Android开发的新手。
我正在创建一个Android应用程序,其中包含一个运行服务的按钮,该服务扩展了Service,使用MediaPlayer播放原始文件夹中的所有音频,因为我需要在后台运行该服务。
问题是,当我在我创建的其他活动(布局)之间切换时,音频会自动停止。
有谁能告诉我如何保持音频播放?
这是我创建的服务的一部分。
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
final MediaPlayer m00 = MediaPlayer.create(this, R.raw.audio_0);
final MediaPlayer m01 = MediaPlayer.create(this, R.raw.audio_1);
final MediaPlayer m02 = MediaPlayer.create(this, R.raw.audio_2);
final MediaPlayer m03 = MediaPlayer.create(this, R.raw.audio_3);
final MediaPlayer m04 = MediaPlayer.create(this, R.raw.audio_4);
final MediaPlayer m05 = MediaPlayer.create(this, R.raw.audio_5);
final MediaPlayer m06 = MediaPlayer.create(this, R.raw.audio_6);
final MediaPlayer m07 = MediaPlayer.create(this, R.raw.audio_7);
final MediaPlayer m08 = MediaPlayer.create(this, R.raw.audio_8);
final MediaPlayer m09 = MediaPlayer.create(this, R.raw.audio_9);
final MediaPlayer m10 = MediaPlayer.create(this, R.raw.audio_10);
final MediaPlayer m11 = MediaPlayer.create(this, R.raw.audio_11);
final MediaPlayer m12 = MediaPlayer.create(this, R.raw.audio_12);
final MediaPlayer m13 = MediaPlayer.create(this, R.raw.audio_13);
final MediaPlayer m14 = MediaPlayer.create(this, R.raw.audio_14);
final MediaPlayer m15 = MediaPlayer.create(this, R.raw.audio_15);
final MediaPlayer m16 = MediaPlayer.create(this, R.raw.audio_16);
final MediaPlayer m17 = MediaPlayer.create(this, R.raw.audio_17);
final MediaPlayer m18 = MediaPlayer.create(this, R.raw.audio_18);
final MediaPlayer m19 = MediaPlayer.create(this, R.raw.audio_19);
final MediaPlayer m20 = MediaPlayer.create(this, R.raw.audio_20);
final MediaPlayer m21 = MediaPlayer.create(this, R.raw.audio_21);
final MediaPlayer m22 = MediaPlayer.create(this, R.raw.audio_22);
final MediaPlayer m23 = MediaPlayer.create(this, R.raw.audio_23);
final MediaPlayer m24 = MediaPlayer.create(this, R.raw.audio_24);
final MediaPlayer m25 = MediaPlayer.create(this, R.raw.audio_25);
final MediaPlayer m26 = MediaPlayer.create(this, R.raw.audio_26);
final MediaPlayer m27 = MediaPlayer.create(this, R.raw.audio_27);
final MediaPlayer m28 = MediaPlayer.create(this, R.raw.audio_28);
final MediaPlayer m29 = MediaPlayer.create(this, R.raw.audio_29);
final MediaPlayer m30 = MediaPlayer.create(this, R.raw.audio_30);
final MediaPlayer m31 = MediaPlayer.create(this, R.raw.audio_31);
final MediaPlayer m32 = MediaPlayer.create(this, R.raw.audio_32);
final MediaPlayer m33 = MediaPlayer.create(this, R.raw.audio_33);
Runnable r = new Runnable()
{
@Override
public void run()
{
m00.start();
m00.setNextMediaPlayer(m01);
m01.setNextMediaPlayer(m02);
m02.setNextMediaPlayer(m03);
m03.setNextMediaPlayer(m04);
m04.setNextMediaPlayer(m05);
m05.setNextMediaPlayer(m06);
m06.setNextMediaPlayer(m07);
m07.setNextMediaPlayer(m08);
m08.setNextMediaPlayer(m09);
m09.setNextMediaPlayer(m10);
m10.setNextMediaPlayer(m11);
m11.setNextMediaPlayer(m12);
m12.setNextMediaPlayer(m13);
m13.setNextMediaPlayer(m14);
m14.setNextMediaPlayer(m15);
m15.setNextMediaPlayer(m16);
m16.setNextMediaPlayer(m17);
m17.setNextMediaPlayer(m18);
m18.setNextMediaPlayer(m19);
m19.setNextMediaPlayer(m20);
m20.setNextMediaPlayer(m21);
m21.setNextMediaPlayer(m22);
m22.setNextMediaPlayer(m23);
m23.setNextMediaPlayer(m24);
m24.setNextMediaPlayer(m25);
m25.setNextMediaPlayer(m26);
m26.setNextMediaPlayer(m27);
m27.setNextMediaPlayer(m28);
m28.setNextMediaPlayer(m29);
m29.setNextMediaPlayer(m30);
m30.setNextMediaPlayer(m31);
m31.setNextMediaPlayer(m32);
m32.setNextMediaPlayer(m33);
}
};
Thread listenThread = new Thread(r);
listenThread.start();
return Service.START_STICKY;
}