bra*_*all 3 java android text-to-speech saiy
当用户将网络合成语音设置为默认值时,我的应用程序会缓存常见的请求以防止未来的网络延迟。
我如何做到这一点,是体现在我的代码在这里。简而言之,它将引擎名称和话语与请求匹配,如果它们匹配,我会流式传输缓存的音频而不是使用 TTS 引擎。
如果用户随后在 Android 文本到语音设置中更改了他们的音调和语速,则缓存的音频当然将不再反映这一点,并且需要被忽略并“重新缓存”,或者相应地处理音频。
我的问题:如何获得系统默认设置,用于音调和语速。
要设置音调和速率,TextToSpeech API中提供了公开的方法:
/**
* Sets the speech rate.
*
* This has no effect on any pre-recorded speech.
*
* @param speechRate Speech rate. {@code 1.0} is the normal speech rate,
* lower values slow down the speech ({@code 0.5} is half the normal speech rate),
* greater values accelerate it ({@code 2.0} is twice the normal speech rate).
*
* @return {@link #ERROR} or {@link #SUCCESS}.
*/
public int setSpeechRate(float speechRate) {
if (speechRate > 0.0f) {
int intRate = (int)(speechRate * 100);
if (intRate > 0) {
synchronized (mStartLock) {
mParams.putInt(Engine.KEY_PARAM_RATE, intRate);
}
return SUCCESS;
}
}
return ERROR;
}
/**
* Sets the speech pitch for the TextToSpeech engine.
*
* This has no effect on any pre-recorded speech.
*
* @param pitch Speech pitch. {@code 1.0} is the normal pitch,
* lower values lower the tone of the synthesized voice,
* greater values increase it.
*
* @return {@link #ERROR} or {@link #SUCCESS}.
*/
public int setPitch(float pitch) {
if (pitch > 0.0f) {
int intPitch = (int)(pitch * 100);
if (intPitch > 0) {
synchronized (mStartLock) {
mParams.putInt(Engine.KEY_PARAM_PITCH, intPitch);
}
return SUCCESS;
}
}
return ERROR;
}
Run Code Online (Sandbox Code Playgroud)
鉴于上述方法都导致将值放入Private Bundle绑定的 TTS 引擎(链接):
private final Bundle mParams = new Bundle();
Run Code Online (Sandbox Code Playgroud)
我使用反射来查看这些值是否被绑定引擎默认/持久化或注入。下面是一个精简的例子,其中Class extends TextToSpeech:
private int getSpeechRate() {
Bundle reflectBundle;
try {
final Field f = this.getClass().getSuperclass().getDeclaredField(TTSDefaults.BOUND_PARAMS);
f.setAccessible(true);
reflectBundle = (Bundle) f.get(this);
if (reflectBundle != null && !reflectBundle.isEmpty()) {
examineBundle(reflectBundle);
if (reflectBundle.containsKey(TTSDefaults.KEY_PARAM_RATE)) {
final int reflectRate = reflectBundle.getInt(TTSDefaults.KEY_PARAM_RATE);
// log
return reflectRate;
} else {
// missing
}
} else {
// empty or null
}
} catch (final NoSuchFieldException e) {
} catch (final IllegalAccessException e) {
} catch (final NullPointerException e) {
}
return -1;
}
/**
* For debugging the bundle extras
*
* @param bundle containing potential extras
*/
private void examineBundle(@Nullable final Bundle bundle) {
if (bundle != null) {
final Set<String> keys = bundle.keySet();
//noinspection Convert2streamapi
for (final String key : keys) {
Log.v(CLS_NAME, "examineBundle: " + key + " ~ " + bundle.get(key));
}
}
}
Run Code Online (Sandbox Code Playgroud)
这些值丢失了,因此,也许可以理解,这不是它们“全局”持久化的方式。
当我第一次尝试解决这个问题时,我认为这很简单——我希望事实确实如此,但我就是见不到树木...
感谢您阅读到这里 - 帮助!
首先,我假设您熟悉 TextToSpeech 源代码。
我认为您无法通过使用反射来获得音高和语速。这些值存储在 TextToSpeech 类的实例变量中,每次查询文本时都会将它们提供给引擎。这意味着当您调用setSpeechRate或setPitch方法时,它们不会改变全局文本到语音设置的音调和语速。
除此之外,音调和语速是在 Android 安全系统设置中定义的,用户必须通过系统 UI 或专门的 API(系统应用程序或 root 访问)为这些值明确修改这些首选项,而不是由应用程序直接修改。
使用以下代码阅读与音调和语速相关的安全设置:
Settings.Secure.getInt(getContentResolver(), Settings.Secure.TTS_DEFAULT_RATE);
Settings.Secure.getInt(getContentResolver(), Settings.Secure.TTS_DEFAULT_PITCH);
Run Code Online (Sandbox Code Playgroud)
在Google Talkback和TextToSpeechSettings 中可以看到略有不同的用法。
Settings.SettingNotFoundException如果没有设置任何值,则会抛出环绕,在这种情况下,您可以回退到隐藏的等效值 TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_PITCH
或者,您可以添加一个额外的参数作为默认值。
Settings.Secure.getInt(getContentResolver(), Settings.Secure.TTS_DEFAULT_PITCH, FALLBACK_TTS_DEFAULT_PITCH);
Run Code Online (Sandbox Code Playgroud)