TextToSpeech android API 22 不播放音频
TextToSpeech android API 22 not playing audio
我在我的应用程序中使用文本到语音引擎。它在 API 23 及更高版本的模拟器 Nexus 6 上运行良好。但是在带有 API 22 的模拟器 Nexus 6 上它不会说话。
两个模拟器都使用 Pico TTS 作为首选引擎。
我的 activity 布局只包含一个按钮 "Speak"。
这是我的 activity 代码:
public class MainActivity extends AppCompatActivity {
private TextToSpeech mTTS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button speakBtn = findViewById(R.id.button);
speakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
});
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTTS.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language not supported");
}
mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.d("TTS", "onStart called, utteranceId = " + utteranceId);
}
@Override
public void onDone(String utteranceId) {
Log.d("TTS", "onDone called, utteranceId = " + utteranceId);
}
@Override
public void onError(String utteranceId) {
Log.d("TTS", "onError called, utteranceId = " + utteranceId);
}
});
} else {
Log.e("TTS", "Initialization failed");
}
}
});
}
private void speak() {
Log.d("TTS", "speak() method called");
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "greeting");
mTTS.speak("hello", TextToSpeech.QUEUE_FLUSH, map);
}
}
这是来自模拟器 Nexus 6 的所有日志 API 22:
02-18 13:54:09.942 9739-9739/? E/libprocessgroup: make 和 chown 失败 /acct/uid_10059: 只读文件系统
02-18 13:54:09.943 9739-9739/? W/Zygote: createProcessGroup 失败,缺少内核 CONFIG_CGROUP_CPUACCT?
02-18 13:54:09.943 9739-9739/? I/art: 未延迟启用 -Xcheck:jni(已启用)
02-18 13:54:09.961 9739-9748/? E/art:向调试器发送回复失败:管道损坏
02-18 13:54:09.961 9739-9748/? I/art: 调试器不再活动
02-18 13:54:09.992 9739-9739/? W/art:在 Android 4.1 之前,方法 android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) 会被错误地覆盖android.graphics.drawable.Drawable
中的 package-private 方法
02-18 13:54:10.001 9739-9739/? I/art:拒绝重新初始化之前失败的 class java.lang.Class
02-18 13:54:10.001 9739-9739/? I/art:拒绝重新初始化之前失败的 class java.lang.Class
02-18 13:54:10.064 9739-9739/? I/TextToSpeech: 成功绑定到com.svox.pico
02-18 13:54:10.072 9739-9757/? D/OpenGLRenderer: 使用 EGL_SWAP_BEHAVIOR_PRESERVED: true
02-18 13:54:10.074 9739-9739/? D/Atlas:正在验证地图...
02-18 13:54:10.093 9739-9739/? I/TextToSpeech: 连接到 ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
02-18 13:54:10.096 9739-9758/? I/TextToSpeech: 建立到 ComponentInfo 的连接{com.svox.pico/com.svox.pico.PicoService}
02-18 13:54:10.111 9739-9757/? I/OpenGLRenderer:初始化 EGL,版本 1.4
02-18 13:54:10.111 9739-9757/? W/OpenGLRenderer: 使用 EGL_SWAP_BEHAVIOR_PRESERVED 选择配置失败,不使用...
重试
02-18 13:54:10.128 9739-9757/? D/EGL_emulation: eglCreateContext: 0xae434e20: maj 2 min 0 rcv 2
02-18 13:54:10.131 9739-9757/? D/EGL_emulation: eglMakeCurrent: 0xae434e20: ver 2 0
02-18 13:54:10.134 9739-9757/? D/OpenGLRenderer: 启用调试模式 0
02-18 13:54:10.174 9739-9757/? D/EGL_emulation: eglMakeCurrent: 0xae434e20: ver 2 0
02-19 09:32:25.570 9739-9739/com.example.ttsapp D/TTS:调用了 speak() 方法
想出了解决问题的方法。如果不是通过调试或执行启动模拟器,而是在启动 Android Studio 后从 AVD Manager 启动模拟器,一切正常。
我在我的应用程序中使用文本到语音引擎。它在 API 23 及更高版本的模拟器 Nexus 6 上运行良好。但是在带有 API 22 的模拟器 Nexus 6 上它不会说话。 两个模拟器都使用 Pico TTS 作为首选引擎。
我的 activity 布局只包含一个按钮 "Speak"。 这是我的 activity 代码:
public class MainActivity extends AppCompatActivity {
private TextToSpeech mTTS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button speakBtn = findViewById(R.id.button);
speakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
});
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTTS.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language not supported");
}
mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.d("TTS", "onStart called, utteranceId = " + utteranceId);
}
@Override
public void onDone(String utteranceId) {
Log.d("TTS", "onDone called, utteranceId = " + utteranceId);
}
@Override
public void onError(String utteranceId) {
Log.d("TTS", "onError called, utteranceId = " + utteranceId);
}
});
} else {
Log.e("TTS", "Initialization failed");
}
}
});
}
private void speak() {
Log.d("TTS", "speak() method called");
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "greeting");
mTTS.speak("hello", TextToSpeech.QUEUE_FLUSH, map);
}
}
这是来自模拟器 Nexus 6 的所有日志 API 22:
02-18 13:54:09.942 9739-9739/? E/libprocessgroup: make 和 chown 失败 /acct/uid_10059: 只读文件系统
02-18 13:54:09.943 9739-9739/? W/Zygote: createProcessGroup 失败,缺少内核 CONFIG_CGROUP_CPUACCT?
02-18 13:54:09.943 9739-9739/? I/art: 未延迟启用 -Xcheck:jni(已启用)
02-18 13:54:09.961 9739-9748/? E/art:向调试器发送回复失败:管道损坏
02-18 13:54:09.961 9739-9748/? I/art: 调试器不再活动
02-18 13:54:09.992 9739-9739/? W/art:在 Android 4.1 之前,方法 android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) 会被错误地覆盖android.graphics.drawable.Drawable
中的 package-private 方法02-18 13:54:10.001 9739-9739/? I/art:拒绝重新初始化之前失败的 class java.lang.Class
02-18 13:54:10.001 9739-9739/? I/art:拒绝重新初始化之前失败的 class java.lang.Class
02-18 13:54:10.064 9739-9739/? I/TextToSpeech: 成功绑定到com.svox.pico
02-18 13:54:10.072 9739-9757/? D/OpenGLRenderer: 使用 EGL_SWAP_BEHAVIOR_PRESERVED: true
02-18 13:54:10.074 9739-9739/? D/Atlas:正在验证地图...
02-18 13:54:10.093 9739-9739/? I/TextToSpeech: 连接到 ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
02-18 13:54:10.096 9739-9758/? I/TextToSpeech: 建立到 ComponentInfo 的连接{com.svox.pico/com.svox.pico.PicoService}
02-18 13:54:10.111 9739-9757/? I/OpenGLRenderer:初始化 EGL,版本 1.4
02-18 13:54:10.111 9739-9757/? W/OpenGLRenderer: 使用 EGL_SWAP_BEHAVIOR_PRESERVED 选择配置失败,不使用...
重试02-18 13:54:10.128 9739-9757/? D/EGL_emulation: eglCreateContext: 0xae434e20: maj 2 min 0 rcv 2
02-18 13:54:10.131 9739-9757/? D/EGL_emulation: eglMakeCurrent: 0xae434e20: ver 2 0
02-18 13:54:10.134 9739-9757/? D/OpenGLRenderer: 启用调试模式 0
02-18 13:54:10.174 9739-9757/? D/EGL_emulation: eglMakeCurrent: 0xae434e20: ver 2 0
02-19 09:32:25.570 9739-9739/com.example.ttsapp D/TTS:调用了 speak() 方法
想出了解决问题的方法。如果不是通过调试或执行启动模拟器,而是在启动 Android Studio 后从 AVD Manager 启动模拟器,一切正常。