我在我的应用中传输音频。它已经完成,但是当我收到一个电话时,我应暂停流,直到呼叫结束,然后再次播放流。是否可以在Android中接收呼叫时暂停和播放流?
答案 0 :(得分:5)
您应该使用Audio Focus侦听器。电话状态不需要这样做,这是非常糟糕的做法(因为许可是完全隐私侵犯)。
关于它的最好的事情是,当用户开始在另一个应用程序中播放音乐或导航试图说些什么时,您还会收到抬头。
这里有一个关于如何使用它的好文档:
http://developer.android.com/training/managing-audio/audio-focus.html
答案 1 :(得分:3)
您可以使用PhoneStateListener
查看手机的状态,并在手机正在使用时暂停音频流。 android reference会向您显示可以使用的回调,this example会向您展示如何使用它。请注意,您需要为清单添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE">
答案 2 :(得分:0)
试试这个更简单的代码,我测试了这个... (完整代码在这里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