Xamarin Forms Text To Speech 无法在没有耳机的设备上播放
Xamarin Forms Text To Speech Wont Play On Device Without Headphones
我有一个使用文本到语音的 Xamarin Forms 应用程序。 iOS 项目中的代码与 Xamarin Implementing Text To Speech 官方指南中的代码完全相同
var speechSynthesizer = new AVSpeechSynthesizer ();
var speechUtterance = new AVSpeechUtterance (text) {
Rate = AVSpeechUtterance.MaximumSpeechRate/4,
Voice = AVSpeechSynthesisVoice.FromLanguage ("en-US"),
Volume = 0.5f,
PitchMultiplier = 1.0f
};
speechSynthesizer.SpeakUtterance (speechUtterance);
这在 iOS 模拟器中效果很好,但是当我部署到我的 iPad 运行 iOS 10.2.1 时,如果我戴上耳机,我只会听到任何声音.我已经把音量调高了,但没有耳机还是什么都没有,如果我确实使用耳机虽然它非常响亮所以暗示它不仅仅是太安静。其他应用程序可以通过我的 iPad 扬声器正常播放音频,所以我认为这不是硬件问题。
我也试过使用这个插件 (https://github.com/jamesmontemagno/TextToSpeechPlugin) 进行文本到语音转换,但是当我部署到设备时它有完全相同的问题。
有什么原因可以在耳机上使用但在没有插入耳机时会失败?
我看不出您的代码无法正常工作的明显原因。我唯一的解释是 'silent mode' 在您的 iDevice 上激活。要关闭此功能,请使用左侧的开关,或者对于较新的设备,通过从屏幕底部向上滑动并按铃铛图标进入 control center。
您还注意到其他应用 会产生声音。作为开发人员,您可以选择忽略静音模式并播放声音,虽然这应该被认为是不好的做法,但 Apple 允许这样做。但是,默认情况下未启用它,因此使用代码,您的声音将被静音模式静音。
似乎无法绕过静音模式,但您可以检测到它并通知用户当静音开关打开时文本转语音不工作。
这是受 SoundSwitch library. On iPhone 8, the await
takes 160-250ms for a 50ms-long mute.caf
but only 3-8ms when the silent switch is on. You can download the 50ms mute.caf
from this Gist 启发的解决方案。
using System;
using Foundation;
using AudioToolbox;
using System.Threading.Tasks;
public class SilentSwitch
{
readonly SystemSound _sound;
public SilentSwitch()
{
var url = NSBundle.MainBundle.GetUrlForResource("mute", "caf");
_sound = new SystemSound(url);
_sound.IsUISound = true;
}
public async Task<bool> IsEnabled()
{
var startTime = DateTime.Now;
await _sound.PlaySystemSoundAsync();
var elapsedTime = DateTime.Now - startTime;
return elapsedTime.TotalMilliseconds < 50;
}
}
我有一个使用文本到语音的 Xamarin Forms 应用程序。 iOS 项目中的代码与 Xamarin Implementing Text To Speech 官方指南中的代码完全相同
var speechSynthesizer = new AVSpeechSynthesizer ();
var speechUtterance = new AVSpeechUtterance (text) {
Rate = AVSpeechUtterance.MaximumSpeechRate/4,
Voice = AVSpeechSynthesisVoice.FromLanguage ("en-US"),
Volume = 0.5f,
PitchMultiplier = 1.0f
};
speechSynthesizer.SpeakUtterance (speechUtterance);
这在 iOS 模拟器中效果很好,但是当我部署到我的 iPad 运行 iOS 10.2.1 时,如果我戴上耳机,我只会听到任何声音.我已经把音量调高了,但没有耳机还是什么都没有,如果我确实使用耳机虽然它非常响亮所以暗示它不仅仅是太安静。其他应用程序可以通过我的 iPad 扬声器正常播放音频,所以我认为这不是硬件问题。
我也试过使用这个插件 (https://github.com/jamesmontemagno/TextToSpeechPlugin) 进行文本到语音转换,但是当我部署到设备时它有完全相同的问题。
有什么原因可以在耳机上使用但在没有插入耳机时会失败?
我看不出您的代码无法正常工作的明显原因。我唯一的解释是 'silent mode' 在您的 iDevice 上激活。要关闭此功能,请使用左侧的开关,或者对于较新的设备,通过从屏幕底部向上滑动并按铃铛图标进入 control center。
您还注意到其他应用 会产生声音。作为开发人员,您可以选择忽略静音模式并播放声音,虽然这应该被认为是不好的做法,但 Apple 允许这样做。但是,默认情况下未启用它,因此使用代码,您的声音将被静音模式静音。
似乎无法绕过静音模式,但您可以检测到它并通知用户当静音开关打开时文本转语音不工作。
这是受 SoundSwitch library. On iPhone 8, the await
takes 160-250ms for a 50ms-long mute.caf
but only 3-8ms when the silent switch is on. You can download the 50ms mute.caf
from this Gist 启发的解决方案。
using System;
using Foundation;
using AudioToolbox;
using System.Threading.Tasks;
public class SilentSwitch
{
readonly SystemSound _sound;
public SilentSwitch()
{
var url = NSBundle.MainBundle.GetUrlForResource("mute", "caf");
_sound = new SystemSound(url);
_sound.IsUISound = true;
}
public async Task<bool> IsEnabled()
{
var startTime = DateTime.Now;
await _sound.PlaySystemSoundAsync();
var elapsedTime = DateTime.Now - startTime;
return elapsedTime.TotalMilliseconds < 50;
}
}