gpt4 book ai didi

java - TextToSpeech SynthesizeToFile 何时完成?

转载 作者:行者123 更新时间:2023-12-02 04:29:52 27 4
gpt4 key购买 nike

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

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

build.gradle(模块:应用程序):

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 和日志都没有显示?

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

谢谢

最佳答案

我想说以下任何一种情况都是可能的:

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,则合成根本没有发生,这将解释没有调用回调。

关于java - TextToSpeech SynthesizeToFile 何时完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56592935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com