I'm trying to capture an Android recording and send it to Firebase storage. I appear to be outputting the audio file (as indicated by the log, it is not null), and the Firebase upload happens. However, the upload is empty. My suspicion is that the issue is occurring somewhere between how I save the output and converting it into a byte array. Also, I don't have much knowledge about byte arrays and how to work with them, so I could be missing something obvious. I'm also running all this from within a fragment.
fileName = Objects.requireNonNull(Objects.requireNonNull(getActivity()).getExternalCacheDir()).getAbsolutePath();
fileName += "/audiorecordtest.3gp";
When I press start I run this:
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();
When I press stop I run this:
recorder.stop();
recorder.release();
recorder = null;
Then I prepare my Firebase upload of the recording:
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType("audio/mpeg")
.build();
UploadTask uploadTask = voiceRef.child(newMessage).putBytes(fileName.getBytes(), metadata);
Here is the full Firebase upload in case it is required:
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();
}
}
});
}
});
}
});