无法从Firebase检索和读取音频文件

时间:2019-03-28 16:45:56

标签: android firebase firebase-storage

我有一个可以录制音频文件并将其上传到Firebase存储的应用。目前,我正在尝试从firebase中读取上传的音频文件,并使用Google Cloud Speech to Text API转录音频文件。

由于某种原因,即使Uri / Url路径正确,我也无法检索音频文件。

以下是用于检索和转录音频文件的代码:

     private void uploadAudioFirebase() {

            String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
            Uri uri = Uri.fromFile(new File(mFilename));

            filepath = mStorage.child("Audio").child(userUid).child(mFilename);

            // upload recorded audio file from phone 
            filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    Toast.makeText(getApplicationContext(), "Audio uploaded to firebase", Toast.LENGTH_SHORT).show();

                    filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            final String downloadUrl = uri.toString();
                            Log.d(RECORD_TAG, "Download url:" + downloadUrl);  // download url is correct !!!!

                            AsyncTask.execute(() -> {
 // get the audio file that was just uploaded                               
 filepath.getBytes(Long.MAX_VALUE).addOnSuccessListener(new 
 OnSuccessListener<byte[]>() {
                                    @Override
                                    public void onSuccess(byte[] bytes) {
                                        Speech speechService = new Speech.Builder(
                                                AndroidHttp.newCompatibleTransport(),
                                                new AndroidJsonFactory(),
                                                null
                                        ).setSpeechRequestInitializer(
                                                new SpeechRequestInitializer(CLOUD_API_KEY))
                                                .build();
                                        RecognitionConfig recognitionConfig = new RecognitionConfig();
                                        recognitionConfig.setLanguageCode("en-US");
                                        RecognitionAudio recognitionAudio = new RecognitionAudio();
                                        recognitionAudio.setContent(bytes.toString());

                                        // Create request
                                        SyncRecognizeRequest request = new SyncRecognizeRequest();
                                        request.setConfig(recognitionConfig);
                                        request.setAudio(recognitionAudio);

                                        // Generate response
                                        SyncRecognizeResponse response = null;
                                        try {
                                            response = speechService.speech()
                                                    .syncrecognize(request)
                                                    .execute();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                            Log.d(RECORD_TAG, "Sync regconize response not working ");
                                        }

                                        // Extract transcript
                                        SpeechRecognitionResult result = response.getResults().get(0);
                                        final String transcript = result.getAlternatives().get(0)
                                                .getTranscript();

                                        Log.d(RECORD_TAG, "Transcribed text: " + transcript);
                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception exception) {
                                        Log.d(RECORD_TAG, "file bytes are not downloaded   ");
                                    }
                                });
    });
                        }
                    }) .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(CustomerMapActivity.this, "AUDIO TRANSCRIPTION NOT WORKING", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }

这是Logcat输出错误: Logcat output

有人知道我从Firebase检索和读取文件的方式是否正确吗? 谢谢!

0 个答案:

没有答案