安卓播放声音过sco的蓝牙耳机 [英] Android: Playing sound over Sco Bluetooth headset
问题描述
在过去的几天我一直在试图从我的Android手机播放任何声音在我的SCO蓝牙耳机。我与这个项目的最终目标是最终使车库门开启器,但首先我需要能够播放声音在耳机。
For the past few days I have been trying to play any sound over my sco bluetooth headset from my android phone. My final goal with this project is to eventually make a garage door opener, but first I need to be able to play sound over the headset.
下面是我使用的是当前code的基础上:
Here is the basis of the current code I'm using:
==Manifest==
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
==Code==
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
short[] soundData = new short [8000*20];
for (int iii = 0; iii < 20*8000; iii++) {
soundData[iii] = 32767;
iii++;
soundData[iii] = -32768;
}
audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
8000, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, soundData.length
* Short.SIZE, AudioTrack.MODE_STATIC);
audioTrack.write(soundData, 0, soundData.length);
audioTrack.play();
我运行此之前,我我的配对蓝牙耳机到我的手机,并将它连接。我已经验证它的工作原理是叫我的语音信箱。当我跑不过我的code,无声音来自任何地方。
Before I run this, I pair my bluetooth headset to my phone and have it connected. I have verified it works by calling my voicemail. When I run my code however, no sound comes from anywhere.
下面是不同线路的code的作用:
Here are the effects of the different lines of code:
当我刚跑我的应用程序:
When I'm just running my app:
audioManager.setMode(AudioManager.MODE_IN_CALL);
这行让我所有的声音停止工作,无论我做什么,因此通常注释掉了。
This line makes all of my sound stop working no matter what I do, so it is typically commented out.
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
这两条线使声音停止走出前置扬声器,并让我的耳机点击声和嘘声像它打开,但没有输出。
These two lines make the sound stop coming out of the front speaker and make my headset click and hiss like it's turned on but there's no output.
AudioManager.STREAM_VOICE_CALL
这是我的电话给AudioTrack构造的一部分,但它使相当差。由于这是设置为STREAM_VOICE_CALL声音出来的前置扬声器,如果我设置为STREAM_MUSIC,声音出来的后置扬声器代替。
This is part of my call to the AudioTrack constructor, but it makes quite a difference. Since this is set to STREAM_VOICE_CALL the sound comes out of the front speaker, if I set this to STREAM_MUSIC, the sound comes out the back speaker instead.
当我在通话过程中打开我的应用程序:
When I open my app during a call:
audioManager.setMode(AudioManager.MODE_IN_CALL);
在通话过程中,该行没有任何影响,因为MODE_IN_CALL已被设置。但是,什么是不同的不过是,我的声音与电话的混合,而通常它不会播放。
During a call, this line has no effect because MODE_IN_CALL was already set. But what's different however is that my sound is mixed with the phone call, whereas normally it doesn't play at all.
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
这些,他们的对手断成两半,控制那里的声音是来自哪里。如果我把这些了,我的声音和电话呼叫来自前置扬声器,这些上,该电话来自于我的耳机,我的声音都将丢失。
These, with their counterpart off halves, control where the audio is coming from. If I turn these off, my sound and the telephone call come from the front speaker, with these on, the telephone call comes from my headset and my sound is lost.
至于为什么我的code不工作,我真的不知道。我相信,我已经完成了清单使用<一个href="http://developer.android.com/reference/android/media/AudioManager.html#startBluetoothSco%28%29">startBluetoothSco().
As to why my code is not working, I honestly have no idea. I believe I have fulfilled the checklist for using startBluetoothSco().
Even if a SCO connection is established, the following restrictions
apply on audio output streams so that they can be routed to SCO headset:
- the stream type must be STREAM_VOICE_CALL - the format must be mono -
the sampling must be 16kHz or 8kHz
所以,没有任何人有什么我做错了的想法?有一次,我设法让我的声音通过耳机播放,但它只是一个短暂的提示音时,我忘了停止()我AudioTrack,所以我必须假设它是一个小故障。
So, does anyone have an idea of what I am doing wrong? There was one time that I managed to get my sound to play through the headset, but it was only a short tone when I forgot to stop() my AudioTrack so I have to assume it was a glitch.
推荐答案
其实,我设法解决这个问题。问题是,我打出过耳机我的间距是太高的频率。我简单地加倍了code,创建音频文件,以使其高低高低高高低低,而不是固定的。
I actually managed to fix this. The problem was that my pitch that I played over the headset was too high of a frequency. I fixed it by simply doubling up the code that creates the audio file so that it goes HIGH HIGH LOW LOW instead of HIGH LOW HIGH LOW.
这篇关于安卓播放声音过sco的蓝牙耳机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!