陈嘉乐
2020-05-26 c209a50cabf524c146fb2d02ce09cfc46c7c2f70
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using Shared;
using Shared.IO;
using System.Net;
namespace HDL_ON.UI.Music
{
    public class MusicMain : FrameLayout
    {
 
        //FrameLayout Frame = null;
        //public MusicListViewMain() {
        //    Frame = this;
        //}
        public override void RemoveFromParent()
        {
            base.RemoveFromParent();
            clearA31Threads();
        }
        static List<System.Threading.Thread> threadLists = new List<System.Threading.Thread>();
 
        static void clearA31Threads()
        {
            var threads = threadLists.FindAll((obj) => { return obj.Name == "A31"; });
            foreach (var thread in threads)
            {
                try
                {
                    threadLists.Remove(thread);
                    if (thread.IsAlive)
                    {
                        thread.Abort();
                    }
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.Message);
                }
            }
        }
        /// <summary>
        ///  显示加载界面
        /// </summary>
        public static Loading Loading = new Loading();
        VerticalRefreshLayout verticalRefresh;
 
        public void Show()
        {
 
 
            #region   界面布局
            this.BackgroundColor = Color.ViewColor;
            var topView = new TopView();
            this.AddChidren(topView.TopFLayoutView());
            topView.topNameBtn.TextID =StringId.a31Music;
            topView.clickBackBtn.MouseUpEventHandler += (sender, e) =>
            {
                RemoveFromParent();
            };
 
            verticalRefresh = new VerticalRefreshLayout
            {
                Y = topView.fLayout.Bottom,
                Height = Application.GetRealHeight(H_W.H - H_W.T_Height),
            };
            this.AddChidren(verticalRefresh);
            #endregion
 
            verticalRefresh.BeginHeaderRefreshingAction += () =>
            {
                SeachMusic();
                verticalRefresh.EndHeaderRefreshing();
 
            };
            this.AddChidren(Loading);
 
            if (A31MusicModel.A31MusicModelList.Count == 0)
            {
                SeachMusic(true);
            }
            else
            {
                clearA31Threads();
                verticalRefresh.RemoveAll();
                for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++)
                {
                    var a31player = A31MusicModel.A31MusicModelList[i];
                    ///这个状态是之前保存的,加载完成后要标记为不在线,后面再读取正确的状态
                    if (!a31player.IsCanShow)
                    {
                        continue;
                    }
                    ///加载界面时默认不在线
                    ///这里标记是为了不读取不在线播放器状态
                    a31player.IsOnLine = false;
                    MusicListView(a31player);
                }
                ///读取正确的信息,包括IP和端口及名称
                SendMethod.Seach((obj) =>
                {
                    try
                    {
                        if (obj == null)
                        {
                            ///这里要读取主从关系
                           // readServerOrClientMode(false);
                            A31MusicModel.Save();
                            return;
                        }
                        var a31MusicModel = A31MusicModel.A31MusicModelList.Find((music) => music.UniqueDeviceName == obj.UniqueDeviceName);
                        if (a31MusicModel != null)
                        {
                            a31MusicModel.IPAddress = obj.IPAddress;
                            a31MusicModel.Port = obj.Port;
                            a31MusicModel.Name = obj.Name;
                            a31MusicModel.IsCanShow = true;
                            a31MusicModel.IsOnLine = true;
                        }
                    }
                    catch (Exception e) { System.Console.WriteLine(e.Message); }
                });
            }
        }
 
        void SeachMusic(bool Yes = false)
        {
            for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++)
            {
                var a31player = A31MusicModel.A31MusicModelList[i];
                a31player.IsCanShow = false;
                a31player.IsOnLine = false;
            }
            if (Yes)
            {
                //为了第一次进来log一下;
                Loading.Start();
            }
            SendMethod.Seach((obj) =>
            {
                if (obj == null)
                {
                    //readServerOrClientMode(true);
                    A31MusicModel.Save();
                    Application.RunOnMainThread(() =>
                    {
                        if (Yes)
                        {
                            //为了第一次进来log一下;
                            Loading.Hide();
                        }
                        verticalRefresh.RemoveAll();
                        for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++)
                        {
                            var a31player = A31MusicModel.A31MusicModelList[i];
                            //这个状态是之前保存的,加载完成后要标记为不在线,后面再读取正确的状态
                            if (!a31player.IsCanShow)
                            {
                                continue;
                            }
                            MusicListView(a31player);
                        }
                    });
                    return;
                }
                var a31MusicModel = A31MusicModel.A31MusicModelList.Find((music) => { return music.UniqueDeviceName == obj.UniqueDeviceName; });
                if (a31MusicModel == null)
                {
                    //不是我们支持的品牌不支持
                    if (obj.Name != null)
                    {
                        A31MusicModel.A31MusicModelList.Add(obj);
                    }
                }
                else
                {
                    a31MusicModel.IPAddress = obj.IPAddress;
                    a31MusicModel.Port = obj.Port;
                    a31MusicModel.Name = obj.Name;
                    a31MusicModel.IsCanShow = true;
                    a31MusicModel.IsOnLine = true;
                }
            });
 
        }
        /// <summary>
        /// 显示音乐列表的方法
        /// </summary>
        void MusicListView(A31MusicModel a31player)
        {
 
 
            MusicView musicView = new MusicView();
            musicView.View(verticalRefresh);
            musicView.singerBtn.Text = a31player.A31PlayStatus.Artist;
            musicView.songNameBtn.Text = a31player.A31PlayStatus.Title;
            musicView.musicNameBtn.Text = a31player.Name;
            musicView.regionBtn.TextID = StringId.region;
            if (a31player.A31PlayStatus.status == "play")
            {
                musicView.playBtn.IsSelected = true;
            }
            else
            {
                musicView.playBtn.IsSelected = false;
            }
            EventHandler<MouseEventArgs> clickPlay = (sender, e) =>
            {
                A31MusicModel.Current = a31player;//当前播放器
                var a31PlayMusicPage = new A31PlayMusicPage();
                MainPage.BasePageView.AddChidren(a31PlayMusicPage);
                a31PlayMusicPage.Show();
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            };
 
            musicView.singerBtn.MouseUpEventHandler += clickPlay;
            musicView.songNameBtn.MouseUpEventHandler += clickPlay;
            musicView.musicNameBtn.MouseUpEventHandler += clickPlay;
            musicView.regionBtn.MouseUpEventHandler += clickPlay;
 
            ///上一曲点击事件
            musicView.prevBtn.MouseDownEventHandler += (sender, e) =>
            {
                musicView.prevBtn.IsSelected = true;
                SendMethod.Previous(a31player);
            };
            musicView.prevBtn.MouseUpEventHandler += (sender, e) =>
            {
                musicView.prevBtn.IsSelected = false;
            };
            ///暂停/播放点击事件
            musicView.playBtn.MouseDownEventHandler += (sender, e) =>
            {
                if (musicView.playBtn.IsSelected)
                {
                    musicView.playBtn.IsSelected = false;
                    SendMethod.Pause(a31player);
                    a31player.A31PlayStatus.status = "pause";
                }
                else
                {
                    musicView.playBtn.IsSelected = true;
                    SendMethod.Play(a31player);
                    a31player.A31PlayStatus.status = "play";
                }
            };
            ///下一曲点击事件
            musicView.nextBtn.MouseDownEventHandler += (sender, e) =>
            {
                musicView.nextBtn.IsSelected = true;
                SendMethod.Next(a31player);
            };
            musicView.nextBtn.MouseUpEventHandler += (sender, e) =>
            {
                musicView.nextBtn.IsSelected = false;
            };
 
            System.Threading.Tasks.Task.Run(() =>
            {
                System.Threading.Thread.CurrentThread.Name = "A31";
                threadLists.Add(System.Threading.Thread.CurrentThread);
                while (true)
                {
                    System.Threading.Thread.Sleep(1000);
                    if (!a31player.IsOnLine)
                    {
                        continue;
                    }
                    SendMethod.ReadStatus(a31player);
                    Application.RunOnMainThread(() =>
                    {
                        musicView.singerBtn.Text = a31player.A31PlayStatus.Artist;
                        musicView.songNameBtn.Text = a31player.A31PlayStatus.Title;
                        musicView.musicNameBtn.Text = a31player.Name;
                        if (a31player.A31PlayStatus.status == "play")
                        {
                            musicView.playBtn.IsSelected = true;
                        }
                        else
                        {
                            musicView.playBtn.IsSelected = false;
                        }
                    });
                }
            });
 
        }
 
 
 
    }
 
}