I am calling this method to try and determine if an iOS device is silent. However it does not seem to be able to detect it even though if I put a breakpoint here "silentText = "Silent Mode On." then it goes to that point. However it still returns an empty string.
Does anyone have any ideas what might be going wrong with this way of returning the value of silentText. I think possibly this is something to do with the way I am running and returning the value from the Task.
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;
}