如何将关键字语音识别从 UI 置于后台?

问题描述 投票:0回答:1

我目前正在使用

pocketsphinx-android
作为关键字语音识别应用程序,但是,我正在丢弃 UI 框架,并且 UI 的行为不一致。我很确定我必须将语音识别放在后台,因为 pocketsphinx 在识别关键字后正在写入原始音频文件。我做了一些研究,我认为使用 WorkManager 可能是我的最佳选择,尽管作为一个新手程序员,我不确定如何构建事物。我也愿意接受比 WorkManager 更好的建议。

这些来自 logcat..

SpeechRecognizer        I  Start recognition "wakeup"
Choreographer           I  Skipped 602 frames!  The application may be doing too much work on its main thread.
OpenGLRenderer          I  Davey! duration=5026ms; Flags=0, FrameTimelineVsyncId=62813507, IntendedVsync=395687491545080, Vsync=395692505327364, InputEventId=0, HandleInputStart=395692509775563, AnimationStart=395692509783532, PerformTraversalsStart=395692509790355, DrawStart=395692510169573, FrameDeadline=395687499878413, FrameInterval=395692509005303, FrameStartTime=8328542, SyncQueued=395692510956761, SyncStart=395692511505980, IssueDrawCommandsStart=395692511578323, SwapBuffers=395692512311136, FrameCompleted=395692518470355, DequeueBufferDuration=21355, QueueBufferDuration=271458, GpuCompleted=395692518077803, SwapBuffersCompleted=395692518470355, DisplayPresentTime=0, CommandSubmissionCompleted=395692512311136, 
SpeechRecognizer        D  Starting decoding
cmusphinx               I  INFO: pocketsphinx.c(986): Writing raw audio file: /storage/emulated/0/Android/data/com.example.atest/files/sync/000000004.raw

现在,我的

MainActivity.java
..

上有语音识别作为界面
public class MainActivity extends AppCompatActivity implements
        RecognitionListener { ...
    
    // Called when keyword is recognized.
    @Override
    public void onPartialResult(Hypothesis hypothesis) { 
        stopRecognitionListener();
    }
        
    //  Called when recognition listener is stopped.
    @Override
    public void onResult(Hypothesis hypothesis) {
        reactToKeyword();
        startRecognitionListener();
    }

我想将语音记录与用户界面分开,因此我正在考虑使用

WorkManager
。我从 Android Studio 参考中获得了以下代码,但更改了一些变量名称以适合我的情况。

public class SpeechRecWorker extends Worker {
   public SpeechRecWorker(
       @NonNull Context context,
       @NonNull WorkerParameters params) {
       super(context, params);
   }

   // I'm confused why they called this "doWork" instead of "returnResult"
   // Does all work have to be in here?
   @Override
   public Result doWork() {

     // Do the work here--in this case, speech rec.
     somework();

     // Indicate whether the work finished successfully with the Result
     return Result.success();
   }
}

我可以在我的 Worker 上实现

RecognitionListener
接口吗?整个界面不应该在 doWork() 函数内吗?或者 doWork() 的存在只是为了返回结果?我的 SpeechRecWorker 中的所有内容都会在自己的线程上吗?

编辑:我非常愚蠢。我在应用程序中设置了 5 秒的睡眠时间,这导致了跳帧。不过我仍然对答案感兴趣。

java android multithreading worker
1个回答
0
投票

将音频生成逻辑放入单独的线程或执行器中

© www.soinside.com 2019 - 2024. All rights reserved.