2016-05-30 57 views
0

我正在开发一个简单的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); 
} 
+2

你有没有试过没有'LOGCAT'为空? –

+0

发布异常堆栈跟踪 – m0skit0

回答

0

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

if (keyCode == KeyEvent.KEYCODE_BACK){}