2014-04-11 36 views
0

我被困在Store中發佈應用程序的最後階段,我在Android中開發了一個遊戲應用程序,我面臨的問題很奇怪,我沒有使用任何遊戲引擎,App開始正常,然後按菜單>遊戲開始>音樂服務開始>音樂暫停onPause()按播放按鈕,但當Power按鈕停止正常音樂> pattren鎖定屏幕出現時,解鎖手機>「遊戲屏幕出現但沒有音樂「>當按此按鈕時,在動作欄上有聲音按鈕按此按鈕可恢復音樂,但不會從屏幕模式解鎖時開始,按下電源按鈕和解鎖屏幕後音樂無法啓動

注意:奇怪的是,它只發生在三星(WVGA)和很少有人喜歡HTC,但沒有在我測試的其他手機上使用。

請幫忙!

public class GameJungleFight extends SherlockActivity implements ServiceConnection{ 
private MusicService mServ; 
private boolean mIsBound = false; 
protected void onCreate(Bundle savedInstanceState) { 
     //Music Service Binding 
    Intent music = new Intent(this, MusicService.class); 
    startService(music); 
    doBindService(); 
} 
// interface connection with the service activity 
    public void onServiceConnected(ComponentName name, IBinder binder) 
    { 
    mServ = ((MusicService.ServiceBinder) binder).getService(); 
    } 

    public void onServiceDisconnected(ComponentName name) 
    { 
    mServ = null; 
    } 

    // local methods used in connection/disconnection activity with service. 

    public void doBindService() 
    { 
    // activity connects to the service. 
     Intent intent = new Intent(this, MusicService.class); 
    bindService(intent, this, Context.BIND_AUTO_CREATE);   
    mIsBound = true; 

    } 
     public void doUnbindService() 
    { 
    // disconnects the service activity. 
    if(mIsBound) 
    { 
    unbindService(this); 
      mIsBound = false; 
    } 
    } 
public void onResume() { 
     super.onResume();   

    } 

     // music pause when phone calls,sms/mms or hold key activated 
    @Override 
    protected void onPause() {   
     super.onPause(); 
     if (mServ != null){ 
      mServ.pause(); 
      if (isFinishing()){ 
       mServ.pause(); 
       mServ.resume(); 
      }  
     } 
    } 
public void onBackPressed() { 
     finish(); 
     } 
    public void onRestart(){   
      super.onRestart(); 
      mServ.resume(); 
     } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     //adding menu to action bar     
    menu.add("SoundOff") 
    .setOnMenuItemClickListener(this.SoundOffButtonClickListener) 
    .setIcon(R.raw.sound_on) 
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 
     return super.onCreateOptionsMenu(menu); 
} 
OnMenuItemClickListener SoundOffButtonClickListener = new OnMenuItemClickListener() { 
     boolean isPlaying = false; 
     public boolean onMenuItemClick(MenuItem item) { 
      if (isPlaying) { 
       mServ.resume();    
       }else{ 
        mServ.pause();   
       } 
       isPlaying = !isPlaying;  
      // mServ.stop(); 
      return true; 
     } 
    };  

}

+0

你不應該在'onResume()'恢復音樂播放? –

+0

我嘗試了,但它崩潰了APP – Mohtashim

+0

拋出了什麼錯誤/異常? –

回答

1

我最後加入以下代碼,測試它真實的設備以及解決我的問題。

1)首先問題是音樂沒有恢復在遊戲活動時解鎖屏幕,正如我所說的(測試設備Samsung GS2,GS3和HTC Gratia)通過使用以下代碼。

通過使用上面的代碼它解決了解鎖屏幕和音樂服務恢復的問題。它實際上檢查屏幕窗口是否焦點或沒有,然後我測試 SGSII,III,宏達電,QMobile工作完美,因爲我想,

2)但是,當我測試索尼Experia Tipo,音樂開始即使在手機鎖定屏幕窗口(這在上述手機型號中沒有發生)我添加了以下代碼,並再次重新測試以及上述型號的每一件固定事項。我用

//Pause Music when Power button Press Lock/Unlock Screen 
    public void onRestart(){   
      super.onRestart(); 
      if (mServ != null){ 
       mServ.pause(); 
       if (isFinishing()){ 
        mServ.pause(); 
        mServ.resume(); 
       } 
      } 
     } 

音樂服務根據我的項目是從鏈路MusicService By AlexSiva很少modifcation,感謝亞歷克斯對他的回答和建議 希望它能幫助任何一個面臨的問題likeme,在測試了SGS,HTC,索尼Tipo,像一個​​魅力工作:)

相關問題