如何在接到电话时暂停和播放音频流
我在我的应用程序中传输音频。已完成,但当我接到电话时,我应该暂停流,直到呼叫结束,然后再次播放流。在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用音频焦点侦听器。电话状态不需要执行此操作,并且确实是不好的做法(因为该许可完全侵犯隐私)。
它最好的一点是,当用户开始在另一个应用程序中播放音乐或导航试图说些什么时,您还会收到提示。
这里有一个关于如何使用它的好文档:
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
您可以使用
PhoneStateListener
查看手机的状态,并在手机使用时暂停音频流。 android 参考 显示了可以使用的回调,并且 此示例向您展示了如何使用它。请注意,您需要向清单添加权限: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:尝试这个更简单的代码,我已经测试过了......
(完整的代码在这里 http:// blog.kerul.net/2012/01/how-to-pause-audio-while-call-is.html)
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)
在您的活动中
通过重写方法
实现此接口,并在您开始播放音频时发送请求,
这将处理所有消息、通话等。您的音频将自动暂停和启动。
implement this interface in your activity
on override method
and send a request when you have started your audio to playing
this will handle all things messages, calls, etc. your audio will pause and start automatically.