我已经找到了答案
首先授予可访问性权限并拥有此类
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
public class AccessAudio extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d("","gf");
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
accessibilityServiceInfo.flags = 1;
accessibilityServiceInfo.eventTypes = -1;
accessibilityServiceInfo.feedbackType = 16;
setServiceInfo(accessibilityServiceInfo);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onInterrupt() {
}
}
之后你可以使用
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice.getPath());
mediaRecorder.prepare();
mediaRecorder.start();