停止和放大器;在来电启动音乐 [英] Stopping & Starting music on incoming calls

查看:185
本文介绍了停止和放大器;在来电启动音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了播放媒体从Android的一个网址

I have implemented an Activity that plays media from a URL in android

为了增加暂停功能,当来电是传入我创建了一个接收器,设置AA变量,当电话呼入。活动读取此变量,然后暂停音乐在其的onPause()方法,并复位是当调用完成和活动恢复音乐在其onResume()方法

In order to add pause functionality when the incoming call is incoming I created a receiver that sets a a variable when the call is coming. The activity reads this variable and then pauses the music in its onPause() method and resets is when the call is done and the activity resumes the music in its onResume() method

这工作正常,只要该活动具有焦点。如果我回到主屏幕,而正在播放的音乐,然后将电话打进来,活动的的onPause不叫&放大器;因此,我可以'停止与放大器;启动音乐

This works fine as long the activity has the focus. If I go back to home screen while the music is playing, and then the call comes, the activity's onpause is not called & hence i can' stop & start the music

有什么出路呢?有人实施了一个媒体播放器,这样它拦截传入和放大器;在所有的时间和放大器打出的电话;停止并正常启动音乐?

What is the way out for this? Anybody implemented a media player so that it intercepts incoming & outgoing calls at all the times & stops and starts the music correctly?

推荐答案

有几件事情可以做:

首先,你可以听的使用 PhoneStateListener 更改呼叫状态。 您可以在TelephonyManager注册监听器:

First of all, you can listen for changes in the call state using a PhoneStateListener. You can register the listener in the TelephonyManager:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            //Incoming call: Pause music
        } else if(state == TelephonyManager.CALL_STATE_IDLE) {
            //Not in call: Play music
        } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
            //A call is dialing, active or on hold
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

记住要注销监听器,当它使用不再需要的 PhoneStateListener.LISTEN_NONE

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

欲了解更多信息,请阅读文档

您可以做的另一件事是侦听广播 android.intent.action.PHONE_STATE 。它将包含额外 TelephonyManager.EXTRA_STATE 这会给你的呼叫信息。 <一href="http://developer.android.com/reference/android/telephony/TelephonyManager.html#ACTION_PHONE_STATE_CHANGED">Take一看文档这里。

Another thing you can do is listening for the broadcast android.intent.action.PHONE_STATE. It will contain the extra TelephonyManager.EXTRA_STATE which will give you information about the call. Take a look at the documentation here.

请注意,您所需要的 android.permission.READ_PHONE_STATE -permission在这两种情况下。

Please note that you'll need the android.permission.READ_PHONE_STATE-permission in both cases.

这篇关于停止和放大器;在来电启动音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆