2

Microsoft Cognitive Speech API を使用して、Android で Text to Speech を実装しています。マイク入力では機能しますが、WAV ファイルでは機能しません。墜落した。Androidコードを添付します。

package com.devstop.speechtotext;

import android.content.Intent;

import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.microsoft.cognitiveservices.speech.CancellationReason;
import com.microsoft.cognitiveservices.speech.ResultReason;
import com.microsoft.cognitiveservices.speech.SpeechConfig;
import com.microsoft.cognitiveservices.speech.SpeechRecognitionResult;
import com.microsoft.cognitiveservices.speech.SpeechRecognizer;
import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
import com.microsoft.cognitiveservices.speech.audio.AudioInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {
    private static final int VERIFY_PERMISSION_REQUEST = 200;
    public String path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(checkPermissionArray(Permissions.READ_EXTERNAL_STORAGE)){



        }else{
            verifyPermission(Permissions.READ_EXTERNAL_STORAGE);


        }

    }

    public void Gallery(View view) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("audio/*"); // specify "audio/mp3" to filter only mp3 files
        startActivityForResult(intent, 1);




    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){

        if(requestCode == 1){

            if(resultCode == RESULT_OK){

                //the selected audio.
                Uri uri = data.getData();

                String path = getPath(uri);
                try {
                    speech(path);
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }

    }

    public  void speech(String path) throws ExecutionException, InterruptedException {
        // Creates an instance of a speech config with specified
// subscription key and service region. Replace with your own subscription key
// and service region (e.g., "westus").
        SpeechConfig config = SpeechConfig.fromSubscription("Ijusthideit", "westus");




        AudioConfig audioInput = AudioConfig.fromWavFileInput(path);


        SpeechRecognizer recognizer = new SpeechRecognizer(config, audioInput);
        {
            // Subscribes to events.
            recognizer.recognizing.addEventListener((s, e) -> {
                System.out.println("RECOGNIZING: Text=" + e.getResult().getText());
            });

            recognizer.recognized.addEventListener((s, e) -> {
                if (e.getResult().getReason() == ResultReason.RecognizedSpeech) {
                    System.out.println("RECOGNIZED: Text=" + e.getResult().getText());
                }
                else if (e.getResult().getReason() == ResultReason.NoMatch) {
                    System.out.println("NOMATCH: Speech could not be recognized.");
                }
            });

            recognizer.canceled.addEventListener((s, e) -> {
                System.out.println("CANCELED: Reason=" + e.getReason());

                if (e.getReason() == CancellationReason.Error) {
                    System.out.println("CANCELED: ErrorDetails=" + e.getErrorDetails());
                    System.out.println("CANCELED: Did you update the subscription info?");
                }
            });

            recognizer.sessionStarted.addEventListener((s, e) -> {
                System.out.println("\n    Session started event.");
            });

            recognizer.sessionStopped.addEventListener((s, e) -> {
                System.out.println("\n    Session stopped event.");
            });

            // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
            System.out.println("Say something...");
            recognizer.startContinuousRecognitionAsync().get();

            System.out.println("Press any key to stop");
            new Scanner(System.in).nextLine();

            recognizer.stopContinuousRecognitionAsync().get();
        }
    }

    private void verifyPermission(String[] permissions) {
        ActivityCompat.requestPermissions(this,permissions,VERIFY_PERMISSION_REQUEST);
    }

    private boolean checkPermissionArray(String[] permissions) {
        for (String check : permissions) {
            if (!checkPermissions(check)) {
                return false;
            }
        }
        return true;
    }

    private boolean checkPermissions(String permission) {
        int permissionRequest = ActivityCompat.checkSelfPermission(getApplicationContext(),permission);
        if(permissionRequest != PackageManager.PERMISSION_GRANTED){

            return false;
        }else{
            return true;
        }

    }

    private String getPath( Uri uri ) {
        String result = null;
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = getApplicationContext().getContentResolver().query(uri, proj, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int column_index = cursor.getColumnIndexOrThrow(proj[0]);
                result = cursor.getString(column_index);
            }
            cursor.close();
        }
        if (result == null) {
            result = "nill";
        }
        return result;
    }



}

wavファイルを選択すると。クラッシュし、logcat がこれを返します。

0x7e40beae4c _ZN9Microsoft17CognitiveServices6Speech16SpeechRecognizer10FromConfigENSt6__ndk110shared_ptrINS1_12SpeechConfigEEENS4_INS1_5Audio11AudioConfigEEE # 9: 0x7e40bcf82c Java_com_microsoft_cognitiveservices_speech_internal_carbon_1javaJNI_SpeechRecognizer_1FromConfig_1_1SWIG_10 #10: 0x7e622fc704 ??? #11: 0x7e622f363c ??? #12: 0x7e61ec20b8 _ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc #13: 0x7e6206c1e0 _ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE #14: 0x7e62067718 _ZN3art11interpreter6DoCallILb1ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE #15: 0x7e622dd9dc MterpInvokeStaticRange #16: 0x7e622e5198 ExecuteMterpImpl #17: 0x7e62047624 ??? #18: 0x7e6204dd24 _ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE #19: 0x7e62066888 _ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE #20: 0x7e622dc134 MterpInvokeStatic #21: 0x7e622e4e98 ExecuteMterpImpl #22: 0x7e62047624 ??? #23: 0x7e6204dd24 _ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE #24: 0x7e62066888 _ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE #25: 0x7e622dbe7c MterpInvokeDirect #26: 0x7e622e4e18 ExecuteMterpImpl #27: 0x7e62047624 ??? コムで。

ここで何が起こっているのかわかりません。音声機能に渡すファイルパスは正しいです。しかし、それでもクラッシュします。どんな助けでも大歓迎です。ありがとう

4

1 に答える 1