简体   繁体   English

为什么我的TextToSpeech没有声音?

[英]Why is my TextToSpeech not making sound?

I am developing an app that requires text to speech. 我正在开发一个需要文本到语音的应用程序。 So I read a few tts tutorials and did this: 因此,我阅读了一些tts教程并做到了:

public void buttonClick (View view) {
    TextToSpeech tts = new TextToSpeech (this, new TextToSpeech.OnInitListener () {
        @Override
        public void onInit(int status) {

        }
    });

    tts.setLanguage (Locale.UK);
    tts.speak ("Hello World", TextToSpeech.QUEUE_FLUSH, null);
}

However, when I run the app and click the button, no voice is heard, only the "click" sound of the button. 但是,当我运行该应用程序并单击该按钮时,没有声音,只有按钮的“喀哒”声。 This also implies that the speaker is switched on. 这也意味着扬声器已打开。

I guess this is because speak(String, int, HashMap<String, String>) is deprecated. 我猜这是因为不推荐使用speak(String, int, HashMap<String, String>) This guess is proved wrong when I look at the docs: 当我查看文档时,这种猜测被证明是错误的:

 /** @deprecated As of API level 21, replaced by
 *         {@link #speak(CharSequence, int, Bundle, String)}.
 */

And my app's minimum SDK version is API18 and my device's android version is Android 4.3. 我的应用程序的最低SDK版本为API18,而设备的Android版本为Android 4.3。 That means speak is not deprecated. 这意味着speak不会被弃用。 The deprecation is maybe just a bug of Android Studio. 弃用可能只是Android Studio的一个错误。

I wonder why it makes no sound and how I can fix this problem. 我不知道为什么它没有声音,如何解决这个问题。

try this example. 试试这个例子。

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;

import android.speech.tts.TextToSpeech;
import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.Button;


import java.util.List;
import java.util.Locale;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.Toast;

public class MainActivity extends Activity {
   TextToSpeech t1;

   Button b1;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      b1=(Button)findViewById(R.id.button);

      t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
         @Override
         public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
               t1.setLanguage(Locale.UK);
            }
         }
      });

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {

            t1.speak("Hi dear its working", TextToSpeech.QUEUE_FLUSH, null);
         }
      });
   }

   public void onPause(){
      if(t1 !=null){
         t1.stop();
         t1.shutdown();
      }
      super.onPause();
   }


}

and xml file is 和xml文件是

<RelativeLayout xmlns:android="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"
   android:transitionGroup="true">


   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Text to Speech"
      android:id="@+id/button"
      android:layout_below="@+id/editText"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="46dp" />

</RelativeLayout>
public void buttonClick (View view) {
TextToSpeech tts = new TextToSpeech (this, new TextToSpeech.OnInitListener () {
    @Override
    public void onInit(int status) {
     // change required.Initialization has to finish first.   
     if(status != TextToSpeech.ERROR) {
     tts.setLanguage (Locale.UK);
     tts.speak ("Hello World", TextToSpeech.QUEUE_FLUSH, null);
     }
    }
});



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM