保存 Android mediaRecorder 输出并将其上传到 Firebase 存储
Saving an Android mediaRecorder output and uploading it to Firebase Storage
我正在尝试捕获 Android 录音并将其发送到 Firebase 存储。我似乎正在输出音频文件(如日志所示,它不为空),并且发生了 Firebase 上传。但是,上传是空的。我怀疑问题发生在我如何保存输出和将其转换为字节数组之间。另外,我对字节数组以及如何使用它们了解不多,所以我可能会遗漏一些明显的东西。
我也在片段中 运行 处理所有这些。
fileName = Objects.requireNonNull(Objects.requireNonNull(getActivity()).getExternalCacheDir()).getAbsolutePath();
fileName += "/audiorecordtest.3gp";
当我按下开始时,我 运行 这个:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Log.e(TAG, "prepare() failed");
}
recorder.start();
当我按下停止时,我 运行 这个:
recorder.stop();
recorder.release();
recorder = null;
然后我准备我的 Firebase 上传录音:
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType("audio/mpeg")
.build();
UploadTask uploadTask = voiceRef.child(newMessage).putBytes(fileName.getBytes(), metadata);
如果需要,这里是完整的 Firebase 上传:
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//int progressInt = (int) progress;
System.out.println("Upload is " + progress + "% done");
//progressBar.setProgress(progressInt);
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
System.out.println("Upload Failed! Please try again.");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
voiceRef.child(newMessage).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Message.Voice voice = new Message.Voice(uri.toString(), timeInt);
Author author = new Author(UserID, UserName, null, false); //UserID, UserName
Message message = new Message(newMessage, author, UserID, null, "sent", Header, null);
message.setVoice(voice);
//The completion listener is added here as this prevents an odd occurrencee of the onChildListener
//in the messages fragment firing twice when this activity is exited too quickly.
myMessageRoomRef.child(newMessage).setValue(message, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
if (databaseError != null) {
System.out.println("Data could not be saved " + databaseError.getMessage());
} else {
System.out.println("Data saved successfully.");
Objects.requireNonNull(getActivity()).finish();
}
}
});
}
});
}
});
fileName.getBytes()
只是returns文件路径的字节数组表示。它不包含文件本身的任何数据。如果你检查那个数组的长度,它会很小。
如果您有文件要上传,您需要将文件的名称告知 Firebase Storage SDK,然后按照 uploading from a local file 文档中的说明进行操作。您的代码将像这样开始:
Uri file = Uri.fromFile(new File(fileName));
uploadTask = voiceRef.putFile(file);
我正在尝试捕获 Android 录音并将其发送到 Firebase 存储。我似乎正在输出音频文件(如日志所示,它不为空),并且发生了 Firebase 上传。但是,上传是空的。我怀疑问题发生在我如何保存输出和将其转换为字节数组之间。另外,我对字节数组以及如何使用它们了解不多,所以我可能会遗漏一些明显的东西。 我也在片段中 运行 处理所有这些。
fileName = Objects.requireNonNull(Objects.requireNonNull(getActivity()).getExternalCacheDir()).getAbsolutePath();
fileName += "/audiorecordtest.3gp";
当我按下开始时,我 运行 这个:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Log.e(TAG, "prepare() failed");
}
recorder.start();
当我按下停止时,我 运行 这个:
recorder.stop();
recorder.release();
recorder = null;
然后我准备我的 Firebase 上传录音:
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType("audio/mpeg")
.build();
UploadTask uploadTask = voiceRef.child(newMessage).putBytes(fileName.getBytes(), metadata);
如果需要,这里是完整的 Firebase 上传:
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//int progressInt = (int) progress;
System.out.println("Upload is " + progress + "% done");
//progressBar.setProgress(progressInt);
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
System.out.println("Upload Failed! Please try again.");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
voiceRef.child(newMessage).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Message.Voice voice = new Message.Voice(uri.toString(), timeInt);
Author author = new Author(UserID, UserName, null, false); //UserID, UserName
Message message = new Message(newMessage, author, UserID, null, "sent", Header, null);
message.setVoice(voice);
//The completion listener is added here as this prevents an odd occurrencee of the onChildListener
//in the messages fragment firing twice when this activity is exited too quickly.
myMessageRoomRef.child(newMessage).setValue(message, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
if (databaseError != null) {
System.out.println("Data could not be saved " + databaseError.getMessage());
} else {
System.out.println("Data saved successfully.");
Objects.requireNonNull(getActivity()).finish();
}
}
});
}
});
}
});
fileName.getBytes()
只是returns文件路径的字节数组表示。它不包含文件本身的任何数据。如果你检查那个数组的长度,它会很小。
如果您有文件要上传,您需要将文件的名称告知 Firebase Storage SDK,然后按照 uploading from a local file 文档中的说明进行操作。您的代码将像这样开始:
Uri file = Uri.fromFile(new File(fileName));
uploadTask = voiceRef.putFile(file);