当用户将网络合成语音设置为默认语音时,我的应用程序会缓存常见请求以防止将来出现网络延迟。
我是如何做到的,在我的代码here中进行了演示。简而言之,它将引擎名称和话语与请求相匹配,如果它们匹配,我流式传输缓存的音频而不是使用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;
}
鉴于上述方法都导致将值放入绑定的TTS引擎(Private Bundle
)的link中:
private final Bundle mParams = new Bundle();
我使用反射来查看这些值是否默认/持久或由绑定引擎注入。下面是一个简洁的例子,其中the Class扩展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));
}
}
}
价值观缺失,因此,也许可以理解,这并不是“全球”持续存在的方式。
当我第一次尝试解决这个问题时,我认为这将是微不足道的 - 我希望事实证明是这样的,但我只是看不到木头的树木......
感谢您阅读这篇文章 - 帮助!
首先,我假设您熟悉TextToSpeech源代码。
我认为你不能通过反射来获得音高和语速。这些值存储在TextToSpeech类的实例变量中,并且每次查询文本时都会将它们提供给引擎。这意味着当您调用setSpeechRate
或setPitch
方法时,它们不会改变全局“文本到语音设置”的音高和语速。
除此之外,pitch和speech rate在Android安全系统设置中定义,这些设置是用户必须通过系统UI或专用API(系统应用程序或root访问权限)为这些值显式修改的首选项,而不是由应用程序直接修改。
使用以下代码阅读与音高和语速相关的安全设置:
Settings.Secure.getInt(getContentResolver(), Settings.Secure.TTS_DEFAULT_RATE);
Settings.Secure.getInt(getContentResolver(), Settings.Secure.TTS_DEFAULT_PITCH);
在Google Talkback和TextToSpeechSettings中可以看到略有不同的用法。
如果没有设置值,将会抛出Settings.SettingNotFoundException
,在这种情况下,您可以回退到the hidden TextToSpeech.Engine.FALLBACK_TTS_DEFAULT_PITCH
的等效值
或者,您可以添加额外参数作为默认值。
Settings.Secure.getInt(getContentResolver(), Settings.Secure.TTS_DEFAULT_PITCH, FALLBACK_TTS_DEFAULT_PITCH);
它在document说
语速。 1.0是正常的语速,较低的值使语音减慢(0.5是正常语速的一半),较大的值加速它(2.0是正常语速的两倍)。
演讲间距。 1.0是正常音调,较低的值会降低合成语音的音调,较大的值会增加它。
因此,setPitch((float) 1.0)
和setSpeechRate((float) 1.0)
设置默认音高和语速。
如果您需要从设备设置到TextToSpeech设置音高,请尝试此操作:
float pitch = Settings.Secure.getFloat(context.getContentResolver(), Settings.Secure.TTS_DEFAULT_PITCH);
textToSpeech.setPitch((float) (pitch / 100.0));