- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 AudioManager 在调用电话之前通过听筒播放声音,因此我使用 AudioManager setMode(MODE_IN_CALL)。
我没有遇到任何问题,除了在 Galaxy S3 中,在正确播放音乐后,铃声听起来非常失真且非常嘈杂。我已阅读 setMode 文档:
The audio mode encompasses audio routing AND the behavior of the telephony
layer. Therefore this method should only be used by applications that replace
the platform-wide management of audio settings or the main telephony application.
In particular, the MODE_IN_CALL mode should only be used by the telephony
application when it places a phone call, as it will cause signals from the
radio layer to feed the platform mixer.
所以我怀疑可能有来自 radio 层馈送平台混频器的信号。
所以我使用的代码是这样的:
am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
speakerWasOn = am.isSpeakerphoneOn();
speakerPrevMode = am.getMode();
bthA2dpWasOn = am.isBluetoothA2dpOn();
bthScoWasOn = am.isBluetoothScoOn();
if (BluetoothBR.bthOn && BluetoothBR.conectarBluetooth()) {
am.setMode(AudioManager.STREAM_VOICE_CALL);
am.setBluetoothA2dpOn(true);
am.setBluetoothScoOn(true);
} else {
am.setSpeakerphoneOn(false);
am.setMode(AudioManager.MODE_IN_CALL);
}
然后我使用 MediaPlayer 在单独的线程中播放 .mp3 文件:
private OnPreparedListener opl = new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
try {
mp.setVolume(1.0f, 1.0f);
mp.start();
} catch (Throwable e) {
e.printStackTrace();
mp.release();
}
}
};
private OnCompletionListener ocl = new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
stop();
}
};
public void run() {
synchronized (mp) {
FileInputStream fis = null;
try {
fis = new FileInputStream(getMp3FilePath());
mp.setDataSource(fis.getFD());
mp.setOnPreparedListener(opl);
mp.setOnCompletionListener(ocl);
mp.prepare();
fis.close();
} catch (Exception e) {
mp.release();
if (fis != null) {
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
public void stop() {
if (mp != null) {
try {
mp.stop();
mp.release();
mp.setVolume(0.5f, 0.5f);
mp.reset();
} catch (Exception ignored) {
}
}
if (BluetoothBR.bthOn) {
BluetoothBR.desconectarBluetooth();
}
}
当音乐播放完毕后,我调用:
am.setSpeakerphoneOn(speakerWasOn);
am.setMode(speakerPrevMode);
am.setBluetoothA2dpOn(bthA2dpWasOn);
am.setBluetoothScoOn(bthScoWasOn);
这只发生在 Samsung Galaxy S3 (afaik) 上,并且已经在 S2、Huawei、SonyEricsson 和其他厂商中进行了测试并且可以正常工作。
有什么想法吗?谢谢
更新:
我发现,如果线程在音乐结束后等待 5 秒并将 AudioManager 设置为原始状态,则一切正常。
am.setSpeakerphoneOn(speakerWasOn);
am.setMode(speakerPrevMode);
am.setBluetoothA2dpOn(bthA2dpWasOn);
am.setBluetoothScoOn(bthScoWasOn);
long ctime = System.currentTimeMillis();
while (System.currentTimeMillis() - ctime < 5000);
最佳答案
我遇到了同样的问题。当我注释掉这一行时
am.setMode(AudioManager.MODE_IN_CALL);
一切对我来说都很好。
关于android - AudioManager MODE_IN_CALL 干扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15180257/
每当我使用它时,它都会使我的应用程序崩溃。此外,对于振动模式,当我使用audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE)时,它会打
我正在尝试获取 ring 音量,但它总是给出 0,不管 我将其设置为多少音量(我通过“设置”访问它来做到这一点在电话里)。 audioManager.getstreamVolume(AudioMana
我正在制作一个说出来电者姓名的应用程序,该应用程序在使用 TextToSpeech 说出来电者姓名之前暂停铃声。我正在使用 检测当前的 Volume int musicVolume = audioMa
调用 AudioManager.setRingerMode 有什么区别?调用AudioManager.setStreamMute(AudioManager.STREAM_VOICE_CALL, ...
我正在尝试在通话时启用扬声器: final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AU
AudioManager中有几种流类型.他们在低水平上有何不同?难道是使用例如AudioManager.STREAM_MUSIC 阻止输入麦克风流?还是别的? 最佳答案 一个最显着的区别是音量控制。
我尝试在播放媒体时通过 setMode(mode_in_call) 更改音频模式。所以,我确定我添加了权限 我这样写代码:AudioManager mAudioManager = (AudioMana
我的 getVolume() 方法总是返回 1.0。我已将手机静音,更改了音量,但是当我调用我的方法时,我随时都会收到 1f。我错过了什么吗? btnPlay.setOnClickListener(n
在进行视频聊天的 WebRT 应用程序中,我想将手机设置为使用免提而不是耳机播放声音。 我尝试过使用AppRTCAudioManager(它包装了AudioManager)来设置静音和设置扬声器,直接
通过按后退按钮关闭我的 Activity 后,我面临内存泄漏: 由于我无法弄清楚它是什么,所以我将几乎整个代码注释为: protected void onCreate(Bundle savedInst
安卓AudioManager有方法public void setStreamVolume (int streamType, int index, int flags) . 我不想使用任何标志,因为它们
我在 Activity 中执行以下代码来设置铃声模式和值: void setRinger(int value) { AudioManager manager = (AudioManager)t
当文本到语音系统说出一个短语时,我试图让音频播放器静音。正如预期的那样,audiomanager 在请求音频焦点时降低音量,但在调用 abandonAudioFocus 时,音量不会恢复并且不会调用
我在第 219 行收到空指针异常:amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);。以前没有发生过,今天我第一次看到,我使
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); switch (am.getRingerMode())
当使用以下代码建立电话调用时,我通过附件设置扬声器: audioManager.setMode(AudioManager.MODE_IN_CALL); audioManager.setSpeakerp
在我的 Android 应用程序中,我有一个方法,可以让我调整音量。这就像它应该的那样工作。 public void adjustVolume(int adjustType){ if(myAu
在我正在开发的应用程序中,当前台服务发生更新时,我们必须通过蜂鸣声通知用户。 我们使用这个 foreground service用于录音。要播放哔哔声,我们使用扬声器。我们在播放 beep 之前启用电
我正在构建一个媒体播放器并以类似于文档的方式实现 onAudioFocusChange(): OnAudioFocusChangeListener afChangeListener = new OnA
我正在使用 AudioManager 在调用电话之前通过听筒播放声音,因此我使用 AudioManager setMode(MODE_IN_CALL)。 我没有遇到任何问题,除了在 Galaxy S3
我是一名优秀的程序员,十分优秀!