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クラスのいくつかのプロパティを追加:あなたはあまりにも速かった

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

:あなたが戻って呼び出される必要があり、あなたの活動の

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); } } 

そして最後にこれを書きます:)これは正しい答えです –

+0

私は@DavidSeroussiに同意します!どうもありがとうございます !私はインターフェイスを持つ本当にnoobです:P。 btw! SpeechRecognitionについて知っていれば、私の最後の投稿を確認することができます。誰かがそれを手伝ってくれたらとても感謝しています! –

+0

@FranciscoDurdinGarcia私は音声認識に精通していませんが、何か疑問があればコーディングの実装で助けてくれるでしょう – Pooya

関連する問題