Android 服务和背景音乐代码-更新-

Android 服务和背景音乐代码-更新-,android,service,background-music,Android,Service,Background Music,我正在制作一个有背景音乐的问答游戏。我在这里看到了一个使用服务和背景音乐的非常有用的源代码,但我的问题是我想在我的EasyOne.class的正确答案按钮中添加另一个声音。我将添加或编辑什么以使其工作。我的观点是。每次我点击easyone.class的a按钮时,它都会播放一个声音,如果我想关闭所有音乐,即使是Correct按钮和背景音乐,如果我转到我的settings.class并单击off按钮,它也会关闭。希望你能帮我。真的很感激 这是我的服务代码 public class MusicServ

我正在制作一个有背景音乐的问答游戏。我在这里看到了一个使用服务和背景音乐的非常有用的源代码,但我的问题是我想在我的EasyOne.class的正确答案按钮中添加另一个声音。我将添加或编辑什么以使其工作。我的观点是。每次我点击easyone.class的a按钮时,它都会播放一个声音,如果我想关闭所有音乐,即使是Correct按钮和背景音乐,如果我转到我的settings.class并单击off按钮,它也会关闭。希望你能帮我。真的很感激

这是我的服务代码

public class MusicService extends Service {
private static final String TAG = "";
MediaPlayer player;


@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "ON", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    player = MediaPlayer.create(this, R.raw.bsound);
    player.setLooping(false);
}

@Override
public void onDestroy() {
    Toast.makeText(this, "OFF", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    player.stop();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "ON", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
    player.start();
}
}

这是我的设置

public class Settings extends Activity implements OnClickListener {
  private static final String TAG = "";
  Button on, off, back;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
    back = (Button) findViewById(R.id.btn_backk);
    on = (Button) findViewById(R.id.btn_on);
    off = (Button) findViewById(R.id.btn_off);
    back.setOnClickListener(new View.OnClickListener() {
        @Override   
       public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Back",
                    Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getApplicationContext(),MainMenu.class);
            startActivity(intent);
    }
 });    
    on.setOnClickListener(this);
    off.setOnClickListener(this);
  }

  public void onClick(View src) {
    switch (src.getId()) {
    case R.id.btn_on:
      Log.d(TAG, "onClick: starting srvice");
      startService(new Intent(this, MusicService.class));
      break;
    case R.id.btn_off:
      Log.d(TAG, "onClick: stopping srvice");
      stopService(new Intent(this, MusicService.class));
      break;
    }
  }
}
我的EasyOne.class-(我的1级简易模式的班级)


在代码的主要活动(游戏实际开始的地方)中,放置:

startService(新意图(MusicService.class))
这将在游戏开始后立即开始播放音乐

当然-在EasyOne的onStart方法中,只需使用与您服务中相同(或几乎相同)的音频播放代码,例如
MediaPlayer=MediaPlayer.create(This,R.raw.othersound);player.setLooping(假);player.start()我只是复制并粘贴到我的easoyone.class上?
public class EasyOne extends Activity {

ImageButton a, b, c;
Intent intent ;
CountDownTimer cdt;
TextView timer;
MediaPlayer clap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.easyone);
    a = (ImageButton) findViewById(R.id.ib_a);
    b = (ImageButton) findViewById(R.id.ib_b);
    c = (ImageButton) findViewById(R.id.ib_c);
    timer = (TextView) findViewById(R.id.tv_timer);
    cdt = new CountDownTimer(5000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {
               timer.setText("seconds remaining: " + millisUntilFinished / 1000);

        }

        @Override
        public void onFinish() {
              timer.setText("TIMES UP!");
            intent = new Intent(getApplicationContext(),TimesUp.class);
            startActivity(intent);

        }
    };

    intent = new Intent(getApplicationContext(),ChoiceTwo.class);
    a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(intent);
          //i want to add a sound here that will play if they click this button and can turn it OFF on the setting.class OFF button
            cdt.cancel();
            Intent intent = new Intent(getApplicationContext(),ChoiceTwo.class);
            startActivity(intent);


        }
    });


    cdt.start();
}
}
startService(new Intent(MusicService.class))