我试图在机器学习算法中检测语音功能,但在后台和手机关机时,我很难运行语音识别api。我尝试将SpeechRecognitor放入服务中,但是,该服务被终止并报告此错误:
e/activitythread:service com.test.speechrecognition.services.speechdetectionservice已泄漏serviceconnection android.SpeechRecognitor$connection@914d0fc最初绑定到这里的是android.app.serviceconnection泄漏:service com.test.speechrecognition.services.speechdetectionservice泄漏了serviceconnectionandroid.speechrecognizer.speechrecognizer$connection@914d0fc它最初绑定在android.app.loadedapk$servicedispatcher(loadedapk.java:1835)的android.app.loadedapk.getservicedispatchercommon(loadedapk.java:1707)的android.app.loadedapk.getservicedispatcher(loadedapk.java:1686)的android.app.contextimpl.bindservicecommon(contextimpl.java:1819)在android.app.contextimpl.bindservice(contextimpl.java:1749)在android.content.contextwrapper.bindservice(contextwrapper.java:756)在android.speechrecognizer.speechrecognizer.starting(speechrecognizer.java:286)在com.test.speechrecognition.services.speechdetectionservice$1.run(speechdetectionservice.java:56)在android.os.handler.handlecallback(handle.java:938)在android.os.handler.dispatchmessage(handler.java:99)在android.os.looper.looper(looper.java:223)在android.app.activitythread.main(activitythread.java:7664)在com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit.java:592)上的java.lang.reflect.method.invoke(本机方法)位于com.android.internal.os.zygoteinit.main(zygoteinit.java:947)
我需要应用程序跟踪它是否每分钟检测到一次语音,十分钟左右。
以下是mainactivity.java和speechdetectionservice.java代码:
主要活动:
public class MainActivity extends AppCompatActivity {
// Create speechRecognizer object
private SpeechRecognizer speechRecognizer;
public static final Integer RecordAudioRequestCode = 1;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check if permissions have been granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
checkPermission();
}
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart:start SpeechDetectionService");
startService(new Intent(this, SpeechDetectionService.class));
}
// Permission check function
private void checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO},RecordAudioRequestCode);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == RecordAudioRequestCode && grantResults.length > 0) {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
}
}
}
语音检测服务:
public class SpeechDetectionService extends Service {
private static final String TAG = SpeechDetectionService.class.getSimpleName();
private SpeechRecognizer speechRecognizer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind()");
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "onStartCommand");
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
final Handler handler = new Handler();
final int delay = 10000;
handler.postDelayed(new Runnable() {
public void run() {
speechRecognizer.startListening(speechRecognizerIntent);
handler.postDelayed(this, delay);
}
}, delay);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
return START_STICKY;
}
暂无答案!
目前还没有任何答案,快来回答吧!