Android文本到語音示例含代碼

    Android文本到語音示例

    TextToSpeech類負責將文本轉換為語音。愛掏網 - it200.com它提供了許多方法來控制語音,例如setSpeedRate(),setPitch()等。愛掏網 - it200.com

    在這個例子中,我們將看到帶有速度和音調選項的Android TextToSpeech示例。愛掏網 - it200.com

    activity_main.xml

    將2個textviews、1個edittext、1個spinner和1個button拖放到布局中。愛掏網 - it200.com 現在activity_main.xml文件將如下所示:

    <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView1"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignParentRight="true"
            android:layout_marginRight="58dp"
            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="28dp"
            android:text="Speak" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_marginTop="62dp"
            android:layout_toLeftOf="@+id/editText1"
            android:text="Speed:" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_alignParentTop="true"
            android:layout_marginTop="42dp"
            android:text="Text:" />
    
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="43dp" />
    
    </RelativeLayout>
    

    Activity類

    讓我們來看一下說出給定文本的代碼。愛掏網 - it200.com

    package com.example.texttospeechspeed;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;
    import android.speech.tts.TextToSpeech;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;
    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 action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
    }
    
    }
    
    

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 国产一区二区三区91| 无码av中文一区二区三区桃花岛| 成人精品一区二区不卡视频| 久久久精品一区二区三区| 日韩AV在线不卡一区二区三区| 日韩在线不卡免费视频一区| 人妻免费一区二区三区最新| V一区无码内射国产| 国产一区二区三区樱花动漫| 天堂va在线高清一区| 无码精品一区二区三区在线| 嫩B人妻精品一区二区三区| 无码精品人妻一区| 美女AV一区二区三区| 精品国产一区二区三区久久狼| 99精品国产高清一区二区麻豆| 人妻AV一区二区三区精品| 性无码免费一区二区三区在线 | 日韩精品电影一区亚洲| 国产精品综合一区二区| 国产精品一区二区久久乐下载| 亚洲大尺度无码无码专线一区| 日韩一区二区三区射精| 国模大胆一区二区三区| 国产福利一区二区精品秒拍| 国产一区风间由美在线观看| 国产91精品一区| 99久久精品费精品国产一区二区| 中文字幕不卡一区| 国产激情无码一区二区app| 日韩免费视频一区二区| 精品无码一区二区三区电影 | 青娱乐国产官网极品一区| 精彩视频一区二区| 中文字幕精品亚洲无线码一区应用 | 日韩A无码AV一区二区三区| 中文日韩字幕一区在线观看| 国精无码欧精品亚洲一区| 91一区二区在线观看精品| 欧美日韩精品一区二区在线观看| 国产第一区二区三区在线观看|