gpt4 book ai didi

c# - 尝试检测 iOS 手机是否处于静音状态时遇到问题?

转载 作者:行者123 更新时间:2023-12-04 09:38:59 26 4
gpt4 key购买 nike

我正在调用此方法来尝试确定 iOS 设备是否处于静音状态。但是,即使我在此处放置断点“silentText =“Silent Mode On”,它似乎也无法检测到它。然后它会到达该点。但是它仍然返回一个空字符串。

有没有人知道这种返回 silentText 值的方式可能会出什么问题? .我认为这可能与我运行和从 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;
}

最佳答案

您可以使用 TaskCompletionSource返回异步方法的结果。

修改实现如下

   public Task<string> IsDeviceSilent()
{

var tcs = new TaskCompletionSource<string>();

AVAudioSession.SharedInstance().SetActive(true);
var outputVolume = AVAudioSession.SharedInstance().OutputVolume;
if ((int)(outputVolume * 100) == 0) {
string result = "Phone volume is set to silent. Please adjust volume higher if you want the phrases and meanings to be read aloud.";
tcs.SetResult(result);
}
else if (outputVolume * 100 < 30)
{
string result = "Phone volume set low. Please adjust volume higher if you want the phrases and meanings to be read aloud.";
tcs.SetResult(result);
}
else
{
var soundFilePath = NSBundle.MainBundle.PathForResource("Audio/mute", "caf");
var sound = new SystemSound(new NSUrl(soundFilePath, false));
DateTime startTime = DateTime.Now;

sound.AddSystemSoundCompletion(() =>
{
var endTime = DateTime.Now;
var timeDelta = (endTime - startTime).Milliseconds;

string silentText = null;

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";
}

tcs.SetResult(silentText);
});
sound.PlaySystemSound();

}

return tcs.Task;
}

调用表单
  SilentModeText = await DependencyService.Get<ISoundMethods>().IsDeviceSilent();

引用: https://stackoverflow.com/a/51318940/8187800 .

关于c# - 尝试检测 iOS 手机是否处于静音状态时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62413859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com