user202690
user202690

Reputation:

Android - TTS no voice coming out

I am trying to implement a TTS application for android. Here is the code i've written so far:

import android.app.Activity;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;

public class AlarmActivity extends Activity implements OnClickListener, TextToSpeech.OnInitListener {

    private TextToSpeech mTts;
    private static final String TAG = "TextToSpeechDemo";
    private static final int MY_DATA_CHECK_CODE = 1234;
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.alarm);

        Button btnAdd = (Button) findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(this);
        btnAdd.setEnabled(false);

        TextView txt = (TextView) findViewById(R.id.txt);
        txt.setText("OnCreate");

        // Fire off an intent to check if a TTS engine is installed
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        TextView txt = (TextView) findViewById(R.id.txt);
        if (requestCode == MY_DATA_CHECK_CODE)
        {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
            {
                // success, create the TTS instance
                txt.setText("Done result");
                mTts = new TextToSpeech(this, this);
                mTts.setLanguage(Locale.US);
                Button btnAdd = (Button) findViewById(R.id.btnAdd);
                btnAdd.setEnabled(true);

            }
            else
            {
                txt.setText("Missing");
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(
                        TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }

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

    @Override
    public void onClick(View v) {

        TextView txt = (TextView) findViewById(R.id.txt);
                txt.setText("Click");

        String myText1 = "Did you sleep well?";
        String myText2 = "I hope so, because it's time to wake up.";
        mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
        mTts.speak(myText2, TextToSpeech.QUEUE_ADD, null);
    }
    @Override
    public void onInit(int status) {
        TextView txt = (TextView) findViewById(R.id.txt);
        txt.setText("status 0");
        if (status == TextToSpeech.SUCCESS) {
                txt.setText("status 1");
            int result = mTts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
                txt.setText("status 2");
            } else {
                Button btnAdd = (Button) findViewById(R.id.btnAdd);
                btnAdd.setEnabled(true);
                txt.setText("status 3");
            }
            } else {
            txt.setText("status 4");
            Log.e(TAG, "Could not initialize TextToSpeech.");
        }
    }
}

My layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Alarms"
        android:id="@+id/txt" />
    <Button android:id="@+id/btnAdd"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Say" />
</LinearLayout>

In netbeans ADB log i can see the following:

13:08:41.314    163 INFO    ActivityManager Starting activity: Intent { act=android.speech.tts.engine.CHECK_TTS_DATA cmp=com.svox.pico/.CheckVoiceData }
13:08:41.504    163 INFO    ActivityManager Displayed activity org.me.talkingdroid/.MainActivity: 2790 ms (total 2790 ms)
13:08:41.544    163 WARN    ActivityManager Binding with unknown activity: android.os.BinderProxy@46d91ac8
13:08:41.634    264 DEBUG   [ScrollKPI] drawScreenCache takes 222ms, drawThumbnailCache takes 106ms
13:08:43.214    163 VERBOSE KeyInputQueue   Enqueueing:  MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.214    163 INFO    WindowManager   dispatchPointer MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.214    163 INFO    WindowManager   Delivering pointer QueuedEvent{470bdcb8 MotionEvent{470684e0 action=0 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}} to Window{46e0c640 org.me.talkingdroid/org.me.talkingdroid.MainActivity paused=false}
13:08:43.264    163 VERBOSE KeyInputQueue   Enqueueing:  MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.264    163 INFO    WindowManager   dispatchPointer MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}
13:08:43.264    163 INFO    WindowManager   Delivering pointer QueuedEvent{46f34300 MotionEvent{470684e0 action=1 x=310.26947 y=438.86896 pressure=0.15294118 size=0.13333334}} to Window{46e0c640 org.me.talkingdroid/org.me.talkingdroid.MainActivity paused=false}
13:08:43.284    10637   INFO    TTS received:   Did you sleep well?
13:08:43.284    10637   INFO    TTS received:   I hope so, because it's time to wake up.
13:08:46.714    10038   DEBUG   dalvikvm    GC_EXPLICIT freed 417 objects / 26192 bytes in 82ms
13:08:47.994    10637   DEBUG   dalvikvm    Debugger has detached; object registry had 398 entries

It's obvious that the text am trying to output gets sent from the log, however i can hear nothing whatsoever coming out from my phone. What can be the issue?

Upvotes: 4

Views: 10248

Answers (5)

mikeLundquist
mikeLundquist

Reputation: 1009

I was having a similar issue on a Raspberry pi 3 running android things where TTS only played over the USB port until I ran TextToSpeech.setAudioAttributes(AudioAttributes) on my TextToSpeech object with the flag AudioAttributes.FLAG_AUDIVILITY_ENFORCED as you can see in the following code:

public class CustomTTS extends TextToSpeech {

    private static final String UTTERANCE_ID =
            "com.example.androidthings.bluetooth.audio.UTTERANCE_ID";

    public CustomTTS(Context context, TextToSpeech.OnInitListener listener){
        super(context, listener);
        setOnUtteranceProgressListener(new MyProgListener());
        try {
            AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder().
                    setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING).
                    setContentType(AudioAttributes.CONTENT_TYPE_SPEECH).
                    setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED);
            setAudioAttributes(audioAttributes.build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Upvotes: 1

Eoghan Hynes
Eoghan Hynes

Reputation: 41

I had to change my default language to one that is supported by text to speech on the phone for it to work:

Settings->Language And Input->Language.

Supported: English(UK) & English(US).
Not Supported: English(IE).

Upvotes: 0

Anthony
Anthony

Reputation: 21

This thread, refers to a problem with the SpeechSynthesis data installer. My problem was also 'no sound' from a TTS programme (similar to that shown), which otherwise executed correctly. In my case it was due to the SpeechSynthesis data not being installed on my tablet. After installing this on my tablet, my program worked even in debugging mode and with the cable attached.

For those new with TTS, like myself I set out below what I did to install the speechsynthesis data on Android 4.0.4. They may be different on other versions but should be similar.

Start 'Settings'- Select: ' Settings>Languages and Input>Text-to-Speech output' then under 'Preferred Engine, select 'Google Text-to-speech Engine' and click on the 'setup' symbol at RHS of that option. This shows three options: First select(1)'Language' then select (2)'Settings for Google TTS' and check 'auto-update voices' and 'Use Wi-Fi only'* then finally select (3)'install voice data'; (The Wi-Fi must be enabled to download the data)

Thanks to stackoverflow, the questions asked and responses given I was able to solve my problem which was not with the code but the configuration of the TTS on my tablet. I have also learned that it is not always the code that is the problem.

Upvotes: 1

YangLi
YangLi

Reputation: 11

Your code is more like android doc. I just wrote this demo code. I believe if you replace requestCode with resultCode in the following line, it will work fine. if (requestCode == MY_DATA_CHECK_CODE) This is my code :

public class TTS_demoActivity extends Activity implements OnClickListener,OnInitListener {
private EditText mContent;
private Button mSpeak;
private String mTemp;
private static final int REQ_TTS_STATUS_CHECK = 1;  
private static final String TAG = "TTS Demo";
private TextToSpeech mTts;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mContent = (EditText) findViewById(R.id.etcontent);
    mSpeak = (Button) findViewById(R.id.btnspeak);
    mSpeak.setOnClickListener(this);
    init();
}
/*
 * 初始化方法检测TTS所需要的数据是否存在
 */
private void init() {
    Intent mCheckIntent=new Intent();
    mCheckIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(mCheckIntent, REQ_TTS_STATUS_CHECK);

}
/*
 * 对检测结果进行分类处理
 */

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==REQ_TTS_STATUS_CHECK)
    {
        switch (resultCode) {
        case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
            mTts = new TextToSpeech(this, this);
            Log.v(TAG, "tts engine is instance");
            break;
        case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
            //文件已经损坏
        case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
            //缺少发音文件
        case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA: 
            //数据文件丢失

            //从新更新TTS数据文件
            Intent mUpdateData=new Intent();
            mUpdateData.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(mUpdateData);

            break;

        case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
            //检测失败应该重新检测
            Log.v(TAG, "TTS engine checked fail");
            break;
        default:
             Log.v(TAG, "Got a failure. TTS apparently not available");  
            break;
        }

    }   

}

@Override
public void onInit(int status) {
    if(status==TextToSpeech.SUCCESS){
        int result=mTts.setLanguage(Locale.US);
        if(result==TextToSpeech.LANG_NOT_SUPPORTED||result==TextToSpeech.LANG_MISSING_DATA)
        {
            Log.v(TAG, "language is not available");

        }else{
            mTts.speak("start complete", TextToSpeech.QUEUE_ADD, null);
            mSpeak.setEnabled(true);
        }

    }

}

@Override
public void onClick(View arg0) {
    mTemp= mContent.getText().toString();
    if(mTemp!=""||mTemp.trim()!=""){
    mTts.speak(mTemp, TextToSpeech.QUEUE_ADD,null);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if(mTts!=null){
        mTts.shutdown();
    }
}

@Override
protected void onStop() {
    super.onStop();
    if(mTts!=null){
        mTts.stop();
    }
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}
}

Upvotes: 1

user202690
user202690

Reputation:

It seems like this happened because the phone was attached to computer in USB debugging mode. Once disabled, TTS works perfectly!

Upvotes: 5

Related Questions