0

我正在開發一個應用程序SpeechRecognizer。我將在不同的活動中將它用於不同的用途,並且它有點髒,所有時間都將相同的代碼添加到不同的類中。所以我將自定義RecognitionListener移到了一個新班級。這樣我就可以在我想要從我的活動中初始化它。但是我不能找到一種方法來接收聽衆的結果(在這種情況下,可以識別語音的可能值的ArrayList)以使用它。從另一個活動接收監聽器onResults()

我試圖通過一個接口來實現它,但我認爲我是以一種錯誤的方式實現它。我的監聽器的代碼是這樣的:

public class SpeechRecognitionListener implements RecognitionListener 
{ 
    private final String TAG = "SpeechRecognitionListener"; 
private Intent mSpeechRecognizerIntent; 
private SpeechRecognizer mSpeechRecognizer; 

public SpeechRecognitionListener(Intent speechRecognizerIntent, SpeechRecognizer speechRecognizer) { 
    mSpeechRecognizerIntent = speechRecognizerIntent; 
    mSpeechRecognizer = speechRecognizer; 
} 

@Override 
public void onBeginningOfSpeech() 
{ 
    //Log.d(TAG, "onBeginingOfSpeech"); 
} 

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

} 

@Override 
public void onEndOfSpeech() 
{ 
    //Log.d(TAG, "onEndOfSpeech"); 
} 

@Override 
public void onError(int error) 
{ 
    mSpeechRecognizer.startListening(mSpeechRecognizerIntent); 

    //Log.d(TAG, "error = " + error); 
} 

@Override 
public void onEvent(int eventType, Bundle params) 
{ 

} 

@Override 
public void onPartialResults(Bundle partialResults) 
{ 

} 

@Override 
public void onReadyForSpeech(Bundle params) 
{ 
    Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$ 
} 

@Override 
public void onResults(Bundle results) 
{ 
    //I want to recieve this array in my main activity 
    ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 

} 

@Override 
public void onRmsChanged(float rmsdB) 
{ 
} 
} 

我只是想收到onResult()陣列以我現在的活動與它合作。希望有人能幫助我!非常感謝!

回答

2

嘗試先定義一個接口:

public interface RecognitionCallback 
{ 
    abstract void onRecoginitionFinished(ArrayList<String> matches); 
} 

現在讓您的活動需要被召回實現此接口。例如:

public class MainActivity extends AppCompatActivity implements RecognitionCallback { 

    ... 

    public void onRecognitionFinished(ArrayList<String> matches) 
    { 
    //do your things with the data 
    } 

} 

另外補充SpeechRecognitionListener類的一些屬性:

public class SpeechRecognitionListener implements RecognitionListener 
{ 
    private final String TAG = "SpeechRecognitionListener"; 
    private Intent mSpeechRecognizerIntent; 
    private SpeechRecognizer mSpeechRecognizer; 
    private RecognitionCallback mCallback 

    public SpeechRecognitionListener(Intent speechRecognizerIntent, SpeechRecognizer speechRecognizer, RecognitionCallback callback) { 
     mSpeechRecognizerIntent = speechRecognizerIntent; 
     mSpeechRecognizer = speechRecognizer; 
     mCallback = callback; 

    ... 

    public void onResults(Bundle results) 
    { 

     ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     mCallback.onRecognitionFinished(matches); 
    } 
}  

最後,在你的活動,你需要得到所謂回寫:

SpeechRecognitionListener listener = new SpeechRecognitionLinstner(intent,recognizer,this); 
+1

你是太快:)這是正確的答案 –

+0

我同意@DavidSeroussi!非常感謝 !我真的很喜歡接口:P。順便說一句!如果您對SpeechRecognition有所瞭解,可以查看我的最後一篇文章,如果有人幫助我,我將非常感激! –

+0

@FranciscoDurdinGarcia我不熟悉語音識別,但在編碼實現中,如果有任何問題我很樂意幫助 – Pooya

相關問題