Android - TTS missing voice

I am trying to implement a TTS application for Android. Here is the code that I have 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 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 the NetBeans ADB log, you 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 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

Obviously, the text that I am trying to output is sent from the journal, however I don’t hear anything from my phone. What could be the problem?

+4
source share
5 answers

This seems to be because the phone was connected to the computer in USB debugging mode. After disconnecting, TTS works fine!

+4
source

Your code is more like android doc. I just wrote this demo code. I believe that if you replace requestCodewith resultCodein the next 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();
}
}
+1

SpeechSynthesis. " " TTS ( ), . , SpeechSynthesis . .

, TTS, , , , Android 4.0.4. , .

"" - : ' > > " " "" Google Text-to-speech Engine " " setup" RHS . : (1) "", (2) " Google TTS" " " " Wi-Fi" *, , , (3) " '; (Wi-Fi )

stackoverflow , , TTS . , .

+1

, , :

- > - > .

: () ().
: (IE).

0

Raspberry pi 3, Android, TTS USB-, TextToSpeech.setAudioAttributes(AudioAttributes) TextToSpeech AudioAttributes.FLAG_AUDIVILITY_ENFORCED, :

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();
        }
    }

}
0

Source: https://habr.com/ru/post/1796479/


All Articles