华为 ML Kit 文字转语音错误

Posted

技术标签:

【中文标题】华为 ML Kit 文字转语音错误【英文标题】:Huawei ML Kit Text to Speech Conversion Error 【发布时间】:2021-12-01 00:06:34 【问题描述】:

我正在开发翻译应用程序,我需要说出用户翻译的内容。通过关注华为Text to Speech Doc,我得到了错误。

onError: MLTtsErrorerrorId=11301, errorMsg='不支持扬声器。 ', 扩展=7002

 protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_speak_and_translate);

        showInterstitialAd();
        MLApplication.getInstance().setApiKey("Your Key");


        if (deviceManufacture.equalsIgnoreCase("Huawei")) 
            setUpHuaweiTts();
         
        
    
    private void setUpHuaweiTts() 
        mlTtsConfig = new MLTtsConfig()
                // Set the text converted from speech to English.
                // MLTtsConstants.TtsEnUs: converts text to English.
                // MLTtsConstants.TtsZhHans: converts text to Chinese.
                .setLanguage(MLTtsConstants.TTS_EN_US)
                // Set the English timbre.
                // MLTtsConstants.TtsSpeakerFemaleEn: Chinese female voice.
                // MLTtsConstants.TtsSpeakerMaleZh: Chinese male voice.
                .setPerson(MLTtsConstants.TTS_SPEAKER_FEMALE_ZH)
                // Set the speech speed. Range: 0.2–1.8. 1.0 indicates 1x speed.
                .setSpeed(1.0f)
                // Set the volume. Range: 0.2–1.8. 1.0 indicates 1x volume.
                .setVolume(1.0f);
        mlTtsEngine = new MLTtsEngine(mlTtsConfig);
        mlTtsEngine.setTtsCallback(new MLTtsCallback() 
            @Override
            public void onError(String s, MLTtsError mlTtsError) 
                Log.d(TAG, "onError: "+ mlTtsError);
            

            @Override
            public void onWarn(String s, MLTtsWarn mlTtsWarn) 
                Log.d(TAG, "onWarn: ");
            

            @Override
            public void onRangeStart(String s, int i, int i1) 
                Log.d(TAG, "onRangeStart: ");
            

            @Override
            public void onAudioAvailable(String s, MLTtsAudioFragment mlTtsAudioFragment, int i, Pair<Integer, Integer> pair, Bundle bundle) 
                Log.d(TAG, "onAudioAvailable: ");
            

            @Override
            public void onEvent(String s, int i, Bundle bundle) 
                // Callback method of a TTS event. eventId indicates the event name.
                switch (i) 
                    case MLTtsConstants.EVENT_PLAY_START:
                        Log.d(TAG, "onEvent: Play");
                        // Called when playback starts.
                        break;
                    case MLTtsConstants.EVENT_PLAY_STOP:
                        // Called when playback stops.
                        boolean isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_PLAY_STOP_INTERRUPTED);
                        Log.d(TAG, "onEvent: Stop");
                        break;
                    case MLTtsConstants.EVENT_PLAY_RESUME:
                        // Called when playback resumes.
                        Log.d(TAG, "onEvent: Resume");      
                        break;
                    case MLTtsConstants.EVENT_PLAY_PAUSE:
                        // Called when playback pauses.
                        Log.d(TAG, "onEvent: Pause");
                        break;

                    // Pay attention to the following callback events when you focus on only synthesized audio data but do not use the internal player for playback:
                    case MLTtsConstants.EVENT_SYNTHESIS_START:
                        // Called when TTS starts.
                        Log.d(TAG, "onEvent: SynStart");
                        break;
                    case MLTtsConstants.EVENT_SYNTHESIS_END:
                        // Called when TTS ends.
                        Log.d(TAG, "onEvent: SynEnd");
                        break;
                    case MLTtsConstants.EVENT_SYNTHESIS_COMPLETE:
                        // TTS is complete. All synthesized audio streams are passed to the app.
                        boolean isInterruptedCheck = bundle.getBoolean(MLTtsConstants.EVENT_SYNTHESIS_INTERRUPTED);
                        Log.d(TAG, "onEvent: SynComplete");
                        break;
                    default:
                        break;
                
            
        );
       mlTtsEngine.speak("test", MLTtsEngine.QUEUE_APPEND);
    

目前,我只是为测试目的设置字符串“test”。我必须从模型中获取文本并将其设置为口语。我在有关扬声器错误的文档中看不到类似的内容。我已经搜索了 ErrorCode on Huawei。

public static final int ERR_ILLEGAL_PARAMETER

参数无效。

常数值:11301

LogCat:调试

LogCat:错误 任何帮助将非常感激。谢谢。

【问题讨论】:

【参考方案1】:

我用英语设置了错误的人。所以改变这条线的代码

.setPerson(MLTtsConstants.TTS_SPEAKER_FEMALE_ZH)

.setPerson(MLTtsConstants.TTS_SPEAKER_MALE_EN)

工作得很好。

【讨论】:

以上是关于华为 ML Kit 文字转语音错误的主要内容,如果未能解决你的问题,请参考以下文章

ML Kit iOS 人脸检测错误

华为mate7出现google文字转语音引擎运行已停止,经常弹出窗口,求解,重新下载也不行

ML Kit 条码扫描仪有时会收到错误的扫描

集成华为机器学习服务(ML Kit)轻松打造爆款小游戏

用华为HMS ML kit人体骨骼识别技术,Android快速实现人体姿势动作抓拍

安卓手机怎么关闭 文本转语音选项?