导致TTS初始化失败的原因是什么?

What are reasons that causes TTS initialization to fail?(导致TTS初始化失败的原因是什么?)
本文介绍了导致TTS初始化失败的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下观察到TTS初始化错误,这太随机了。

  • TTS引擎已安装,存在语音集,并且可以从辅助功能选项播放示例TTS。

  • TTS初始化在以前初始化和播放的同一设备上随机失败。

  • 在不同的设备(一加、华硕、三星)和各种Android操作系统版本中观察到此问题。

代码段:

公共类TTSHandler实现OnInitListener{

public EPUBReader context;
private TextToSpeech tts;
private HashMap<String, String> ttsMap;
private boolean isTtsPlaying = false,shouldResume = false,initialised = false,
            ttsModeEnabled = false;
private String textToRead;
private GoogleVersionPreferences mSharedPreferences;
private AudioManager mAudioManager;


@SuppressWarnings("deprecation")
public TTSHandler(EPUBReader context){

    this.context = context;
    tts = new TextToSpeech(context,this);

}

@Override
public void onInit(int status) {
    Log.d("TTS", "onInit called");
    if(status == TextToSpeech.SUCCESS){

        initialised = true;
        Log.d("TTS", "initialised success: status = "+status);
        initTTSComponents();

    }else{
        initialised = false;
        Log.d("TTS", " initialised failed: status ="+status);
    }

}

/**
 * called form JavascriptInterface
 * @param text is the sentence to be read
 */
public void readText(String text){

    if(text!=null){
        textToRead = text;
    }
    ttsMap = new HashMap<String, String>();
    ttsMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, textToRead);
    //for Next/Prev
    isTtsPlaying = true; 
    context.runOnUiThread(new Runnable(){
        @Override
        public void run() {
            context.toggleTtsIcons();
        }

    });

    tts.speak(textToRead,0, ttsMap);

}

/**
 * called to play,pause or resume tts play. 
 */
public void playPauseReading(){
    context.lockOrientationChange();
    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);

    if(isTtsPlaying){

        isTtsPlaying = false;
        shouldResume = true;
        tts.stop();

    }else{

        isTtsPlaying = true;

        if(shouldResume){
            shouldResume = false;
            requestAudioFocusForTTS();
            JavaScriptInterface.loadJavascript("javascript:resumeUsingTts()", context.getEPUBReaderFragment().getWebview());
        }else{
            requestAudioFocusForTTS();
            if(volume <= 1){
                Toast.makeText(context,context.getResources().getString(R.string.tts_volume_alert), 300).show();
            }
            int dir = mSharedPreferences.getEpubNavigationSetting(context.bookId);
            JavaScriptInterface.loadJavascript("javascript:playFromRange("+dir+")", context.getEPUBReaderFragment().getWebview());

        }
    }
    context.toggleTtsIcons();
}

/**
 * called to stop playing tts 
 * highlight will be removed and we will hide the tts controls
 */
public void stopReading(){

    if(tts !=null && ttsModeEnabled){
        isTtsPlaying = false;
        shouldResume = false;
        ttsModeEnabled = false;
        tts.stop();
        Toast.makeText(context,context.getResources().getString(R.string.tts_stop) , 300).show();
        JavaScriptInterface.loadJavascript("javascript:stopUsingTts()", 
                context.getEPUBReaderFragment().getWebview());
        abandonAudioFocus();
        context.runOnUiThread(new Runnable(){

            @Override
            public void run() {
                context.toggleControlPanel(false);
                context.unlockOrientationChange();
                context.invalidateOptionsMenu();
            }

        });


    }
}


/**
 * To check the whether tts is playing or not 
 * @return
 */
public boolean isReading(){
    if(tts.isSpeaking()){
        return true;
    }
        return false;
}

/**
 *  called from javascriptInterface at the end of a chapter
 *  
 */
public void stopReadingOnEndOfChapter(){

    Log.d("TTS", "stopReadingOnEndOfChapter");
    shouldResume = false;
    isTtsPlaying = false;
    ttsModeEnabled = false;
    abandonAudioFocus();
    context.runOnUiThread(new Runnable(){

        @Override
        public void run() {
            context.toggleControlPanel(false);
            context.unlockOrientationChange();
            context.invalidateOptionsMenu();
            Toast.makeText(context,context.getResources().getString(R.string.end_of_chapter) , 300).show();

        }});


}

/**
 * called from javascriptInterface to disable tts mode when the chapter doesnt have any text content.
 */
public void stopOnNoContent(){

    Toast.makeText(context,context.getResources().getString(R.string.tts_no_content) , 300).show();
    shouldResume = false;
    isTtsPlaying = false;
    ttsModeEnabled = false;
    abandonAudioFocus();
    context.runOnUiThread(new Runnable(){

        @Override
        public void run() {
            context.toggleControlPanel(false);
            context.unlockOrientationChange();
            context.invalidateOptionsMenu();

        }});
}

/**
 * called from javascript to stop tts while user taps on links
 */
public void stopTtsOnLinks(){

    if(isReading() || shouldResume)
        Toast.makeText(context,context.getResources().getString(R.string.tts_stop) , 300).show();

    shouldResume = false;
    isTtsPlaying = false;
    ttsModeEnabled = false;
    tts.stop();
    context.toggleControlPanel(false);
    context.unlockOrientationChange();
    abandonAudioFocus();

}

/**
 * Will release the resources used by the TTS engine 
 */
public void shutDownTTS(){
    if(tts !=null){
        tts.shutdown();
    }
}

/**
 * called from EPUBReader to set the speech rate
 * @param speechRate
 */
public void setSpeechRate(int value){

    switch(value){
        case 0:
            tts.setSpeechRate((float)0.1);
            break;
        case 1:
            tts.setSpeechRate((float)0.5);
            break;
        case 2:
            tts.setSpeechRate((float)1);
            break;
        case 3:
            tts.setSpeechRate((float)1.5);
            break;
        case 4:
            tts.setSpeechRate((float)2);
            break;

    }

}

/**
 * called form EPUBReader to start playing the next line 
 */
public void playNext(){
    isTtsPlaying = false;
    tts.stop();
    JavaScriptInterface.loadJavascript("javascript:readNext()",context.getEPUBReaderFragment().getWebview());
}

/**
 * called form EPUBReader to start playing the previous line
 */
public void playPrevious(){
    isTtsPlaying = false;
    tts.stop();
    JavaScriptInterface.loadJavascript("javascript:playPrevious()",context.getEPUBReaderFragment().getWebview());
}

/**
 * if TTS engine is initialised successfully,
 *  we will set the listener and other components.
 */
private void initTTSComponents(){

    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    tts.setSpeechRate(1);
    mSharedPreferences = GoogleVersionPreferences.getGoogleAppVersion(context);

    tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {

            @Override
            public void onStart(String utteranceId) {
                Log.d("TTS", "onStart:"+utteranceId);
            }

            @Override
            public void onError(String utteranceId) {
                Log.d("TTS", "onError ID:"+utteranceId);

            }

            @Override
            public void onDone(String utteranceId) {
                Log.d("BUG", "onDone, isTtsPlaying:"+isTtsPlaying);
                if(isTtsPlaying){
                    JavaScriptInterface.loadJavascript("javascript:readNext();",
                            ((EPUBReader) context).getEPUBReaderFragment().getWebview());
                }
            }

        });

}

/**
 * Requests audio focus so that when TTS starts 
 * other audio apps will be paused/ducked as per the app.
 */
private void requestAudioFocusForTTS(){

    mAudioManager.requestAudioFocus(null,AudioManager.STREAM_MUSIC , AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE);
}

/**
 *Will abandon the received audio Focus so that the 
 *previous app can attain it.
 */
private void abandonAudioFocus(){

    mAudioManager.abandonAudioFocus(null);
}




/**
 * getters and setters
 * @param value
 */
public void setisTtsPlaying(boolean value){
    isTtsPlaying = value;
}

public boolean isTtsPlaying(){
    return isTtsPlaying;
}

public void setshouldResume(boolean value){
     shouldResume = value;
}

public boolean shouldResume(){
    return shouldResume;
}

public boolean isTtsInitialised(){
    return initialised;
}

public boolean ttsModeEnabled(){
    return ttsModeEnabled;
}

public void setttsModeEnabled(boolean value){
    ttsModeEnabled = value;
}

}

注意:收集的日志:

对于三星设备(Tabs 10.5操作系统:5.0.2): E/SamsungTTS(20259):onLoadLanguage()-SamsungTTS尚未准备好。

一加一(操作系统:6.0.1):

08-08 18:54:53.112:w/TextToSpeech(22946):停止失败:TTS引擎连接未完全设置 08-08 18:56:15.869:I/TextToSpeech(22946):成功绑定com.google.android.tts 08-08 18:56:16.060:I/TextToSpeech(22946):连接到ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}

推荐答案

我的TTS初始化失败,因为我使用的是安卓11仿真器,它需要在安卓清单中说明这一点

<queries>
        <intent>
            <action android:name="android.intent.action.TTS_SERVICE" />
        </intent>
</queries>

这篇关于导致TTS初始化失败的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

The Google Mobile Ads SDK was initialized without AppMeasurement(Google 移动广告 SDK 在没有 AppMeasurement 的情况下进行了初始化)
No JNI_OnLoad found in ... skipping init(在 ... 中未找到 JNI_OnLoad 正在跳过初始化)
No JNI_OnLoad found skipping init gt; Application shutdown(没有找到跳过初始化的 JNI_OnLoad 应用程序关闭)
Fatal error: use of unimplemented initializer #39;init(coder:)#39; for class(致命错误:对类使用未实现的初始化程序“init(coder:))
Error in Swift class: Property not initialized at super.init call(Swift 类中的错误:在 super.init 调用时未初始化属性)
Conditional Binding: if let error – Initializer for conditional binding must have Optional type(条件绑定:if let error - 条件绑定的初始化程序必须具有可选类型)