如何在 Android 中从配对的蓝牙音频设备(即 Moster Clarity Bluetooth Speaker)录制语音。
我已经在 Android 中与设备配对,我想通过设备上的麦克风录制声音(而不是使用手机的内置麦克风)。
这是我用来录制的代码:
try {
isRecording = true;
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (file == null) {
File rootDir = Environment.getExternalStorageDirectory();
file = File.createTempFile(PREFIX, EXTENSION, rootDir);
}
recorder.setOutputFile(file.getAbsolutePath());
recorder.prepare();
recorder.start();
timDown = new RecordCountDown(10000, 1000);
timDown.start();
} catch (Exception e) {
Log.i("Error Message", "Error Message :" + e.getMessage());
}
我该怎么做?
最佳答案
试试这个代码可能对你有帮助..
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();
关于android - 在 Android 中从蓝牙音频设备录制音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14455797/