我对android wear的麦克风有疑问。
蓝牙耳机可与Android Wear搭配使用。我的应用发布了VoIP applecation。 当我使用配对的蓝牙耳机播放我的应用程序中的声音(来自网络)时,耳机会播放此声音。但是,当我尝试从麦克风录制语音时。打开了Android Wear的麦克风,而不是耳机。
如何从蓝牙耳机麦克风实现语音读取?
附加Player.class
public class Player {
private static final String TAG = Player.class.getName();
private AudioTrack audioTrack;
private boolean isWorking;
public Player() {
try {
audioTrack = new AudioTrack(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setLegacyStreamType(AudioManager.STREAM_MUSIC)
.build(),
new AudioFormat.Builder()
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(AudioConsts.SAMPLERATE)
.build(),
AudioConsts.GetPlayerBufferSize(),
AudioTrack.MODE_STREAM,
AudioManager.AUDIO_SESSION_ID_GENERATE);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
public void play() {
audioTrack.play();
}
public void stopReading() {
if (!isWorking)
return;
audioTrack.release();
isWorking = false;
}
public void appendForPlayback(byte[] audioMessage, int size) {
new Executor().doInBackground(audioMessage);
}
private class Executor extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... bytes) {
if (bytes != null) {
if (bytes.length > 0) {
byte[] audioMessage = bytes[0];
if (audioMessage.length != 0) {
int written = audioTrack.write(audioMessage, 0, audioMessage.length);
if (written != audioMessage.length) {
Log.d(TAG, "WTF");
}
}
}
}
return null;
}
}}
附加Recorder.class
public class Recorder {
private static final String TAG = Recorder.class.getName();
private boolean isAlive;
private Thread recordThread;
private IRecorderBytesListener listener;
private AudioRecord audioRecord;
public Recorder() {
isAlive = true;
audioRecord = new AudioRecord.Builder()
.setAudioSource(MediaRecorder.AudioSource.MIC)
.setAudioFormat(new AudioFormat.Builder()
.setSampleRate(AudioConsts.SAMPLERATE)
.setEncoding(AudioConsts.ENCODING_PCM_16BIT)
.build())
.setBufferSizeInBytes(AudioConsts.GetRecorderBufferSize())
.build();
//audioRecord.setPreferredDevice(audioDeviceInfo);
recordThread = new Thread(() -> {
ByteBuffer buffer = ByteBuffer.allocateDirect(AudioConsts.GetRecorderBufferSize());
byte[] audioMsg = new byte[AudioConsts.FRAME_SIZE * AudioConsts.ENCODING_PCM_16BIT];
while (isAlive) {
if (audioRecord.getRecordingState() == 1) {
try {
Thread.sleep(50);
} catch (Exception e) {
Log.d(TAG, "hz");
}
continue;
}
buffer = (ByteBuffer) buffer.rewind();
int len = audioRecord.read(buffer, AudioConsts.GetRecorderBufferSize());
if (len != AudioConsts.GetRecorderBufferSize())
Log.d(TAG, "WTF LEN");
len -= AudioConsts.OFFSET_AUDIO_RECORDER;
if (len > 0) {
try {
System.arraycopy(buffer.array(), AudioConsts.OFFSET_AUDIO_RECORDER,
audioMsg, 0, len);
if (listener != null)
listener.bytesReceived(audioMsg, len);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
} else {
Log.d(TAG, "WTF");
}
}
audioRecord.stop();
});
recordThread.start();
}
public void startRecording() {
audioRecord.startRecording();
}
public void stopRecording() {
audioRecord.stop();
}
public void setListener(IRecorderBytesListener listener) {
this.listener = listener;
}
public void dispose() {
isAlive = false;
}}
对不起,我的英语。