0

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();
                                                        }
                                                    }
                                                });
                                            }
                                        });
                                    }
                                });
Christopher Mills
  • 711
  • 10
  • 28

1 Answers1

1

fileName.getBytes() just returns a byte array representation of the path of the file. It doesn't contain any of the data in the file itself. If you check the length of that array, it will be tiny.

If you have a file to upload, you will need to tell the Firebase Storage SDK the name of the file, then follow the instructions in the documentation for uploading from a local file. Your code will start like this:

Uri file = Uri.fromFile(new File(fileName));
uploadTask = voiceRef.putFile(file);
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks Doug. Your answer, along with this link, sorted out the issue. https://stackoverflow.com/questions/11985518/android-record-sound-in-mp3-format/33054794#33054794 – Christopher Mills Jul 07 '20 at 17:43