首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测Whatsapp调用并暂停ExoPlayer

检测Whatsapp调用并暂停ExoPlayer
EN

Stack Overflow用户
提问于 2017-09-25 05:12:24
回答 1查看 1.8K关注 0票数 2

我正在开发一个安卓音乐播放器应用程序.I只是想在电话到达时暂停,这已经被电话管理器所使用,但我也想检测Whatsapp呼叫和其他互联网呼叫.How来实现这一点。

这是我的代码,应该做哪些更改来检测internet呼叫,还可以暂停播放呼叫和播放呼叫结束。

代码语言:javascript
复制
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);
    }

}
EN

回答 1

Stack Overflow用户

发布于 2017-09-25 08:56:46

我将避免处理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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46397994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档