0

我想在 android 中创建一个语音识别应用程序并在服务中运行它,这样即使没有在应用程序中我也可以使用它。所以我寻找参考,我在 GitHub 中找到了一个演示应用程序。

这是网站https://github.com/ihrupin/SpeechRecognitionService

我下载了该应用程序,还阅读了文档,是的,它对我来说运行良好,它也在服务中运行,但我真正想要的是,例如,如果我说(打开 Facebook)它将打开已安装的 Facebook 应用程序。

我是使用 PocketSphinx 的新手。

这是主要活动

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");
}
}

这是引导接收器

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

0

如果要修改现有语法并添加自己的单词,则必须对其进行一些修改。在您的识别器设置方法中写下这一行。

识别器.addKeyphraseSearch("facebookPhrase", "打开 Facebook");

要编辑此示例,您可以在此处的官方网站上阅读

https://cmusphinx.github.io/wiki/tutoriallm/

于 2018-03-03T08:22:55.253 回答