在我的应用程序中,有一个使用设备音频插孔的读卡器。现在,我的问题是我想在内置扬声器发出声音时读卡器在设备的HeadPhone插孔中完好无损。安卓耳机插入耳机插孔时通过内置扬声器发出的音频
以下是我已经试过代码:使用setMode方法
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(true);
float maxVolume = mAudioManager
.getStreamMaxVolume(AudioManager.STREAM_RING);
soundPool.play(soundID, maxVolume, maxVolume, 1, 0, 1f);
1)使用反射方法
Class audioSystemClass;
try {
audioSystemClass = Class
.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod(
"setForceUse", int.class, int.class);
// First 1 == FOR_MEDIA, second 1 == FORCE_SPEAKER. To go
// back to the default
// behavior, use FORCE_NONE (0).
setForceUse.invoke(null, 1, 1);
final float maxVolume = mAudioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundPool.play(soundID, maxVolume, maxVolume, 1, 0, 1f);
} catch (ClassNotFoundException e) {
Log.e("Test", e.toString());
e.printStackTrace();
} catch (NoSuchMethodException e) {
Log.e("Test", e.toString());
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.e("Test", e.toString());
e.printStackTrace();
} catch (IllegalArgumentException e) {
Log.e("Test", e.toString());
e.printStackTrace();
} catch (InvocationTargetException e) {
Log.e("Test", e.toString());
e.printStackTrace();
}
2)但是,这些都码在只运行具有默认FM应用程序的设备。但我想在所有设备中具有此功能。
请分享您的经验!
也许[这](http://stackoverflow.com/questions/2917184/forcing-sound-output-through-speaker-in-android)会帮助你,它比你尝试什么略有不同。 – Strider