通过蓝牙耳机捕获音频与Android设备配对 [英] Capture Audio through Bluetooth Headset paired with Android Device
本文介绍了通过蓝牙耳机捕获音频与Android设备配对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图捕捉到从蓝牙耳机的音频与Android设备配对。
I am trying to capture audio from a Bluetooth Headset paired with an Android Device.
以下是有关code:
Intent in=null;
final int bufferSize=BufferElements2Rec*BytesPerElement;
final BroadcastReceiver brr=new BroadcastReceiver()
{
@Override
public void onReceive(Context context,Intent intent)
{
int state=intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,-1);
Log.d(labelApp,"Audio SCO State = "+state);
if(AudioManager.SCO_AUDIO_STATE_CONNECTED==state)
{
Log.d(labelApp,"Entered and Starting Recording");
//recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
// RECORDER_SAMPLERATE, RECORDER_CHANNELS,
// RECORDER_AUDIO_ENCODING, bufferSize);
recorder = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, bufferSize);
if(recorder==null)
{
Log.d(labelApp,"null");
}
else
{
Log.d(labelApp,"not null");
}
recorder.startRecording();
recordingThread=new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
writeAudioDataToFile();
}
},"AudioRecorder Thread");
recordingThread.start();
Log.d(labelApp,"Launched Recording Thread");
}
}
};
try
{
Log.d(labelApp,"Initializing BT");
am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
//am.setMode(AudioManager.MODE_IN_CALL);
//in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
//in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
Log.d(labelApp,"Starting Bluetooth");
am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
am.setBluetoothScoOn(true);
am.setMode(AudioManager.MODE_IN_CALL);
am.startBluetoothSco();
Log.d(labelApp,"Can BT record from mic? "+am.isBluetoothScoAvailableOffCall());
//in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
// The following line makes the audio go to hell
//am.setMode(AudioManager.MODE_IN_CALL);
//am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
Log.d(labelApp,"Everything initializated");
Log.d(labelApp,"Recorder is...");
}
catch(Exception e)
{
Log.e(labelApp,"exception",e);
writeStack(e);
}
try
{
Log.d(labelApp,"Initializing BT");
am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
//am.setMode(AudioManager.MODE_IN_CALL);
//in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
//in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
Log.d(labelApp,"Starting Bluetooth");
am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
am.setBluetoothScoOn(true);
am.setMode(AudioManager.MODE_IN_CALL);
am.startBluetoothSco();
Log.d(labelApp,"Can BT record from mic? "+am.isBluetoothScoAvailableOffCall());
//in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
// The following line makes the audio go to hell
//am.setMode(AudioManager.MODE_IN_CALL);
//am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
Log.d(labelApp,"Everything initializated");
Log.d(labelApp,"Recorder is...");
}
catch(Exception e)
{
Log.e(labelApp,"exception",e);
writeStack(e);
}
舱单问权限:
- WRITE_EXTERNAL_STORAGE
- RECORD_AUDIO
- 网络
- 许可权
- BROADCAST_STICKY
- 蓝牙
- BLUETOOTH_ADMIN
每个应用的典型筛选LogCat中的输出是:
The typical Filtered LogCat output per the app is:
- 初始化BT
- 启动蓝牙
- 可以从话筒BT记录?真正
- 一切初始化
- 刻录...
- 音频SCO状态= 2
- 音频SCO状态= 1
- 进入并开始录音
- 不为空
- 启动录制主题
当am.startBluetoothSco();被调用时,我能听到BT设备上的短暂的噪音,但随后的应用程序只是获取来自Android设备的麦克风音频到位BT的一个。
When the am.startBluetoothSco(); is invoked, I can hear a brief noise on the BT device, but then the app simply gets the audio from the Android Device's mic in place of the BT's one.
这是我丢失/做错了任何暗示?
Any hint on what am I missing/doing wrong?
先谢谢您的关注。
推荐答案
code到声音从蓝牙耳机录制
code to voice recording from bluetooth headset
public class Recording {
static int count = 0;
static String Shared;
static String bFlag;
public static int TIMEOUT = 5000;
public static int COUNTDOWN_INTERVAL = 1000;
static Context context;
public static void checkAndRecord(Context context,
OnBluetoothRecording BluetoothRecording, boolean resume) {
// Check bluetooth flag And Bluetooth is ON or OFF
if (getBluetoothFlag(context) && isBluetoothON()) {
// Check for bluetooth and Record
startBluetoothRecording(BluetoothRecording, resume, context);
} else {
// If Bluetooth is OFF Show Toast else Dont Show
if (getBluetoothFlag(context) && !isBluetoothON()) {
// false because recording not started
Toast.makeText(context,
"Bluetooth is OFF. Recording from Phone MIC.",
Toast.LENGTH_SHORT).show();
BluetoothRecording.onStartRecording(resume, false);
} else {
// false because recording not started
BluetoothRecording.onStartRecording(resume, false);
}
}
}
private static void startBluetoothRecording(
final OnBluetoothRecording BluetoothRecording,
final boolean resume, Context context) {
// TODO Auto-generated method stub
final int MAX_ATTEPTS_TO_CONNECT = 3;
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
final CountDownTimer timer = getTimer(BluetoothRecording, audioManager,
resume);
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(
AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
// cancel Timer
timer.cancel();
context.unregisterReceiver(this);
// pass through and true because
// recording from bluetooth so set 8000kHz
BluetoothRecording.onStartRecording(resume, true);
} else if (AudioManager.SCO_AUDIO_STATE_DISCONNECTED == state) {
if (count > MAX_ATTEPTS_TO_CONNECT) {
context.unregisterReceiver(this);
// Stop BluetoothSCO
audioManager.stopBluetoothSco();
// reset Counter
count = 0;
// stop timer
timer.cancel();
// false because still recording not started
BluetoothRecording.onStartRecording(resume, false);
} else {
// Increment Disconnect state Count
count++;
}
}
}
}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
// Start the timer
timer.start();
audioManager.startBluetoothSco();
}
// set the Timeout
private static CountDownTimer getTimer(
final OnBluetoothRecording BluetoothRecording,
final AudioManager audioManager, final boolean resume) {
// TODO Auto-generated method stub
return new CountDownTimer(TIMEOUT, COUNTDOWN_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
// Do Nothing
}
@Override
public void onFinish() {
// stopBluetoothSCO() and start Normal Recording
audioManager.stopBluetoothSco();
// false because recording button is already clicked but still
// not recording.
BluetoothRecording.onStartRecording(resume, false);
}
};
}
// Return's the bluetooth state
private static boolean isBluetoothON() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
return bluetoothAdapter.isEnabled();
}
// Return's the bluetoothFlag state
private static boolean getBluetoothFlag(Context context) {
// shared pref
SharedPreferences sp = context.getSharedPreferences(Shared,
Context.MODE_PRIVATE);
return sp.getBoolean(bFlag, false);
}
}
这篇关于通过蓝牙耳机捕获音频与Android设备配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文