TTS引擎使用扬声器模式输出到正在进行的呼叫

时间:2013-12-30 11:13:31

标签: android

public class MainActivity extends Activity implements
TextToSpeech.OnInitListener,OnItemSelectedListener {
/** Called when the activity is first created. */

private TextToSpeech tts;
private Button buttonSpeak;
private EditText editText;
private Spinner speedSpinner,pitchSpinner;

private static String speed="Normal";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tts = new TextToSpeech(this, this);
buttonSpeak = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText1);
speedSpinner = (Spinner) findViewById(R.id.spinner1);

//load data in spinner
loadSpinnerData();
speedSpinner.setOnItemSelectedListener(this);

//button click event
buttonSpeak.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        setSpeed();
        speakOut();
    }

});
}


@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");
    } else {
        buttonSpeak.setEnabled(true);
        speakOut();
    }

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

}

@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
    tts.stop();
    tts.shutdown();
}
super.onDestroy();
}

private void setSpeed(){
    if(speed.equals("Very Slow")){
        tts.setSpeechRate(0.1f);
    }
    if(speed.equals("Slow")){
        tts.setSpeechRate(0.5f);
    }
    if(speed.equals("Normal")){
        tts.setSpeechRate(1.0f);//default 1.0
    }
    if(speed.equals("Fast")){
        tts.setSpeechRate(1.5f);
    }
    if(speed.equals("Very Fast")){
        tts.setSpeechRate(2.0f);
    }
    //for setting pitch you may call 
    //tts.setPitch(1.0f);//default 1.0
}

private void speakOut() {
String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

private void loadSpinnerData() {
    //Data for speed Spinner
    List<String> lables = new ArrayList<String>();
    lables.add("Very Slow");
    lables.add("Slow");
    lables.add("Normal");
    lables.add("Fast");
    lables.add("Very Fast");

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    speedSpinner.setAdapter(dataAdapter);

  }

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    // On selecting a spinner item
    speed = parent.getItemAtPosition(position).toString();

    Toast.makeText(parent.getContext(), "You selected: " + speed,
            Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the actienter code here`on bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
}

}

////////////////////////////////////////    请告诉我!!如何能够为正在进行的录音注入扬声器的声音........ 我的目标是构建一个用于在静音人之间进行通信的android软件,我创建一个由文本字段和语音生成按钮组成的活动, 我的TTS引擎完美地完成了语音生成, 我的下一步是TTS生成的扬声器语音听到麦克风并听到人“B”, 但是这里的人B听不到声音

0 个答案:

没有答案