在Android游戏中按下音量控制

时间:2016-05-30 20:27:09

标签: java android

我正在开发一款简单的Android游戏,可以通过服务在后台播放声音。比赛开始时,声音很好。但是当按下音量增大键或向下按钮时,应用程序会立即退出。

public class PlayAudio extends Service{
    private static final String LOGCAT = null;
    MediaPlayer objPlayer;

    public void onCreate(){
        super.onCreate();
        Log.d(LOGCAT, "Service Started!");
        objPlayer = MediaPlayer.create(this,R.raw.relax);
    }

    public int onStartCommand(Intent intent, int flags, int startId){
        objPlayer.start();
        Log.d(LOGCAT, "Media Player started!");
        if(objPlayer.isLooping() != true){
            Log.d(LOGCAT, "Problem in Playing Audio");
        }
        return 1;
    }

    public void onStop(){
        objPlayer.stop();
        objPlayer.release();
    }

    public void onPause(){
        objPlayer.stop();
        objPlayer.release();
    }

    public void onDestroy(){
        objPlayer.stop();
        objPlayer.release();
    }

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

此代码开始播放声音

public void playAudio() {
    Intent objIntent = new Intent(this, PlayAudio.class);
    startService(objIntent);
}

1 个答案:

答案 0 :(得分:0)

问题解决了。我错过了这个条件:

if (keyCode == KeyEvent.KEYCODE_BACK){}