2016-11-26 12 views
0

私は私のスピーチの文脈を得ることができ、同じ音声で答えるアプリを作っています。 「こんにちは、私の名前はジョンです」と言えば、「こんにちは、私の名前はジョンです」と返答するはずです。私は、GoogleのSTTとTTSサービスを使ってAndroid Studioにプログラムを書いています。私が持っている問題は、STTだけがうまく動作しますが、TTSはSTTから受信した文字列を取得していないようです。GoogleからSpeech to TextへのText To Speechサービスに文字列を渡すことができません。助けが必要

コードをよくご覧ください。

import java.util.ArrayList; 
import java.util.Locale; 

import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.os.Build; 
import android.speech.tts.TextToSpeech; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.RecognizerIntent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.ibm.watson.developer_cloud.conversation.v1.ConversationService; 
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest; 
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse; 

public class MainActivity extends Activity { 

private TextView txtSpeechInput; 
private ImageButton btnSpeak; 
private final int REQ_CODE_SPEECH_INPUT = 100; 
private TextToSpeech tts; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput); 
    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); 

    // hide the action bar 
    getActionBar().hide(); 

    btnSpeak.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      promptSpeechInput(); 
     } 
    }); 

    tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      if (status == TextToSpeech.SUCCESS) { 
       int result = tts.setLanguage(Locale.US); 
       if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
        Log.e("TTS", "This Language is not supported"); 
       } 
       speak(); 

      } else { 
       Log.e("TTS", "Initilization Failed!"); 
      } 
     } 
    }); 

} 

private void speak(){ 
    tts.speak(txtSpeechInput.getText().toString(),TextToSpeech.QUEUE_FLUSH, null, null); 
} 


@Override 
public void onDestroy() { 
    if (tts != null) { 
     tts.stop(); 
     tts.shutdown(); 
    } 
    super.onDestroy(); 
} 
/** 
* Showing google speech input dialog 
* */ 
private void promptSpeechInput() { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
      getString(R.string.speech_prompt)); 
    try { 
     startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); 
    } catch (ActivityNotFoundException a) { 
     Toast.makeText(getApplicationContext(), 
       getString(R.string.speech_not_supported), 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

/** 
* Receiving speech input 
* */ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
    case REQ_CODE_SPEECH_INPUT: { 
     if (resultCode == RESULT_OK && null != data) { 

      ArrayList<String> result = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      txtSpeechInput.setText(result.get(0)); 
     } 
     break; 
    } 

    } 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
}} 

答えて

0

問題を修正しました。私は間違った場所で電話しています。以下は

修正されたコードです:

import java.util.ArrayList; 
import java.util.Locale; 

import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.os.Build; 
import android.speech.tts.TextToSpeech; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.RecognizerIntent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements TextToSpeech.OnInitListener{ 

private TextView txtSpeechInput; 
private ImageButton btnSpeak; 
private TextToSpeech tts; 
private final int REQ_CODE_SPEECH_INPUT = 100; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput); 
    tts = new TextToSpeech(this, this); 
    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); 

    // hide the action bar 
    getActionBar().hide(); 

    btnSpeak.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      promptSpeechInput(); 
     } 
    }); 
} 


/** 
* Showing google speech input dialog 
* */ 
private void promptSpeechInput() { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
      getString(R.string.speech_prompt)); 
    try { 
     startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); 
    } catch (ActivityNotFoundException a) { 
     Toast.makeText(getApplicationContext(), 
       getString(R.string.speech_not_supported), 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

/** 
* Receiving speech input 
* */ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
    case REQ_CODE_SPEECH_INPUT: { 
     if (resultCode == RESULT_OK && null != data) { 

      ArrayList<String> result = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      txtSpeechInput.setText(result.get(0)); 
      speak(); 
     } 
     break; 
    } 

    } 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

private void speak(){ 
    tts.speak(txtSpeechInput.getText().toString(),TextToSpeech.QUEUE_FLUSH, null, null); 
} 

@Override 
public void onInit(int status) { 
    if (status == TextToSpeech.SUCCESS) { 
     int result = tts.setLanguage(Locale.US); 
     if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
      Toast.makeText(this, "This language is not supported", Toast.LENGTH_LONG).show(); 
     } 
     else{ 
      promptSpeechInput(); 
     } 
    }else{ 
     Toast.makeText(this, "Initialization failed", Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void onDestroy() { 
    if (tts != null) { 
     tts.stop(); 
     tts.shutdown(); 
    } 
    super.onDestroy(); 
}} 
関連する問題