簡體   English   中英

Android 將語音保存為文本音頻

[英]Android save Speech to Text audio

我正在嘗試創建一個應用程序,該應用程序將允許用戶將 Google 語音創建的音頻文件上傳為文本到服務器。 我已設法獲取音頻文件的 URI,但如何訪問它或將其轉換為可收聽的格式? 我試圖播放它,但到目前為止什么都沒有。 這就是我所擁有的。

我的演講轉文字

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode){
        case REQ_CODE_SPEECH_INPUT: {
            Bundle bundle = data.getExtras();
            if(resultCode==RESULT_OK && null!= data){
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                vtt.setText(result.get(0));
                Uri audioUri = data.getData();
                uri.setText(String.valueOf(audioUri));
                audioPath = uri.getText().toString();

            }
            break;

        }
    }
}

當我嘗試播放 audioPath 變量時。 什么都沒有出來。 如何將其轉換為可收聽的格式?

我得到的 uri 示例

content://com.google.android.googlequicksearchbox.AudioProvider/NoteToSelfOriginalAudio1.amr

感謝您的幫助

我發現我應該在某個地方使用內容解析器並對輸入流做一些事情,但我不確定是什么。

我設法解決了這個問題。

最簡單的方法是使用apache commons IO中的函數

audioPath = uri.getText().toString();
                finalPath = combinedPath.replaceAll(" ","");
                Log.d("Filepath",combinedPath.replaceAll(" ",""));
                ContentResolver contentResolver = getContentResolver();
                try{
                    InputStream filestream = contentResolver.openInputStream(audioUri);
                    File targetFIle = new File(finalPath);
                    FileUtils.copyInputStreamToFile(filestream,targetFIle);
                }catch (IOException e){
                    e.toString();
                }
Button_to_speak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Intent intent
                    = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
                    Locale.getDefault());
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text");

            try {
                startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT);
            }
            catch (Exception e) {
                Toast
                        .makeText(MainActivity.this, " " + e.getMessage(),
                                Toast.LENGTH_SHORT)
                        .show();
            }
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM