0
我通過藍牙套接字連接發送音頻文件到其他設備。我複製字節的字節,所以在另一端的大小是完全一樣的。 但是,當我試圖在另一端播放文件時,我只聽到尷尬的噪音(每次我啓動播放器時聽起來有點不同)。通過藍牙流複製後損壞的音頻文件
現在奇怪的部分:只要我複製「腐敗」文件到另一個位置(通過一些安卓文件管理器),我可以播放它,它聽起來非常好!
任何想法?
發送:
outputStream = mSocket.getOutputStream();
long totalLength = file.length();
String command = Protocol.COMMAND_SEND_FILE + Protocol.SEPARATOR + file.getName() + Protocol.SEPARATOR + totalLength;
outputStream.write(command.getBytes());
outputStream.flush();
long bytesWritten = 0;
while ((c = is.read(buffer, 0, buffer.length)) > 0) {
outputStream.write(buffer, 0, c);
outputStream.flush();
bytesWritten += c;
}
mLogger.log("sent total of bytes", bytesWritten+"");
is.close();
接收:
while (mRunning) {
final StringBuilder sb = new StringBuilder();
if (!mReceiveFile) {
bytesRead = mInputStream.read(buffer);
if (bytesRead != -1) {
String result = "";
while ((bytesRead == bufferSize) && (buffer[bufferSize] != 0)) {
result = result + new String(buffer, 0, bytesRead);
bytesRead = mInputStream.read(buffer);
}
result = result + new String(buffer, 0, bytesRead);
sb.append(result);
}
} else {
if (mLogger != null) {
mLogger.log("receiving file", mReceiveFilename);
}
int c = 0;
long bRead = 0;
OutputStream oos = new FileOutputStream(Utils.getAppRootDir() + "/" + mReceiveFilename);
while (bRead < mReceiveFileLength && (c = mInputStream.read(buffer, 0, buffer.length)) > 0) {
if ((bRead + bufferSize) >= mReceiveFileLength) {
c = (int) (mReceiveFileLength - bRead);
mLogger.log("rest bytes", "" + c);
}
if(bRead < 10000 || bRead + 10000 > mReceiveFileLength){
mLogger.log(new String(buffer));
}
oos.write(buffer, 0, c);
oos.flush();
bRead += c;
mLogger.log("read " + bRead + " of " + mReceiveFileLength + " bytes");
}
oos.close();
mLogger.log("saved file", mReceiveFilename);
mReceiveFile = false;
mReceiveFilename = null;
}
if (sb.toString().startsWith(Protocol.COMMAND_SEND_FILE)) {
// "SEND_FILE:filename.ext"
try {
String[] command = sb.toString().split(Protocol.SEPARATOR);
mReceiveFile = true;
mReceiveFilename = command[1];
mReceiveFileLength = Long.parseLong(command[2]);
} catch (Exception e) {
mLogger.log("Protocoll exception command could not be parsed:" + sb.toString());
}
} else {
EventBus.getDefault().post(new BluetoothCommunicator(sb.toString()));
}
}