java蓝牙语音聊天_如何使用蓝牙耳机录制声音

本文介绍了如何在Java中利用蓝牙耳机进行语音聊天,并提供了一段实现蓝牙耳机录音的代码示例,涉及到AudioManager和AudioRecord类的使用,强调了在AndroidManifest.xml中设置正确权限的重要性。
摘要由CSDN通过智能技术生成

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();

我自己偶然发现了这一点,我想指出slott评论的重要性,包括正确的权限,最重要的是要设置

在您的清单文件中 . 如果没有它,您将不会收到任何错误消息,但状态将不会更改为已连接 .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值