0

我有一个蓝牙耳机(BTC6White)。我想对着麦克风说话,Android 设备会播放声音。

那么,我该怎么做呢?首先,我可以建立一个套接字连接

socket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString("0000111e-0000-1000-8000-00805f9b34fb"));
socket.connect();

那么,我如何获得音频?这个方法有什么用:startBuetoothSco?要将音频放入扬声器,我应该使用 Auditrack 吗?

int buffersize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT);
soundData = new byte [buffersize*5];    
audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 
   8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT, 
   soundData.length,AudioTrack.MODE_STREAM);

但是,那么,我应该填充缓冲区 soundData 吗?如何?使用套接字(进入线程)?

  mmInStream = socket.getInputStream();
  public void run() {
  byte[] buffer = new byte[8000*2]; 
      while (true) {
           bytes = mmInStream.read(buffer);
            audioTrack.write(buffer, 0, bytes); //write directly?
      }}

startBuetoothSco() 是为了什么?了解 sco 国家?SCO_AUDIO_STATE_CONNECTED... 还是发送/接收数据?我不明白如何从耳机获取音频数据,然后如何流式传输到扬声器。是否需要建立SCO连接(使用AudioManager)才能获取蓝牙耳机的数据?

很难找到有关此问题的信息,并且 android 文档很差(本主题)。

4

1 回答 1

0

要检索音频数据,您可以设置流源。例如,如果你想通过蓝牙耳机录制声音,你可以初始化 AudioRecord 和 AudioManager 如下:

AudioRecord ar = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    RECORDER_SAMPLERATE, 
                    RECORDER_OUT_CHANNELS,
                    RECORDER_AUDIO_ENCODING,
                    bufferSize);
ar.startRecording();

并打电话

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.startBluetoothSCO();

然后,您可以定义一个线程来读取音频数据,您可以在其中编写以下代码:

private Runnable run_record = new Runnable(){

    @Override
    public void run() {
        // TODO Auto-generated method stub
        short dataBuf[] = new short[bufferSize];
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file_path);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        while(isRecording){
            ar.read(dataBuf, 0, bufferSize);
            Log.d(LOG_TAG, "writing "+bufferSize+" bytes of data to file "+file_path);
            byte data[] = short2byte(dataBuf);
            try {
                os.write(data, 0, bufferSize);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }};

我希望我解释清楚。

于 2013-07-24T05:35:26.910 回答