如何在接到电话时暂停和播放音频流

发布于 2024-12-20 22:43:46 字数 80 浏览 0 评论 0原文

我在我的应用程序中传输音频。已完成,但当我接到电话时,我应该暂停流,直到呼叫结束,然后再次播放流。在android中接听电话时可以暂停和播放流吗?

I stream audio in my app. It's done but when I receive a call I should pause the stream until the call is finished, and then play the stream again. Is it possible to pause and play the stream while receiving a call in android?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷雾森÷林ヴ 2024-12-27 22:43:46

您应该使用音频焦点侦听器。电话状态不需要执行此操作,并且确实是不好的做法(因为该许可完全侵犯隐私)。

它最好的一点是,当用户开始在另一个应用程序中播放音乐或导航试图说些什么时,您还会收到提示。

这里有一个关于如何使用它的好文档:

http://developer .android.com/training/managing-audio/audio-focus.html

You should use Audio Focus listeners. Phone state is NOT required to do this and is really bad practice (because the permission is totally privacy invasive).

The best thing about it is that you'll also receive a heads up when the user starts playing music in another app or when navigation is trying to say something.

There is a good doc on how to use it here:

http://developer.android.com/training/managing-audio/audio-focus.html

作死小能手 2024-12-27 22:43:46

您可以使用 PhoneStateListener 查看手机的状态,并在手机使用时暂停音频流。 android 参考 显示了可以使用的回调,并且 此示例向您展示了如何使用它。请注意,您需要向清单添加权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE">

You can use the PhoneStateListener to see the status of the phone, and pause your audio stream when the phone is in use. The android reference shows you the callbacks you can use, and this example shows you how to use it. Note that you'll need to add a permission to your manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE">
反话 2024-12-27 22:43:46

尝试这个更简单的代码,我已经测试过了......
(完整的代码在这里 http:// blog.kerul.net/2012/01/how-to-pause-audio-while-call-is.html

public class UrClassActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    private MediaPlayer mp;
    ...
    ...
    ...
    //this is a code segmentation
    //THis two lines of codes need to be inserted to the onCreate method
    //This following codes dealing with the phone-state
    //detect voice incoming call
    //onCallStateChanged method will be executed if there's incoming
    //or outcoming call
    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                //INCOMING call
                //do all necessary action to pause the audio
                if(mp!=null){//check mp
                    setPlayerButton(true, false, true);

                    if(mp.isPlaying()){ 

                        mp.pause();
                    }
                }

            } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                //Not IN CALL
                //do anything if the phone-state is idle
            } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                //A call is dialing, active or on hold
                //do all necessary action to pause the audio
                //do something here
                if(mp!=null){//check mp
                    setPlayerButton(true, false, true);

                    if(mp.isPlaying()){ 

                        mp.pause();
                    }
                }
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };//end PhoneStateListener

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

}//end onCreate

}//end class

Try this simpler code, I've tested this...
(complete code is here http://blog.kerul.net/2012/01/how-to-pause-audio-while-call-is.html)

public class UrClassActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    private MediaPlayer mp;
    ...
    ...
    ...
    //this is a code segmentation
    //THis two lines of codes need to be inserted to the onCreate method
    //This following codes dealing with the phone-state
    //detect voice incoming call
    //onCallStateChanged method will be executed if there's incoming
    //or outcoming call
    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                //INCOMING call
                //do all necessary action to pause the audio
                if(mp!=null){//check mp
                    setPlayerButton(true, false, true);

                    if(mp.isPlaying()){ 

                        mp.pause();
                    }
                }

            } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                //Not IN CALL
                //do anything if the phone-state is idle
            } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                //A call is dialing, active or on hold
                //do all necessary action to pause the audio
                //do something here
                if(mp!=null){//check mp
                    setPlayerButton(true, false, true);

                    if(mp.isPlaying()){ 

                        mp.pause();
                    }
                }
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };//end PhoneStateListener

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

}//end onCreate

}//end class
我只土不豪 2024-12-27 22:43:46

在您的活动中

AudioManager.OnAudioFocusChangeListener

通过重写方法

override fun onAudioFocusChange(focusChange: Int) {
    if (focusChange <= 0) {
        //pause music
        
        mediaPlayer!!.pause()

    } else {
         //play music
        
        mediaPlayer!!.start()

    }

实现此接口,并在您开始播放音频时发送请求,

requestAudioFocus(
        this,
        AudioManager.STREAM_MUSIC,
        AudioManager.AUDIOFOCUS_GAIN
    )

这将处理所有消息、通话等。您的音频将自动暂停和启动。

implement this interface in your activity

AudioManager.OnAudioFocusChangeListener

on override method

override fun onAudioFocusChange(focusChange: Int) {
    if (focusChange <= 0) {
        //pause music
        
        mediaPlayer!!.pause()

    } else {
         //play music
        
        mediaPlayer!!.start()

    }

and send a request when you have started your audio to playing

requestAudioFocus(
        this,
        AudioManager.STREAM_MUSIC,
        AudioManager.AUDIOFOCUS_GAIN
    )

this will handle all things messages, calls, etc. your audio will pause and start automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文