【问题标题】:how to run listening and recognizer service in background all the time android如何一直在后台运行监听和识别服务android
【发布时间】:2021-01-11 04:19:12
【问题描述】:

我想开发一个应用程序,它每次都倾听用户的意见,并且当关键字这么说时做某事。
为此我使用了vosk-api
我声明IntentService。当前,当用户单击底部导航中的切换按钮时,服务将停止。我怎样才能一直在后台运行服务,比如ok google? 代码是:

public class MainActivity extends AppCompatActivity {

private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;

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

    // Check if user has given permission to record audio
    int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);
        return;
    }

}

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

    if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent intent = new Intent(this, MyIntentService.class);
            startService(intent);
        } else {
            Log.e("MainActivity", "RECORD_AUDIO not allowed");
        }
    }
}

public class MyIntentService extends IntentService {

private SpeechService speechService;
private Model model;

public MyIntentService() {
    super("MyIntentService");
}


@Override
protected void onHandleIntent(@Nullable Intent intent) {
    initRecognizer();

    speechService.addListener(new RecognitionListener() {
        @Override
        public void onPartialResult(String s) {
            Log.d("MyIntentService", s + "\n");
            if (s.contains("me"))
                Toast.makeText(getApplicationContext(), "coollll!!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onResult(String s) {
            Log.d("MyIntentService", s + "\n");
            if (s.contains("me"))
                Toast.makeText(getApplicationContext(), "coollll!!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Exception e) {
            Log.d("MyIntentService", "Error");
        }

        @Override
        public void onTimeout() {
            speechService.cancel();
            speechService = null;
            Log.d("MyIntentService", "onTimeout");
        }
    });
    speechService.startListening();

}

public void initRecognizer(){
    Assets assets;
    try {
        assets = new Assets(getApplicationContext());
        File assetDir = assets.syncAssets();
        model = new Model(assetDir.toString() + "/model-android");
        KaldiRecognizer rec = new KaldiRecognizer(model, 16000.0f);
        speechService = new SpeechService(rec, 16000.0f);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

谢谢!

【问题讨论】:

    标签: java android service speech-recognition


    【解决方案1】:

    我通过创建一个实现RecognitionListener 的前台服务来做到这一点。但我从 vosk 演示应用开始。

    我在服务中的onCreate 方法中调用initModel()。在initModel 方法的回调中,我调用recognizeMicrophone 方法,与演示应用中的方法相同。

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 1970-01-01
      • 2021-11-05
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多