【问题标题】:When TextToSpeech synthesizeToFile is completed?TextToSpeech synthesizeToFile 什么时候完成?
【发布时间】:2019-06-14 06:53:18
【问题描述】:

我需要致电synthesizeToFile 来创建一些音频文件,并且我需要知道它何时完成了所有这些文件的创建,以便通知用户。

我已经阅读了其他问题,但没有找到适合我的问题,或者该问题与 speak 功能有关。

build.gradle(模块:app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.domain.appName"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

MainActivity.java:

public class MainActivity extends AppCompatActivity {
TextToSpeech tts;

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

@Override
protected void onResume() {
    super.onResume();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        Map<String, String> mapFilesToGenerate = new HashMap<>();

        List<MyEnum> listMyEnum = new ArrayList<>(Arrays.asList(MyEnum.values()));
        for (MyEnum e : listMyEnum)
        {
            File file = new File(getApplicationContext().getFilesDir() + "/" + e.id);
            if (!file.exists())
            {
                mapFilesToGenerate.put(e.id, e.name);
            }
        }

        // this Toast will be shown
        Toast.makeText(getApplicationContext(), "Preparing audio files...", Toast.LENGTH_SHORT).show();
        Log.d("testTTS", "Preparing audio files...");

        tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener()
        {
            @Override
            public void onInit(int status)
            {
                if (status == TextToSpeech.SUCCESS)
                {
                    tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
                    {
                        // **********
                        // all Toasts and Logs in this block aren't showed
                        // **********

                        @Override
                        public void onStart(String utteranceId) {
                            Toast.makeText(getApplicationContext(), "generating audio files... (" + mapFilesToGenerate.size() + " files)", Toast.LENGTH_LONG).show();
                            Log.d("testTTS", "generating audio files... (" + mapFilesToGenerate.size() + " files)");
                        }

                        @Override
                        public void onDone(String utteranceId) {
                            Toast.makeText(getApplicationContext(), "Generation complete.", Toast.LENGTH_LONG).show();
                            Log.d("testTTS", "Generation complete.");
                            new Thread()
                            {
                                public void run()
                                {
                                    MainActivity.this.runOnUiThread(new Runnable()
                                    {
                                        public void run()
                                        {

                                            Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();
                                            Log.d("testTTS", "TTS Completed");
                                        }
                                    });
                                }
                            }.start();
                        }

                        @Override
                        public void onError(String utteranceId) {
                            Log.d("testTTS", "error.");
                        }
                    });

                    generateAudioFiles("en", mapFilesToGenerate);

                } else {
                    Toast.makeText(getApplicationContext(), "TTS inizialization failed! status = " + status, Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

private void generateAudioFiles(Map<String, String> mapFilesToGenerate)
{
    for (Map.Entry<String, String> entry : mapFilesToGenerate.entrySet())
    {
        saveAudioFileWithTTS("en", entry.getValue(), entry.getKey());
    }
}

private boolean saveAudioFileWithTTS(String text, String pathFileName)
{
    int returnCode = -2;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        Locale locale = new Locale("en");

        tts.setLanguage(locale);

        tts.setPitch(1.2f);
        tts.setSpeechRate(0.8f);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tts.setVoice(new Voice("nomeVoce", locale, Voice.QUALITY_VERY_HIGH, Voice.LATENCY_NORMAL, false, null));
        }

        String completePath = getApplicationContext().getFilesDir().getAbsolutePath() + "/" + pathFileName;

        File fileToCreate = new File(completePath);
        returnCode = tts.synthesizeToFile
            (
                text
                , null
                , fileToCreate
                , pathFileName
            );
    }

    return returnCode == 0;
}

为什么没有显示setOnUtteranceProgressListener 中的所有 Toast 和日志?

我做错了什么?有什么我没有考虑的吗?

谢谢

【问题讨论】:

    标签: java android logging listener text-to-speech


    【解决方案1】:

    我会说以下任何一种都是可能的:

    A) 调用

    generateAudioFiles("en", mapFilesToGenerate);
    

    设置 UtteranceProgressListener 之后太快了。

    B) 不必要地使用 'new Thread()' 时只

    runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();
        }
    });
    Log.d("testTTS", "TTS Completed");
    

    是必须的。

    C) 在所有 UtteranceProgressListener 回调中不使用上述技术 (B)(因为所有这些回调都是在后台线程上调用的,除非您像这样包装代码,否则不会显示 toast)。

    D) 您使用的 TTS 引擎根本无法准确报告 synthesizeToFile() 的完成

    ...您还可以检查 synthesizeToFile() 的返回值——如果它返回 false,那么合成根本没有发生,这说明没有调用回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多