我正在尝试创建一个应用程序,以使用 Nearby 通信 API 将音频从一台设备中的本地文件流式传输到另一台设备。问题是我设法流式传输音频,但我在远程设备上唯一能听到的是某种无意义的破裂噪音。到目前为止,我读到的是我需要调整我正在使用的 minBufferSize 中的值和 sampleRate 中的值,但我一直在尝试这个并且我没有取得太多成就。
这是我发送字节块的代码:
AudioTrack speaker;
//Audio Configuration.
private int sampleRate = 16000; //How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
minBufSize=2048;
speaker = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, 10*minBufSize, AudioTrack.MODE_STREAM);
}
final InputStream file;
final byte [] arrayStream = new byte [minBufSize];
try {
file= new FileInputStream (filepath);
bytesRead = file.read(arrayStream);
while (bytesRead!=-1) {
new Thread(new Runnable() {
public void run() {
sendMessage(arrayStream);
}
}).start();
bytesRead = file.read(arrayStream);
}
Toast.makeText(this, "Mensaje totalmente completado",Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
private void sendMessage(byte [] payload) {
Nearby.Connections.sendReliableMessage(mGoogleApiClient, mOtherEndpointId, payload);
//mMessageText.setText(null);
}
这是在远程设备上接收和播放消息的代码:
@Override
public void onMessageReceived(String endpointId, byte[] payload, boolean isReliable) {
// A message has been received from a remote endpoint.
Toast.makeText(this,"Mensaje recibido",Toast.LENGTH_SHORT).show();
debugLog("onMessageReceived:" + endpointId + ":" + new String(payload));
playMp3(payload);
}
private void playMp3(final byte[] mp3SoundByteArray) {
if (isPlaying==false){
speaker.play();
isPlaying=true;
}else{
//sending data to the Audiotrack obj i.e. speaker
speaker.write(mp3SoundByteArray, 0, minBufSize);
Log.d("VR", "Writing buffer content to speaker");
}
}
谁能帮我这个??
谢谢!