2017-09-25 119 views
2

我正在開發一個android音樂播放器應用程序。我只是想暫停當一個電話到達,這已經通過使用電話管理器,但我也想檢測WhatsApp電話和其他互聯網電話。如何做到這一點
這是我的代碼更改,應採取什麼措施也發現互聯網通話暫停呼叫播放器和電話上播放結束檢測WhatsApp電話和暫停ExoPlayer

private void player_manager_on_call() { 
    // TODO Auto-generated method stub 
    // Manage player on Call 

    phoneStateListener = new PhoneStateListener() { 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      if (state == TelephonyManager.CALL_STATE_RINGING) 
      // Incoming // call: // Pause // music 
      { 

       try { 

        boolean is_playing = mediaController.Check_IsPlaying(); 
        if (is_playing) { 
         Log.d("xiomi", "CALL_STATE_RINGING"); 

         // Get details of song from sp 

         position = PlayerConstants.SONG_NUMBER; 

         data = PlayerConstants.SONGS_LIST.get(position); 

         Play_link = data.getPath(); 
         Artist = data.getArtist() + "-" + data.getAlbum(); 
         albumArt = UtilFunctions.getAlbumart(
           getApplicationContext(), data.getAlbumId()); 
         Log.e("albumArt", "albumArt " + albumArt); 

         mediaController.doPauseResume(); 
         showControls(); 

         // start notification service again for play 

         startNotificationService(MainActivity.this, 
           data.getTitle(), Artist, albumArt, 
           is_playing, position); 

        } 
       } catch (Exception e) { // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } else if (state == TelephonyManager.CALL_STATE_IDLE) // Not in 
                    // //call: 
                    // // 
                    // Play 
                    // // 
                    // music 

       try { 

        if (player != null) { 
         Log.d("xiomi", "CALL_STATE_RINGING"); 

         // Get details of song from sp 
         position = PlayerConstants.SONG_NUMBER; 

         data = PlayerConstants.SONGS_LIST.get(position); 

         Play_link = data.getPath(); 
         Artist = data.getArtist() + "-" + data.getAlbum(); 
         albumArt = UtilFunctions.getAlbumart(
           getApplicationContext(), data.getAlbumId()); 
         Log.e("albumArt", "albumArt " + albumArt); 
         boolean is_playing = mediaController 
           .Check_IsPlaying(); 
         mediaController.doPauseResume(); 
         showControls(); 

         // start notification service again for play 

         startNotificationService(MainActivity.this, 
           data.getTitle(), Artist, albumArt, 
           is_playing, position); 

        } 
       } catch (Exception e) { // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      else if (state == TelephonyManager.CALL_STATE_OFFHOOK) // A // 
                    // call 
      // is // dialing, // active // or // on // hold 
      { 

       try { 

        boolean is_playing = mediaController.Check_IsPlaying(); 
        if (is_playing) { 
         Log.d("xiomi", "CALL_STATE_OFFHOOK"); 

         // Get details of song from sp 

         position = PlayerConstants.SONG_NUMBER; 

         data = PlayerConstants.SONGS_LIST.get(position); 

         Play_link = data.getPath(); 
         Artist = data.getArtist() + "-" + data.getAlbum(); 
         albumArt = UtilFunctions.getAlbumart(
           getApplicationContext(), data.getAlbumId()); 
         Log.e("albumArt", "albumArt " + albumArt); 

         mediaController.doPauseResume(); 
         showControls(); 

         // start notification service again for play 

         startNotificationService(MainActivity.this, 
           data.getTitle(), Artist, albumArt, 
           is_playing, position); 

        } 
       } catch (Exception e) { // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
      super.onCallStateChanged(state, incomingNumber); 
     } 
    }; 

    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
    if (mgr != null) { 
     mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); 
    } 

} 
+0

whatsapp調用可能是應用程序的內部狀態,它可能不會被捕獲爲全局CALL狀態。 –

+1

只要您的活動失去焦點,就會觸發'onPause',因此您可以在'onPause'中處理此問題嗎? –

+0

@sunilsunny'onPause'方法不能處理這個,因爲如果我們點擊手機的home按鈕,那麼這個方法也會被調用,我的播放器也應該是一個後臺播放器 –

回答

0

我會避免應對代碼Android文檔,所以我只是給你鏈接應該回答你的問題。

首先,您不需要自己檢測來自不同應用程序的呼叫,只需要註冊廣播接收器並對音頻焦點更改進行操作即可。

  1. 在這裏,你可以檢查出來怎麼耳機時,斷開處理: https://developer.android.com/guide/topics/media-apps/volume-and-earphones.html

  2. ,在鏈路下面你可以看到你的問題如何處理呼叫等 https://developer.android.com/guide/topics/media-apps/audio-focus.html

  3. 答案
+0

這是好的,但我用這個,我需要處理whatsapp和skype調用 –