【发布时间】:2020-06-17 10:09:55
【问题描述】:
我正在调用此方法来尝试确定 iOS 设备是否处于静音状态。但是,即使我在此处设置断点“silentText = “Silent Mode On”,它似乎也无法检测到它。然后它会转到该点。但是它仍然返回一个空字符串。
有没有人知道这种返回silentText 值的方式可能出了什么问题。我认为这可能与我运行并从任务返回值的方式有关。
SilentModeText = await DependencyService.Get<ISoundMethods>().IsDeviceSilent();
public async Task<string> IsDeviceSilent()
{
AVAudioSession.SharedInstance().SetActive(true);
var outputVolume = AVAudioSession.SharedInstance().OutputVolume;
if ((int)(outputVolume * 100) == 0)
return "Phone volume is set to silent. Please adjust volume higher if you want the phrases and meanings to be read aloud.";
else if (outputVolume * 100 < 30)
return "Phone volume set low. Please adjust volume higher if you want the phrases and meanings to be read aloud.";
var soundFilePath = NSBundle.MainBundle.PathForResource("Audio/mute", "caf");
var sound = new SystemSound(new NSUrl(soundFilePath, false));
DateTime startTime = DateTime.Now;
var silentText = "";
sound.AddSystemSoundCompletion(async () =>
{
var endTime = DateTime.Now;
var timeDelta = (endTime - startTime).Milliseconds;
if (timeDelta < 100)
{
silentText = "Silent Mode On. Please turn off the Silent Mode switch at the top left side of the phone if you want to listen to the phrases and meanings using the phone speaker.";
}
else {
silentText = "Silent Mode Off";
}
});
sound.PlaySystemSound();
return silentText;
}
【问题讨论】:
标签: c# xamarin xamarin.forms