如何将值从文本转换为语音?

时间:2014-04-03 07:50:18

标签: android text-to-speech

我正在尝试制作一个小例子,其中我正在从用户那里获取文本,而我正试图让该文本发言但我无法做到这一点。我使用的代码是。

在xml文件中......

<EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="62dp"
        android:layout_marginTop="33dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="61dp"
        android:text="Text To Speech" />

活动类......

public class MainActivity extends Activity implements OnClickListener,TextToSpeech.OnInitListener            
{
    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSpeak = (Button) findViewById(R.id.button1);
        btnSpeak.setOnClickListener(this);       
        txtText = (EditText) findViewById(R.id.editText1);
        tts = new TextToSpeech(this, this);


    }

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

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        speakOut();
    }



    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
         if (status != TextToSpeech.SUCCESS) 
         {

                 tts.setLanguage(Locale.ENGLISH);

         } 


    }

    private void speakOut() {

         Log.i("Speak", "In speakout method");

            String text = txtText.getText().toString();

            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }

}

在manifestset文件中......

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.texttospeech"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.texttospeech.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

对于设置语言,你必须做这样的事情

tts=new TextToSpeech(TextActivity.this, new OnInitListener() {
        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status != TextToSpeech.ERROR){
                tts.setLanguage(Locale.getDefault());
            }
        }
    });

点击按钮

Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        startActivityForResult(i, ReQuest);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == ReQuest && resultCode == RESULT_OK){
        ArrayList<String>alist = new ArrayList<String>();
        alist = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        textspeech.setText(alist.get(0));
    }
}

答案 1 :(得分:0)

试试这个:

Thread x;
MediaPlayer  mediaPlayer;
 x=new Thread(){    
public void run(){
         try{
             url1="http://www.translate.google.com/translate_tts?ie=UTF-8&q="this is word which is speech"%0A&tl="this is language"&prev=input";


            mediaPlayer=new MediaPlayer();
            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(url1);
            mediaPlayer.prepare(); // might take long! (for buffering, etc)
            mediaPlayer.start();
                    } catch (IllegalArgumentException e) {
                        mediaPlayer.reset();
                    } catch (IllegalStateException e) {
                        mediaPlayer.reset();
                    } catch (IOException e) {
                        mediaPlayer.reset();
                    } 


            finally{ 
            //  x.suspend();
            } 
     }

    };

然后你像这样调用线程:

x.start();

请不要忘记向manifest.xml添加权限

<uses-permission android:name="android.permission.INTERNET" />