我正在编写一个Android应用程序,用于存储和管理带有一些基本元数据和标记的语音备忘录.录音时我使用:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(currentRecordingFileName);
// and so on
Run Code Online (Sandbox Code Playgroud)
这在以正常方式使用手机时效果很好.但是,即使插入耳机,它也不会检测到蓝牙耳机的存在,仍会使用手机自带的麦克风.
我也尝试使用MediaRecorder.AudioSource.DEFAULT,希望它会自动选择正确的源,但根本没有录制声音.
我怎样才能a)检测是否插入蓝牙耳机和/或b)使用蓝牙耳机作为媒体记录器的音频源?
Ste*_*han 11
olivierg基本上是对的(AudioSource仍然可以是MIC),一些基本代码看起来像这样:
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
Log.d(TAG, "Audio SCO state: " + state);
if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
/*
* Now the connection has been established to the bluetooth device.
* Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
* new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
* AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
*
* After finishing, don't forget to unregister this receiver and
* to stop the bluetooth connection with am.stopBluetoothSco();
*/
unregisterReceiver(this);
}
}
}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
Log.d(TAG, "starting bluetooth");
am.startBluetoothSco();
Run Code Online (Sandbox Code Playgroud)
我自己偶然发现了这一点,我想指出slott评论的重要性,包括正确的权限,最重要的是要设置
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Run Code Online (Sandbox Code Playgroud)
在您的清单文件中.如果没有它,您将不会收到任何错误消息,但状态将不会更改为已连接.
根据文档,您需要使用AudioManager.startBluetoothSco()启动 SCO 音频连接,然后您似乎需要使用MediaRecorder.AudioSource.VOICE_CALL。
据我所知,您无法选择特定设备等。这是在系统级别执行的,即在用户将耳机与电话配对之后。
编辑:
正如 Stefan 提到的,AudioSource 需要是 MIC。
VOICE_CALL 似乎不起作用。
您可以像这样检测连接的蓝牙设备:
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不确定如何从耳机而不是常规麦克风进行录音
| 归档时间: |
|
| 查看次数: |
23040 次 |
| 最近记录: |