2017-10-28 100 views
0

Android開發新手,試圖將語音實現爲文本,將實時打印屏幕上的文字,但獲得以下錯誤。似乎無法理解問題出在哪裏。是因爲我從togglebutton事件中調用了startRecording()stopRecording()還是完全是其他內容。試圖對文本實現語音,不知道出了什麼問題

com.example.android.movi​​ebud E/SpeechRecognizer:未連接到識別服務

package com.example.android.moviebud; 

import android.content.Intent; 
import android.os.Bundle; 
import android.speech.RecognitionListener; 
import android.speech.RecognizerIntent; 
import android.speech.SpeechRecognizer; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.CompoundButton; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ToggleButton; 

public class MainActivity extends AppCompatActivity implements RecognitionListener { 
    ToggleButton recBtn; 
    SpeechRecognizer recognizer; 
    Intent recognitionIntent; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     recognizer = SpeechRecognizer.createSpeechRecognizer(this); 
     recognizer.setRecognitionListener(this); 
     recognitionIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     recBtn = (ToggleButton) findViewById(R.id.recBtn); 
     recBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 
       if (isChecked) { 
        recognizer.startListening(recognitionIntent); 
       } else { 
        recognizer.stopListening(); 
        recognizer.destroy(); 
       } 
      } 
     }); 
    } 

    @Override 
    public void onReadyForSpeech(Bundle bundle) { 
     Toast.makeText(getApplicationContext(), "READY", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onBeginningOfSpeech() { 
     Toast.makeText(getApplicationContext(), "Speech recognition started", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onRmsChanged(float v) { 

    } 

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

    } 

    @Override 
    public void onEndOfSpeech() { 

    } 

    @Override 
    public void onError(int i) { 
     Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onResults(Bundle bundle) { 
     TextView v = (TextView) findViewById(R.id.speech); 
     StringBuilder sb = new StringBuilder(""); 
     for (String s : bundle.getStringArrayList(recognizer.RESULTS_RECOGNITION)) { 
      sb.append(s); 
     } 
     v.setText(sb.toString()); 
    } 

    @Override 
    public void onPartialResults(Bundle bundle) { 

    } 

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

    } 
} 

回答

0

試試這個代碼,它是工作,我都做到了。

public class MainActivity extends AppCompatActivity { 


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

     checkPermission(); 

     final EditText editText = findViewById(R.id.editText); 

     final SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 


     final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, 
       Locale.getDefault()); 


     mSpeechRecognizer.setRecognitionListener(new RecognitionListener() { 
      @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) { 
       //getting all the matches 
       ArrayList<String> matches = bundle 
         .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 

       //displaying the first match 
       if (matches != null) 
        editText.setText(matches.get(0)); 
      } 

      @Override 
      public void onPartialResults(Bundle bundle) { 

      } 

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

      } 
     }); 

     findViewById(R.id.button).setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       switch (motionEvent.getAction()) { 
        case MotionEvent.ACTION_UP: 
         mSpeechRecognizer.stopListening(); 
         editText.setHint("You will see input here"); 
         break; 

        case MotionEvent.ACTION_DOWN: 
         mSpeechRecognizer.startListening(mSpeechRecognizerIntent); 
         editText.setText(""); 
         editText.setHint("Listening..."); 
         break; 
       } 
       return false; 
      } 
     }); 
    } 


    private void checkPermission() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (!(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)) { 
       Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, 
         Uri.parse("package:" + getPackageName())); 
       startActivity(intent); 
       finish(); 
      } 
     } 
    } 
} 

富勒更多詳情,您可以訪問我的博客文章 - Android Speech To Text Tutorial

相關問題