Search code examples
androidgoogle-cloud-firestorefirebase-storagevoice-recording

Firebase Storage , Unable to play a recorded audio message . Overwriting of the old registration on the new entry. How to solve?


I am trying to implement a chat system with the possibility of sending voice messages. here is a bit my logic: maintain a button and passed its message. Afterwards, release the button then, the message is saved in firestore storage. I do the recording well but I encounter 2 obstacles: on a new entry, the previous entry is overwritten, I rather want to have the complete list of voice messages in my firenase storage. In addition after recording, I can not open the voice. Can someone rescue me?

enter image description here

Here is my code : Oncreate Methode

fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        fileName += "/recored_audio.3gp" ;
        btnrecoder.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_DOWN) {

                    startRecording();
                    btnrecoder.setText(" Recording start...");
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    stopRecording();
                }
                return  true;
            }
        });

Out of the onCreate  :

    private void startRecording() {
        MediaRecorder  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(LOG_TAG, "prepare() failed" + e.getMessage());
            }

            recorder.start();
        }

        private void stopRecording() {
            recorder = new MediaRecorder();

            recorder.release();
            try{
                recorder.stop();
            }catch(RuntimeException stopException){
                //handle cleanup here
                Log.d(TAG," message derreure " + stopException.getMessage());
            }
            recorder = null;
            uploadAudio();

        }


        private void uploadAudio(){

            StorageReference fii = storageReference.child("Audio").child("new audio.3gp");
            Uri uri = Uri.fromFile(new File(fileName));
            fii.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    btnrecoder.setText("Uploading finished");
                    Toast.makeText(FriendList.this, "Vocal Save to db storage", Toast.LENGTH_SHORT).show();
                }
            });
        }

There were questions similar to this one that did not get concrete answers


Solution

  • As I posed here Unable to play audio in firebase storage sent from android app ,

    Your problem could have been caused by MediaRecorder. I have figured out that the output format should be set to AAC. Then the browser will be able to play it. I experienced this problem when I was trying to use React JS to play audio captured by MediaRecorder in Android, and I tried various formats (.3gp, .mp4, .wav), but this is the only one that worked. I suspect there are other ones, too.

    So:

    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);