Android 录音和播放+扬声器作为一个选项
我正在为Android 编写一个sip 应用程序。在通话过程中,我必须录制麦克风输入并播放传入的音频。必须可以选择使用免提电话。它必须兼容 Android 1.5+(1.5、1.6、2.0、2.1、2.2、未来版本)并且设备便携。
好的。因此,我使用 AudioTrack 播放传入音频,使用 AudioRecord 记录来自麦克风的数据,使用 AudioManager.setSpeakerphoneOn() 启用或禁用扬声器。
听起来很简单,但它并不像应有的那么简单。除非调用 AudioManager.setMode(AudioManager.MODE_IN_CALL),否则 AudioManager.setSpeakerphoneOn(false) 不起作用。所以我必须处于 MODE_IN_CALL 状态。
仍然可以,简单,它可以在我的 Android 1.6 G1、旧版三星手机、模拟器、许多我们的客户设备等上运行。但它并不在任何地方都可以运行:(
在三星平板电脑 GT-P1000 上,我得到 0, 0, 0 的流, 0,...作为麦克风输入。我认为摩托罗拉手机上也有同样的问题(客户抱怨)。经过一些测试,我意识到这是由 AudioManager.setMode(AudioManager.MODE_IN_CALL) 引起的,这会导致麦克风无法工作。但我必须调用它,否则我无法禁用扬声器。
是否有一种简单的方法可以使其简单、正确且工作?如果可能的话,没有像 Sipdroid 这样高度危险的源代码:
if (RtpStreamReceiver.samsung) {
AudioManager am = (AudioManager) Receiver.mContext.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setMode(AudioManager.MODE_NORMAL);
}
或者
void initMode() {
samsung = Build.MODEL.contains("SAMSUNG") || Build.MODEL.contains("SPH-") ||
Build.MODEL.contains("SGH-") || Build.MODEL.contains("GT-");
if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL &&
(Receiver.pstn_state == null || Receiver.pstn_state.equals("IDLE"))) {
setMode(AudioManager.MODE_NORMAL);
}
非常感谢。 扬
I am programming a sip application for Android. During a call I must record the microphone input and play the incoming audio. There must be an optional using of speakerphone. It must be Android 1.5+ (1.5, 1.6, 2.0, 2.1, 2.2, future versions) compatible and device portable.
OK. So I am using AudioTrack to play incoming audio, AudioRecord to record data from the microphone and AudioManager.setSpeakerphoneOn() to enable or disable speakerphone.
Sounds simple but it is not as simple as it should be. AudioManager.setSpeakerphoneOn(false) does not work unless AudioManager.setMode(AudioManager.MODE_IN_CALL) is called. So I have to be in MODE_IN_CALL.
Still OK and simple and it works on my G1 with Android 1.6, older Samsung phone, emulators, many our customer devices etc. But it does not work everywhere :(
On Samsung tablet GT-P1000 I get stream of 0, 0, 0, 0, ... as microphone input. I think the same problem is on Motorola phones (customers complain). After some test I realised that it is caused by AudioManager.setMode(AudioManager.MODE_IN_CALL). This causes that microphone does not work on some devices. But I have to call it because otherwise I can not disable speakerphone.
Is there a simple way how to make it simple, correct and working? And if possible without hell like Sipdroid highly dangerous source code:
if (RtpStreamReceiver.samsung) {
AudioManager am = (AudioManager) Receiver.mContext.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setMode(AudioManager.MODE_NORMAL);
}
or
void initMode() {
samsung = Build.MODEL.contains("SAMSUNG") || Build.MODEL.contains("SPH-") ||
Build.MODEL.contains("SGH-") || Build.MODEL.contains("GT-");
if (Receiver.call_state == UserAgent.UA_STATE_INCOMING_CALL &&
(Receiver.pstn_state == null || Receiver.pstn_state.equals("IDLE"))) {
setMode(AudioManager.MODE_NORMAL);
}
Thanks a lot
Jan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下........
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Try it out........
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);