using System;
using System.Collections.Generic;
using Shared;
using Shared.IO;
using System.Net;
using HDL_ON.Entity;
using HDL_ON.UI.UI2.FuntionControlView.Music;
namespace HDL_ON.UI.Music
{
public class MusicMain : FrameLayout
{
private static MusicMain s_Current = null;
public static MusicMain Current
{
get
{
if (s_Current == null)
{
s_Current = new MusicMain();
}
return s_Current;
}
}
///
/// MusicMain对象构造函数
///
public MusicMain()
{
Tag = "MusicMain";
}
///
/// 重写RemoveFromParent方法
///
public override void RemoveFromParent()
{
base.RemoveFromParent();
ClearA31Threads();
//进来没有音乐被收藏过,退出有音乐被收藏过
//A31MusicModel.ReadMusicStates();
}
///
/// 创建线程列表
///
static List threadLists = new List();
///
/// 移除线程
///
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)
{
MainPage.Log("clearA31Threads error : " + e.Message);
}
}
}
///
/// 显示加载界面
///
public static Loading loading = new Loading();
///
/// 定义全局对象
///
VerticalRefreshLayout verticalRefresh;
public void Show()
{
#region 界面布局
this.BackgroundColor = MusicColor.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),
Name = "verticalRefresh",
};
this.AddChidren(verticalRefresh);
#endregion
verticalRefresh.BeginHeaderRefreshingAction += () =>
{
////发送读取音乐播放器状态线程
SeachMusic();
verticalRefresh.EndHeaderRefreshing();
};
this.AddChidren(loading);
SeachMusic();
}
///
/// 刷新播放器列表
///
void SeachMusic()
{
Application.RunOnMainThread(() =>
{
verticalRefresh.RemoveAll();
A31MusicModel.A31MusicModelList.Clear();
var musicDeviceList= FunctionList.List.GetMusicList();
for (int i = 0; i < musicDeviceList.Count; i++)
{
var function = musicDeviceList[i];
var music = A31MusicModel.A31MusicModelList.Find((obj) =>
(obj.functionMusic.deviceId == function.deviceId && function.spk == SPK.MusicStandard) || (
obj.functionMusic.deviceId == function.deviceId && function.spk == SPK.AvMusic)
);
if (music == null)
{
A31MusicModel.A31MusicModelList.Add(new A31MusicModel { functionMusic = function });
}
else
{
music.functionMusic = function;
}
}
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++)
{
var a31player = A31MusicModel.A31MusicModelList[i];
//if (a31player.functionMusic.online == false)
//{
// //不在线不显示
// continue;
//}
Application.RunOnMainThread(() =>
{
MusicListView(a31player);
});
}
///进来读一次音乐播放器状态
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++)
{
var a31player = A31MusicModel.A31MusicModelList[i];
if (a31player.functionMusic.online == false)
{
//不在线不读取
continue;
}
//发送读取音乐播放器状态线程
System.Threading.Tasks.Task.Run(() =>
{
try
{
SendMethod.Current.RefreshDeviceStatus(new List { a31player.functionMusic.deviceId });
System.Threading.Thread.Sleep(500);
}
catch { }
});
}
});
}
///
/// 显示音乐列表的方法
///
void MusicListView(A31MusicModel player)
{
///
/// 为了音乐刷新状态定义全局对象
///
MusicView musicView = new MusicView();
musicView.ViewAddChidren(verticalRefresh);
musicView.muiscFl.Tag = player.functionMusic;//多个音乐播放器更新状态要用到
musicView.singerBtn.Text = player.functionMusic.GetAttrState(KeyProperty.song_name);
musicView.songNameBtn.Text = player.functionMusic.GetAttrState(KeyProperty.song_name);
musicView.musicNameBtn.Text = player.functionMusic.name;
musicView.collectIconBtn.Visible = false;//先暂时隐藏收藏功能
///收藏事件
musicView.collectIconBtn.MouseUpEventHandler += (sender, e) =>
{
musicView.collectIconBtn.IsSelected = !musicView.collectIconBtn.IsSelected;
if (musicView.collectIconBtn.IsSelected)
{
player.functionMusic.collect = true;
}
else
{
player.functionMusic.collect = false;
}
};
///进入音乐主页事件
EventHandler clickPlayView = (sender, e) =>
{
A31MusicModel.Current = player;
var a31PlayMusicPage = new A31PlayMusicPage();
MainPage.BasePageView.AddChidren(a31PlayMusicPage);
a31PlayMusicPage.Show();
MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
};
musicView.musicIfonFl.MouseUpEventHandler += clickPlayView;
musicView.clickBtn.MouseUpEventHandler += clickPlayView;//
musicView.singerBtn.MouseUpEventHandler += clickPlayView;
musicView.songNameBtn.MouseUpEventHandler += clickPlayView;//
musicView.musicNameBtn.MouseUpEventHandler += clickPlayView;
musicView.regionBtn.MouseUpEventHandler += clickPlayView;//
///上一曲点击事件
musicView.prevBtn.MouseDownEventHandler += (sender, e) =>
{
musicView.prevBtn.IsSelected = true;
player.functionMusic.SetAttrState(KeyProperty.song_step, ValueProperty.up);
Dictionary dic = new Dictionary();
dic.Add(KeyProperty.song_step, ValueProperty.up);
SendMethod.Current.SendControlCommand(player.functionMusic, dic);
};
musicView.prevBtn.MouseUpEventHandler += (sender, e) =>
{
musicView.prevBtn.IsSelected = false;
};
///暂停/播放点击事件
musicView.playBtn.MouseDownEventHandler += (sender, e) =>
{
string status = ValueProperty.off;
if (musicView.playBtn.IsSelected)
{
musicView.playBtn.IsSelected = false;
status = ValueProperty.off;
}
else
{
musicView.playBtn.IsSelected = true;
status = ValueProperty.on;
}
player.functionMusic.SetAttrState(KeyProperty.on_off, status);
Dictionary dic = new Dictionary();
dic.Add(KeyProperty.on_off, status);
SendMethod.Current.SendControlCommand(player.functionMusic, dic);
};
///下一曲点击事件
musicView.nextBtn.MouseDownEventHandler += (sender, e) =>
{
musicView.nextBtn.IsSelected = true;
player.functionMusic.SetAttrState(KeyProperty.song_step, ValueProperty.down);
Dictionary dic = new Dictionary();
dic.Add(KeyProperty.song_step, ValueProperty.down);
SendMethod.Current.SendControlCommand(player.functionMusic, dic);
};
musicView.nextBtn.MouseUpEventHandler += (sender, e) =>
{
musicView.nextBtn.IsSelected = false;
};
//更新状态线程
var musicThread = new System.Threading.Thread(() =>
{
while (true)
{
if (!player.functionMusic.online)
{
///不在线不读状态
continue;
}
//SendMethod.ReadStatus(player);
SendMethod.Current.GetDeviceStatus(ref player, new List { player.functionMusic.deviceId }, player.functionMusic.sid);
System.Threading.Thread.Sleep(1000);
Application.RunOnMainThread(() =>
{
musicView.singerBtn.Text = player.functionMusic.GetAttrState(KeyProperty.song_name);
musicView.songNameBtn.Text = player.functionMusic.GetAttrState(KeyProperty.song_name);
musicView.musicNameBtn.Text = player.functionMusic.name;
if (player.functionMusic.GetAttrState(KeyProperty.on_off) == ValueProperty.on)
{
musicView.playBtn.IsSelected = true;
}
else
{
musicView.playBtn.IsSelected = false;
}
musicView.regionBtn.Text = player.functionMusic.GetRoomListName();
if (player.functionMusic.collect)
{
musicView.collectIconBtn.IsSelected = true;
}
else
{
musicView.collectIconBtn.IsSelected = false;
}
});
}
})
{ IsBackground = true, Name = "A31" };
musicThread.Start();
threadLists.Add(musicThread);
}
///
///指定刷新界面
///
/// 判断字符
public void RefreshView(AlinkStatusData alinkStatusData)
{
if (alinkStatusData == null || alinkStatusData.status.Count == 0)
{
return;
}
Application.RunOnMainThread(() =>
{
try
{
for (int a =0; a < MainPage.BasePageView.ChildrenCount; a++)
{
var view = MainPage.BasePageView.GetChildren(a);
if (view.GetType() == typeof(MusicMain))
{
var musicMain = view as MusicMain;
if (musicMain != null)
{
for (int b = 0; b < musicMain.ChildrenCount; b++)
{
var view1 = musicMain.GetChildren(b);
if (view1.GetType() == typeof(VerticalRefreshLayout))
{
var vv = view1 as VerticalRefreshLayout;
if (vv != null && vv.Name == "verticalRefresh")
{
for (int c = 0; c < vv.ChildrenCount; c++)
{
var viewfl = vv.GetChildren(c);
if (viewfl.GetType() == typeof(FrameLayout))
{
var fl = viewfl as FrameLayout;
if ((fl.Tag as Function).sid!= alinkStatusData.sid) {
//不是当前音乐不会更新状态
continue;
}
if (fl != null && fl.Name == "parentfl")
{
for (int i = 0; i < fl.ChildrenCount; i++)
{
var viewfl1 = fl.GetChildren(i);
if (viewfl1.GetType() == typeof(FrameLayout))
{
var fl1 = viewfl1 as FrameLayout;
if (fl1 != null && fl1.Name == "musicparentfl")
{
for (int j = 0; j < fl1.ChildrenCount; j++)
{
if (fl1.GetChildren(j).GetType() == typeof(Button))
{
var btn = fl1.GetChildren(j) as Button;
if (btn == null ||btn.Name==null)
{
continue;
}
switch (btn.Name)
{
case "song":
{
var s = alinkStatusData.status.Find((o) => o.key == KeyProperty.song_name);
if (s != null)
{
btn.Text = s.value;
}
}
break;
case "playstatus":
{
var s = alinkStatusData.status.Find((o) => o.key == KeyProperty.on_off);
if (s != null)
{
if (s.value == ValueProperty.on)
{
btn.IsSelected = true;
}
else
{
btn.IsSelected = false;
}
}
}
break;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
catch { }
});
}
}
}