0

Androidで音声認識アプリを作成し、アプリ内でなくても使えるようにサービスで動かしたいです。そこでリファレンスを探したところ、GitHub でデモ アプリを見つけました。

これはサイトですhttps://github.com/ihrupin/SpeechRecognitionService

私はアプリをダウンロードし、ドキュメントも読みました。はい、それは私にとってはうまく動作しており、サービスでも実行されていますが、たとえば、(Facebook を開く) と言った場合、インストールされている Facebook アプリが開きます。

私は PocketSphinx を使用する初心者です。

これは MainActivity です

public class MainActivity extends AppCompatActivity {

private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
private static final String LOG_TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ((Button)findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i(LOG_TAG, "onClick");
            int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
            if (permissionCheck == PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);
                return;
            }
            startService(new Intent(MainActivity.this, VoiceService.class));
        }
    });
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            startService(new Intent(MainActivity.this, VoiceService.class));
        } else {
            finish();
        }
    }
}
}

これは私のサービスです

public class VoiceService extends Service implements
    RecognitionListener {

private static final String LOG_TAG = VoiceService.class.getSimpleName();



private static final String KWS_SEARCH = "wakeup";


private static final String KEYPHRASE = "lisa";

private SpeechRecognizer recognizer;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
        runRecognizerSetup();
    }
    return super.onStartCommand(intent, flags, startId);
}

private void runRecognizerSetup() {

    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(VoiceService.this);
                File assetDir = assets.syncAssets();
                setupRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception result) {
            if (result != null) {
                Log.i(LOG_TAG, "Failed to init recognizer ");
            } else {
                switchSearch(KWS_SEARCH);
            }
        }
    }.execute();
}

@Override
public void onDestroy() {
    super.onDestroy();

    if (recognizer != null) {
        recognizer.cancel();
        recognizer.shutdown();
    }
}


@Override
public void onPartialResult(Hypothesis hypothesis) {
    if (hypothesis == null)
        return;

    String text = hypothesis.getHypstr();
    if (text.contains(KEYPHRASE)) {
        Toast.makeText(this, "onPartialResult text=" + text, Toast.LENGTH_SHORT).show();
        switchSearch(KWS_SEARCH);
    }

    Log.i(LOG_TAG, "onPartialResult text=" +text);
}


@Override
public void onResult(Hypothesis hypothesis) {
    if (hypothesis != null) {
        String text = hypothesis.getHypstr();
        Log.i(LOG_TAG, "onResult text=" +text);

    }
}

@Override
public void onBeginningOfSpeech() {
    Log.i(LOG_TAG, "onBeginningOfSpeech");
}


@Override
public void onEndOfSpeech() {
    if (!recognizer.getSearchName().contains(KWS_SEARCH))
        switchSearch(KWS_SEARCH);
    Log.i(LOG_TAG, "onEndOfSpeech");
}

private void switchSearch(String searchName) {
    Log.i(LOG_TAG, "switchSearch searchName = " + searchName);
    recognizer.stop();


    recognizer.startListening(searchName, 10000);
}

private void setupRecognizer(File assetsDir) throws IOException {


    recognizer = SpeechRecognizerSetup.defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))

            .setRawLogDir(assetsDir)
            .setKeywordThreshold(1e-45f)
            .setBoolean("-allphone_ci", true)


            .getRecognizer();
    recognizer.addListener(this);


    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
}

@Override
public void onError(Exception error) {
    Log.i(LOG_TAG, "onError " + error.getMessage());
}

@Override
public void onTimeout() {
    switchSearch(KWS_SEARCH);
    Log.i(LOG_TAG, "onTimeout");
}
}

これはBootReceiverです

public class BootReceiver extends BroadcastReceiver {
private static final String LOG_TAG = BootReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(LOG_TAG, "onReceive");
    if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
        Log.i(LOG_TAG, "onReceive onBoot");
        context.startService(new Intent(context, VoiceService.class));
    }
}
}

このトピックについて調べたところ、文法と辞書を変更する必要があることがわかりましたが、その方法がわかりません。何か案は?

4

1 に答える 1