2017-10-09 101 views
-1

我试图让我的Android 2.3.3应用程序的语音识别工作,但缺少一些基本的东西。Android正在忽略RECORD_AUDIO权限和语音API实施

首先,我AndroidManifest.xml文件的顶部,我有:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.myuser.myapp"> 
    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 

     ... remainder omitted for brevity 

其中我相信应促使Android的询问最终用户是否要授予我的应用程序权限的话筒时,它启动(如果没有,请纠正我!)...

接下来,我有Activity,其中有我的应用程序的语音识别功能的整个需要。基本上,我希望应用程序来检测每当有人说出声来,“转到”:

public class SpeechActivity extends AppCompatActivity implements RecognitionListener { 
    private SpeechRecognizer speechRecognizer; 

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

     int check = ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO); 
     if(check != PackageManager.PERMISSION_GRANTED) { 
      throw new RuntimeException("Ya can't do that now, ya here."); 
     } 

     speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 
     speechRecognizer.setRecognitionListener(this); 

     Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test"); 
     speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
     speechRecognizer.startListening(speechIntent); 
     Log.i("SpeechActivity", "Speech Recognizer is listening"); 
    } 

    @Override 
    public void onReadyForSpeech(Bundle bundle) { 

    } 

    @Override 
    public void onBeginningOfSpeech() { 

    } 

    @Override 
    public void onRmsChanged(float v) { 

    } 

    @Override 
    public void onBufferReceived(byte[] bytes) { 

    } 

    @Override 
    public void onEndOfSpeech() { 

    } 

    @Override 
    public void onError(int i) { 

    } 

    @Override 
    public void onResults(Bundle bundle) { 
     Log.i("SpeechActivity", "Speech was detected..."); 
     String spoken = ""; 
     ArrayList<String> data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     for (int i = 0; i < data.size(); i++) { 
      spoken += data.get(i); 
     } 

     Log.i(this.getClass().getName(), String.format("The word \"%s\" was detected.", spoken)); 
     if(spoken.equalsIgnoreCase("go")) { 
      doSomething(); 
     } 
    } 

    @Override 
    public void onPartialResults(Bundle bundle) { 

    } 

    @Override 
    public void onEvent(int i, Bundle bundle) { 

    } 
} 

我通过(通过USB)我的手机连接到我的笔记本电脑上运行我的三星Galaxy S7边缘应用,点击Android Studio中的运行(app)按钮,然后选择我的手机作为连接的设备。

当应用程序在我的手机上启动时,我注意到的第一件事是它不会提示我接受/拒绝应用程序使用麦克风的权限。这是一个预警信号,告诉我有什么东西是错误的。

但是,当我浏览到SpeechActivity/activity_speech.xml屏幕,我得到:

在运行此我得到:

FATAL EXCEPTION: main 
Process: com.example.myuser.myapp, PID: 9585 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myuser.myapp/com.example.myuser.myapp.SpeechActivity}: java.lang.RuntimeException: Ya can't do that now, ya here. 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6688) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
Caused by: java.lang.RuntimeException: Ya can't do that now, ya here. 
    at com.example.myuser.myapp.SpeechActivity.onCreate(SpeechActivity.java:43) 
    at android.app.Activity.performCreate(Activity.java:6912) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900) 

是否有可能我需要下载和安装,以插件或APK让语音识别工作?为什么我的应用程序没有要求使用手机麦克风的权限?语音识别器是如何开始/聆听的,但似乎没有听到任何演讲?

+0

声明清单中的权限是Mashmallow之前的方式,因为您需要通过权限询问用户权限,以便在屏幕上显示提示。你应该检查这个链接:https://developer.android.com/training/permissions/requesting.html – vincenth

+0

三星Galaxy S7 Edge与Android 2.3.3,真的吗? – rupinderjeet

+0

是@rupinderjeet 2.3.3为什么我的特定设备出现问题? – smeeb

回答

3

根据the documentation,这是一个危险的许可。您应该向用户请求。

RECORD_AUDIO:允许应用程序记录音频。

防护等级:危险

常数值: “android.permission.RECORD_AUDIO”

简单地说,你可以检查你的应用程序有使用

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, 
    Manifest.permission.RECORD_AUDIO); 

if (permissionCheck == PERMISSION_GRANTED) { 
    // you have the permission, proceed to record audio 

    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 
    speechRecognizer.setRecognitionListener(this); 

    Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test"); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
    speechRecognizer.startListening(speechIntent); 
    Log.i("SpeechActivity", "Speech Recognizer is listening"); 

} 
else { 

    // you don't have permission, try requesting for it 

    ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.RECORD_AUDIO}, 
      MY_PERMISSION_REQUEST_RECORD_AUDIO); 
} 

把你的时间这个权限,在Android指南中阅读Requesting Permissions指南。

+0

谢谢@rupinderjeet(+1;我也编辑了我以前的评论,因为你修改后的答案更符合我的问题!)。请参阅我的编辑,我添加了您的建议权限检查,现在当我导航到“SpeechActivity”时抛出异常(“Ya不能那么做......”),我的应用程序就会死亡。此外,它仍然不会促使我授权使用麦克风。有任何想法吗? – smeeb

+0

它会抛出异常,因为您没有权限。我编辑了我的答案,告诉你如何请求权限。另外,请注意我的回答的最后一行 – rupinderjeet

+0

谢谢@rupinderjeet(+1) - 我添加了堆栈跟踪如果它可以帮助你。你为什么说我没有权限?我不应该在某个时候获得许可吗? – smeeb