【问题标题】:Problems with trying to detect if an iOS phone is on silent?尝试检测 iOS 手机是否处于静音状态时遇到问题?
【发布时间】: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


    【解决方案1】:

    您可以使用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.

    【讨论】:

    • 等待全面检查。完成后会更新。感谢您的帮助。
    • 退房需要一些时间。然后会标记。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 2012-05-19
    • 2015-10-27
    相关资源
    最近更新 更多