wxr
2020-07-06 23c075a9c27946773feccf05abc90489a6bf5203
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using AVFoundation;
using Foundation;
using MediaPlayer;
using Shared;
using UIKit;
 
namespace com.hdl.on
{
    public static class Volume
    {
        /// <summary>
        /// 声音变化事件
        /// </summary>
        public static Action<int> VolumeChange;
        /// <summary>
        /// 设置当前音量
        /// </summary>
        /// <param name="volume">Volume.</param>
        public static int MusicVolume
        {
            set
            {
                if ((DateTime.Now - dateTime).TotalMilliseconds < 2000)
                {
                    return;
                }
                foreach (var view in BaseViewController.MPVolumeView.Subviews)
                {
                    if (view.GetType().Name == "UISlider")
                    {
                        (view as UISlider).SetValue((float)Math.Round(value / 100.0f, 2, MidpointRounding.AwayFromZero), false);
                        beforeVolume = (view as UISlider).Value;
                        //Shared.HDLUtils.WriteLine("接收到更新音量:" + (float)Math.Round(value / 100.0f, 2, MidpointRounding.AwayFromZero) + "  " + beforeVolume);
                        break;
                    }
                }
            }
        }
        static System.DateTime dateTime = DateTime.MinValue;
        /// <summary>
        /// 之前的音量
        /// </summary>
        static float beforeVolume;
        public static void Init()
        {
            NSNotificationCenter.DefaultCenter.AddObserver(new NSString("AVSystemController_SystemVolumeDidChangeNotification"),
                                                                       (obj) =>
                                                                       {
                                                                           //如果应用进入后台,不执行音量变化
                                                                           if (UIApplication.SharedApplication.ApplicationState != UIApplicationState.Active || "Audio/Video" != obj.UserInfo.ObjectForKey(new NSString("AVSystemController_AudioCategoryNotificationParameter")).ToString())
                                                                           {
                                                                               return;
                                                                           }
                                                                           //Shared.HDLUtils.WriteLine("接收到音量变化事件");
                                                                           dateTime = DateTime.Now;
                                                                           var tempValue = obj.UserInfo.ObjectForKey(new NSString("AVSystemController_AudioVolumeNotificationParameter"));
                                                                           if (tempValue == null)
                                                                           {
                                                                               return;
                                                                           }
                                                                           try
                                                                           {
                                                                               //有时转换失败,可能有时值有问题
                                                                               var volume = 100 * float.Parse(tempValue.ToString());
                                                                               volume = (float)Math.Round(volume, 2, MidpointRounding.AwayFromZero);
                                                                               //Shared.HDLUtils.WriteLine("音量变化事件:" + volume + "  " + beforeVolume * 100);
                                                                               if (VolumeChange != null && beforeVolume * 100 != volume)
                                                                               {
                                                                                   VolumeChange((int)volume);
                                                                               }
                                                                           }
                                                                           catch { }
                                                                       });
        }
    }
}