using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using Shared.SimpleControl.Phone;
using Shared.SimpleControl.R;
using SmartHome.UI.SimpleControl.Phone.Music;
using System.Xml;
using System.Security;
using com.hdl.on;
using SmartHome;
namespace Shared.SimpleControl.Phone.Music
{
class MyMusic : FrameLayout
{
public override void RemoveFromParent ()
{
UserMiddle.DevicePageView.ScrollEnabled = true;
base.RemoveFromParent ();
clearA31Threads ();
clearOldThreads ();
}
///
/// 加载所有音乐的滑动视图
///
MusicVerticalScrolViewLayout verticalScrolViewLayout;
string openWeb (string url)
{
try {
var webClient = new Shared.Net.MyWebClient (2000);
return webClient.DownloadString (url);
} catch (Exception e) {
System.Console.WriteLine (e.Message);
return null;
}
}
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) {
System.Console.WriteLine (e.Message);
}
}
}
static void clearOldThreads ()
{
var threads = threadLists.FindAll ((obj) => { return obj.Name == "Old"; });
foreach (var thread in threads) {
try {
threadLists.Remove (thread);
thread.Abort ();
} catch (Exception e) {
System.Console.WriteLine (e.Message);
}
}
}
public void Show (bool reload)
{
UserMiddle.DevicePageView.ScrollEnabled = false;
BackgroundColor = 0xff2F2F2F;
var tTopFrameLayout = new FrameLayout {
Height = Application.GetRealHeight (36),
BackgroundColor = SkinStyle.Current.MusicTopFrameLayout,
};
AddChidren (tTopFrameLayout);
var topFrameLayout = new FrameLayout {
Height = Application.GetRealHeight (100),
Y = tTopFrameLayout.Bottom,
BackgroundColor = SkinStyle.Current.MusicTopFrameLayout,
};
AddChidren (topFrameLayout);
#region 总音量控制层
/*
var bottomFrameLayout = new FrameLayout {
Height = Application.GetRealHeight (134),
Y = topFrameLayout.Bottom,
BackgroundColor = 0xFF181818,
};
//AddChidren (bottomFrameLayout);
var voiceVol = new Button {
Width = Application.GetRealWidth (107),
Height = Application.GetRealHeight (127),
X = Application.GetRealWidth (-3),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "MusicIcon/PlayVoice.png",
};
bottomFrameLayout.AddChidren (voiceVol);
var frameLayoutVol = new FrameLayout {
Width = Application.GetRealWidth (450),
Height = Application.GetRealHeight (50),
X = Application.GetRealWidth (90),
Gravity = Gravity.CenterVertical,
};
bottomFrameLayout.AddChidren (frameLayoutVol);
allVol = new HorizontalSeekBar {
Width = Application.GetRealWidth (450 - 45),
Height = Application.GetRealHeight (50),
Radius = (uint)Application.GetRealHeight (25),
X = Application.GetRealWidth (13),
Max = 100,
Progress = 50,
SleepTime = 1000,
ProgressColor = 0xffFE5E00,
};
frameLayoutVol.AddChidren (allVol);
allVol.ProgressChanged += allVol_ProgressChanged;
muteVol = new Button {
Width = Application.GetRealWidth (107),
Height = Application.GetRealWidth (127),
X = Application.GetRealWidth (515),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "MusicIcon/PlayMute.png",
SelectedImagePath = "MusicIcon/PlayMuteSelected.png",
};
bottomFrameLayout.AddChidren (muteVol);
muteVol.MouseDownEventHandler += MuteVol_MouseDownEventHandler;
var line = new Button {
Height = Application.GetRealHeight (4),
Y = topFrameLayout.Bottom,
UnSelectedImagePath = "MusicIcon/MusicThread.png",
};
//AddChidren (line);
*/
#endregion
FrameLayout frayout = new FrameLayout {
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight (1000),
BackgroundImagePath = "MusicIcon/MusicBackgroun.png",
Y = topFrameLayout.Bottom,
};
AddChidren (frayout);
verticalScrolViewLayout = new MusicVerticalScrolViewLayout ();
verticalScrolViewLayout.ReplaceChanged += (arg1, arg2) => {
var d = true;
if (arg1.Tag.GetType () == typeof (A31MusicModel) && arg2.Tag.GetType () == typeof (A31MusicModel)) {
MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.disposition) + "...");
System.Threading.Tasks.Task.Run (() => {
try {
var serverMusic = arg1.Tag as A31MusicModel;
var clientMusic = arg2.Tag as A31MusicModel;
if (clientMusic.ServerClientType == 1) {
d = false;
return;
}
if (serverMusic.ServerClientType == -1 && clientMusic.ServerClientType == -1) {
if (serverMusic.ServerIP == clientMusic.ServerIP) {
d = false;
return;
}
}
if (serverMusic.ServerClientType == 1 && clientMusic.ServerClientType == -1) {
if (serverMusic.IPAddress == clientMusic.ServerIP) {
d = false;
return;
}
}
clearA31Threads ();
var IP = "";
if (serverMusic.ServerClientType == -1) {
IP = serverMusic.ServerIP;
} else {
IP = serverMusic.IPAddress;
}
var statusEx = openWeb ("http://" + IP + "/httpapi.asp?command=getStatusEx");
if (statusEx == null) {
statusEx = openWeb ("http://" + IP + "/httpapi.asp?command=getStatusEx");
}
if (statusEx == null) {
return;
}
var serverIfon = Newtonsoft.Json.JsonConvert.DeserializeObject (statusEx);
if (serverIfon == null) {
return;
}
string ssid = "";
foreach (var b in serverIfon.ssid) {
ssid += System.Convert.ToString (b, 16).ToUpper ().Length < 2 ? "0" + System.Convert.ToString (b, 16).ToUpper () : System.Convert.ToString (b, 16).ToUpper ();
}
System.Threading.Thread.Sleep (1000);
if (null == openWeb ("http://" + clientMusic.IPAddress + "/httpapi.asp?command=ConnectMasterAp:ssid=" + ssid + ":ch=" + serverIfon.WifiChannel + ":auth=OPEN:encry=NONE:pwd=:chext=0:JoinGroupMaster:eth" + serverIfon.eth2 + ":wifi" + serverIfon.ra0 + ":uuid" + serverIfon.uuid)) {
if (null == openWeb ("http://" + clientMusic.IPAddress + "/httpapi.asp?command=ConnectMasterAp:ssid=" + ssid + ":ch=" + serverIfon.WifiChannel + ":auth=OPEN:encry=NONE:pwd=:chext=0:JoinGroupMaster:eth" + serverIfon.eth2 + ":wifi" + serverIfon.ra0 + ":uuid" + serverIfon.uuid)) {
return;
}
}
System.DateTime dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalSeconds < 10) {
System.Threading.Thread.Sleep (1000);
try {
var result = openWeb ("http://" + serverMusic.IPAddress + "/httpapi.asp?command=multiroom:getSlaveList");
if (result != null) {
var tmepSlaves = Newtonsoft.Json.JsonConvert.DeserializeObject (result);
if (tmepSlaves != null && tmepSlaves.slave_list != null) {
if (null != tmepSlaves.slave_list.Find ((obj) => obj.uuid.Replace ("uuid:", "") == clientMusic.UniqueDeviceName)) {
//已经添加成功
break;
}
}
}
} catch (Exception e) { System.Console.WriteLine (e.Message); }
}
} catch (Exception e) { System.Console.WriteLine (e.Message); } finally {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
if (d) {
//this.RemoveFromParent ();
//MyMusic mymusic = new MyMusic ();
//MainPage.MainFrameLayout.AddChidren (mymusic);
//mymusic.Show (true);
UserMiddle.DevicePageView.PageIndex = 0;
var myMusic = new MyMusic ();
UserMiddle.DevicePageView.AddChidren (myMusic);
myMusic.Show (true);
UserMiddle.DevicePageView.PageIndex = 1;
}
});
}
});
}
};
verticalScrolViewLayout.LongPressAction += (arg) => {
if (arg.Tag.GetType () == typeof (A31MusicModel)) {
var clientMusic = arg.Tag as A31MusicModel;
if (clientMusic != null && clientMusic.ServerClientType == -1) {
MainPage.Loading.Start ();
System.Threading.Tasks.Task.Run (() => {
openWeb ("http://" + clientMusic.ServerIP + "/httpapi.asp?command=multiroom:SlaveKickout:" + clientMusic.IPAddress);
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
//this.RemoveFromParent ();
//MyMusic music = new MyMusic ();
//MainPage.MainFrameLayout.AddChidren (music);
//music.Show (true);
UserMiddle.DevicePageView.PageIndex = 0;
var myMusic = new MyMusic ();
UserMiddle.DevicePageView.AddChidren (myMusic);
myMusic.Show (true);
UserMiddle.DevicePageView.PageIndex = 1;
});
});
}
}
};
frayout.AddChidren (verticalScrolViewLayout);
var back = new Button {
Width = Application.GetMinRealAverage (72),
Height = Application.GetMinRealAverage (89),
X = Application.GetRealWidth (-1),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "MusicIcon/MusicBack.png",
};
topFrameLayout.AddChidren (back);
back.MouseDownEventHandler += back_MouseDownEventHandler;
var wifi = new Button {
Width = Application.GetMinRealAverage (50),
Height = Application.GetMinRealAverage (50),
UnSelectedImagePath = "MusicIcon/wifi.png",
//UnSelectedImagePath = "MusicIcon/set.png",
X = Application.GetRealWidth (450),
Gravity = Gravity.CenterVertical,
};
topFrameLayout.AddChidren (wifi);
wifi.MouseUpEventHandler += (sender, e) => {
var wifiSet = new WiFiSet ();
MainPage.MainFrameLayout.AddChidren (wifiSet);
wifiSet.Show ();
MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load));
System.Threading.Tasks.Task.Run (() => {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
wifiSet.Init ();
});
});
};
var device = new Button {
Width = Application.GetRealWidth (200),
Height = Application.GetRealHeight (60),
TextID = MyInternationalizationString.MusicDevicelist,
Gravity = Gravity.Center,
TextColor = SkinStyle.Current.MusicTextColor,
};
topFrameLayout.AddChidren (device);
var a31search = new Button {
Width = Application.GetMinRealAverage (60),
Height = Application.GetMinRealAverage (80),
X = Application.GetRealWidth (545),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "MusicIcon/seekdevice.png",
};
topFrameLayout.AddChidren (a31search);
a31search.MouseUpEventHandler += (sender, e) => {
//RemoveFromParent ();
//MyMusic mymusic = new MyMusic ();
//MainPage.MainFrameLayout.AddChidren (mymusic);
//mymusic.Show (true);
UserMiddle.DevicePageView.PageIndex = 0;
var myMusic = new MyMusic ();
UserMiddle.DevicePageView.AddChidren (myMusic);
myMusic.Show (true);
UserMiddle.DevicePageView.PageIndex = 1;
};
initOldMusic (verticalScrolViewLayout);
if (reload || A31MusicModel.A31MusicModelList.Count == 0) {
refreshDevice (verticalScrolViewLayout);
} else {
clearA31Threads ();
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++) {
var a31player = A31MusicModel.A31MusicModelList [i];
///这个状态是之前保存的,加载完成后要标记为不在线,后面再读取正确的状态
if (!a31player.IsCanShow) {
continue;
}
if (a31player.ServerClientType == 1) {
if (a31player.Slave != null && "0" != a31player.Slave.slaves) {
for (int j = 0; j < a31player.Slave.slave_list.Count; j++) {
///最后一个从播放器
if (a31player.Slave.slave_list.Count - 1 == j) {
var tempA31Player = A31MusicModel.A31MusicModelList.Find ((obj) => a31player.Slave.slave_list [j].uuid.Replace ("uuid:", "") == obj.UniqueDeviceName);
if (tempA31Player != null) {
///标记从播放器最后一个
if (!tempA31Player.IsEnd) {
tempA31Player.IsEnd = true;
}
}
}
}
}
}
///加载界面时默认不在线
a31player.IsOnLine = false;
addViews (a31player);
}
///读取正确的信息,包括IP和端口及名称
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 initOldMusic (MusicVerticalScrolViewLayout verticalScrolViewLayout)
{
foreach (Room room in Room.Lists) {
var musiceList = room.DeviceList.FindAll (tempDevice => { return tempDevice.Type == DeviceType.MusicModel; });
foreach (var tempDevice in musiceList) {
var MusicModelBackground = new FrameLayout {
Width = Application.GetRealWidth (640 - 24),
Height = Application.GetRealHeight (190 + 20),
X = Application.GetRealWidth (12),
Y = verticalScrolViewLayout.ChildrenCount * Application.GetRealHeight (190 + 20),
Tag = tempDevice,
};
verticalScrolViewLayout.AddChidren (MusicModelBackground);
var frameLayout = new FrameLayout {
// Y = Application.GetRealHeight (20) + verticalScrolViewLayout.ChildrenCount * Application.GetRealHeight (190 + 20),
Y = Application.GetRealHeight (20),
X = Application.GetRealWidth (12),
Width = Application.GetRealWidth (640 - 24),
Height = Application.GetRealHeight (190),
BackgroundColor = SkinStyle.Current.MusicPalyerView,//0xff181818,
//Radius = (uint)Application.GetRealHeight (10),
Tag = tempDevice,
};
MusicModelBackground.AddChidren (frameLayout);
var titelFrameLayout = new FrameLayout () {
Height = Application.GetRealHeight (80),
Y = Application.GetRealHeight (3),
};
frameLayout.AddChidren (titelFrameLayout);
var Living = new Button {
Width = Application.GetRealWidth (300),
Height = Application.GetRealHeight (80),
Text = tempDevice.Name,
TextAlignment = TextAlignment.CenterLeft,
X = Application.GetRealWidth (29),
Gravity = Gravity.CenterVertical,
TextColor = SkinStyle.Current.MusicTextColor,
};
titelFrameLayout.AddChidren (Living);
var btnPlayMusic = new Button {
Width = Application.GetRealWidth (80),
Height = Application.GetRealHeight (80),
X = Application.GetRealWidth (515),//510
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "MusicIcon/MusicPlay.png",
SelectedImagePath = "MusicIcon/MusicPlaySelected.png",
Tag = tempDevice,
};
titelFrameLayout.AddChidren (btnPlayMusic);
btnPlayMusic.MouseUpEventHandler += (sender, e) => {
Button button = (Button)sender;
MusicModel musicModel = (MusicModel)button.Tag;
if (button.IsSelected) {
button.IsSelected = false;
if (musicModel.MusicType == 2) {
Control.ControlBytesSend (Command.ControlMusicModel, musicModel.SubnetID, musicModel.DeviceID, MusicModel.MusiceBytes ("*Z1OFF"));
} else {
Control.ControlBytesSend (Command.ControlMusicModel, musicModel.SubnetID, musicModel.DeviceID, MusicModel.MusiceBytes ("*S" + musicModel.SourceID + "PLAYSTOP"));
}
} else {
button.IsSelected = true;
if (musicModel.MusicType == 2) {
Control.ControlBytesSend (Command.ControlMusicModel, musicModel.SubnetID, musicModel.DeviceID, MusicModel.MusiceBytes ("*Z1ON"));
} else {
Control.ControlBytesSend (Command.ControlMusicModel, musicModel.SubnetID, musicModel.DeviceID, MusicModel.MusiceBytes ("*S" + musicModel.SourceID + "PLAYSTOP"));
}
}
};
var image = new Button {
Width = Application.GetRealWidth (80),
Height = Application.GetRealHeight (80),
X = Application.GetRealWidth (30),
UnSelectedImagePath = "MusicIcon/musicplay1.png",
Y = Application.GetRealHeight (220 - 130),
Tag = tempDevice,
};
frameLayout.AddChidren (image);
var currentMusic = new Button {
Width = Application.GetRealWidth (300),
Height = Application.GetRealHeight (50),
X = Application.GetRealWidth (135),
Y = Application.GetRealHeight (210 - 130),
Text = "UnKown",
Tag = tempDevice,
TextSize = 12,
TextAlignment = TextAlignment.CenterLeft,
TextColor = SkinStyle.Current.MusicTextColor,
};
frameLayout.AddChidren (currentMusic);
EventHandler CurrentPlayPage = (sender, e) => {
Room.CurrentRoom = room;
//Button button = (Button)sender;
Button button = image;
if (((MusicModel)button.Tag).SourceID == "5") {
RadioPage radioPage = new RadioPage ();
MainPage.MainFrameLayout.AddChidren (radioPage);
radioPage.Show ((MusicModel)button.Tag);
} else {
PlayPage playMusic = new PlayPage ();
MainPage.MainFrameLayout.AddChidren (playMusic);
playMusic.Show ((MusicModel)button.Tag, (button.Tag as MusicModel).SDCardSongList);
}
};
image.MouseUpEventHandler += CurrentPlayPage;
currentMusic.MouseUpEventHandler += CurrentPlayPage;
Living.MouseUpEventHandler += CurrentPlayPage;
var voice = new Button {
Width = Application.GetRealWidth (33),
Height = Application.GetRealHeight (31),
X = Application.GetRealWidth (145),
Y = Application.GetRealHeight (275 - 130),
UnSelectedImagePath = "MusicIcon/MusicVoice.png",
};
frameLayout.AddChidren (voice);
var horizontalSeekBarvol = new HorizontalSeekBar {
Width = Application.GetRealWidth (380),
Height = Application.GetRealHeight (50),
Radius = (uint)Application.GetRealHeight (25),
Y = Application.GetRealHeight (275 - 140),
X = Application.GetRealWidth (200),
Max = tempDevice.GetType () == typeof (MusicModel) ? 79 : 100,
Tag = tempDevice,
ProgressColor = 0xffFE5E00,
ThumbRadius = 9,
ThumbColor = SkinStyle.Current.HorizontalSeekBarThumbColor,
};
frameLayout.AddChidren (horizontalSeekBarvol);
horizontalSeekBarvol.ProgressChanged += (sender, e) => {
var seekBar = (HorizontalSeekBar)sender;
MusicModel musicModel = (MusicModel)(sender as HorizontalSeekBar).Tag;
Control.ControlBytesSend (Command.ControlMusicModel, musicModel.SubnetID, musicModel.DeviceID,
MusicModel.MusiceBytes ("*Z1VOL" + (79 - seekBar.Progress)));
};
if (tempDevice.Type == DeviceType.MusicModel) {
var music = tempDevice as MusicModel;
System.Threading.Tasks.Task.Run (() => {
System.Threading.Thread.CurrentThread.Name = "Old";
threadLists.Add (System.Threading.Thread.CurrentThread);
var dateTime = DateTime.Now.AddSeconds (-5);
while (true) {
if (5 <= (DateTime.Now - dateTime).TotalSeconds) {
dateTime = DateTime.Now;
Control.ControlBytesSend (Command.ControlMusicModel, music.SubnetID, music.DeviceID, MusicModel.MusiceBytes ("*Z1STATUS?"), SendCount.Zero);
}
Application.RunOnMainThread (() => {
try {
if (music.PlayStatus == MusicModel.Status.Play) {
btnPlayMusic.IsSelected = true;
} else {
btnPlayMusic.IsSelected = false;
}
//当前的音乐名称
currentMusic.Text = music.curPlayMusicName.Replace (".mp3", "") == string.Empty ? "UnKown" : music.curPlayMusicName.Replace (".mp3", "");
horizontalSeekBarvol.Progress = 79 - int.Parse (music.CurVol);
} catch (Exception e) {
var d = e.Message;
}
});
//等待1秒
System.Threading.Thread.Sleep (500);
}
});
}
}
}
}
///搜索A31音乐播放器
void seach (Action action, int time = 5 * 1000, string uid = "AllUniqueDeviceName")
{
System.Threading.Tasks.Task.Run (() => {
System.Net.Sockets.UdpClient udpClient = null;
int localPort = 65535;
for (; 1024 < localPort; localPort--) {
try {
udpClient = new System.Net.Sockets.UdpClient (localPort);
break;
} catch (Exception e) { System.Console.WriteLine (e.Message); }
}
System.Threading.Tasks.Task.Run (() => {
var tempDateTime = DateTime.Now;
while (udpClient != null) {
try {
if (time < (DateTime.Now - tempDateTime).TotalMilliseconds) {
var tempBytes = System.Text.Encoding.UTF8.GetBytes ("完成");
udpClient.Send (tempBytes, tempBytes.Length, new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("127.0.0.1"), localPort));
} else {
var stringBuilder = new StringBuilder ();
stringBuilder.AppendLine ("M-SEARCH * HTTP/1.1");
stringBuilder.AppendLine ("St: ssdp:wiimudevice");
stringBuilder.AppendLine ("Mx: 3");
stringBuilder.AppendLine ("Host: 239.255.255.250:1900");
stringBuilder.AppendLine ("Man: \"ssdp:discover\"");
stringBuilder.AppendLine ();
var tempBytes = System.Text.Encoding.ASCII.GetBytes (stringBuilder.ToString ());
//请求获取A31服务器信息
udpClient.Send (tempBytes, tempBytes.Length, new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("239.255.255.250"), 1900));
udpClient.Send (tempBytes, tempBytes.Length, new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("239.255.255.250"), 1900));
//如果1000毫秒没有数据回复,就关闭当前Socket,不再等待接收数据
}
System.Threading.Thread.Sleep (500);
} catch (Exception e) { System.Console.WriteLine (e.Message); }
}
});
while (true) {
try {
//接收回来的数据
var remoteIpEndPoint = new System.Net.IPEndPoint (0, 0);
//开始在这里等待接收数据,
var receviceBytes = udpClient.Receive (ref remoteIpEndPoint);
if (receviceBytes == null) {
break;
}
if ("完成" == System.Text.Encoding.UTF8.GetString (receviceBytes)) {
if (action != null) {
//表示完成了
action (null);
}
try {
udpClient.Close ();
udpClient = null;
} catch (Exception e) { System.Console.WriteLine (e.Message); }
break;
}
var sr = new System.IO.StreamReader (new System.IO.MemoryStream (receviceBytes, 0, receviceBytes.Length));
string tempLine = null;
string ipAddress = null;
int port = 0;
string uniqueDeviceName = null;
//一行一行数据判断,找出需要的信息
while ((tempLine = sr.ReadLine ()) != null) {
//找出Ip地址相关的信息
//System.Console.WriteLine (tempLine);
if (tempLine.StartsWith ("LOCATION: http://")) {
tempLine = tempLine.Replace ("LOCATION: http://", "").Split ('/') [0];
string [] ipAndPort = tempLine.Split (':');
ipAddress = ipAndPort [0];
port = int.Parse (ipAndPort [1]);
} else if (tempLine.StartsWith ("USN: uuid:")) {
uniqueDeviceName = tempLine.Replace ("USN: uuid:", "").Split (':') [0];
}
}
//关闭流
sr.Close ();
if (action != null) {
if ("AllUniqueDeviceName" == uid) {
action (new A31MusicModel { IPAddress = ipAddress, Port = port, Name = getDeviceName (ipAddress, port), UniqueDeviceName = uniqueDeviceName });
} else if (uid == uniqueDeviceName) {
if (action != null) {
//表示完成了
action (null);
}
try {
udpClient.Close ();
udpClient = null;
} catch (Exception e) { System.Console.WriteLine (e.Message); }
break;
}
}
} catch (Exception e) { System.Console.WriteLine (e.Message); }
}
});
}
///加载界面
void addViews (A31MusicModel a31player)
{
var BackgroundColorfra = new FrameLayout {
Width = Application.GetRealWidth (640 - 24),
Height = Application.GetRealHeight (190 + 20),
X = Application.GetRealWidth (12),
Y = verticalScrolViewLayout.ChildrenCount * Application.GetRealHeight (190 + 20),
Tag = a31player,
};
verticalScrolViewLayout.AddChidren (BackgroundColorfra);
var a31frameLayout = new FrameLayout {
//Y = Application.GetRealHeight (20) + verticalScrolViewLayout.ChildrenCount * Application.GetRealHeight (190 + 20),
Y = Application.GetRealHeight (20),
X = Application.GetRealWidth (12),
Width = Application.GetRealWidth (640 - 24),
Height = Application.GetRealHeight (190),
//BackgroundColor = 0xff181818,
BackgroundColor = SkinStyle.Current.MusicPalyerView,
//Radius = (uint)Application.GetRealHeight (10),
Tag = a31player,
};
BackgroundColorfra.AddChidren (a31frameLayout);
var a31titelFrameLayout = new FrameLayout {
Height = Application.GetRealHeight (80),
Y = Application.GetRealHeight (3),
};
a31frameLayout.AddChidren (a31titelFrameLayout);
var a31palyername = new Button {
Width = Application.GetRealWidth (350),
Height = Application.GetRealHeight (80),
Text = a31player.Name,
TextAlignment = TextAlignment.CenterLeft,
X = Application.GetRealWidth (29),
Gravity = Gravity.CenterVertical,
TextColor = SkinStyle.Current.MusicTextColor,
};
a31titelFrameLayout.AddChidren (a31palyername);
var colorBtn = new Button {
Height = LayoutParams.MatchParent,
Width = Application.GetRealWidth (5),
BackgroundColor = 0xffFE5E00,
//Radius = (uint)Application.GetRealHeight (10),
Visible = false
};
a31frameLayout.AddChidren (colorBtn);
var serverOrClientBtn = new Button {
Width = Application.GetRealWidth (80),
Height = Application.GetRealHeight (80),
X = Application.GetRealWidth (410),
Gravity = Gravity.CenterVertical,
Visible = false
};
serverOrClientBtn.MouseUpEventHandler += (sender, e) => {
// clearA31Threads ();
///解散所有
/*
if (a31player.ServerClientType == 1) {
var alert = new Alert (Language.StringByID (MyInternationalizationString.prompt), Language.StringByID (MyInternationalizationString.DissolvedGroup),
Language.StringByID (MyInternationalizationString.cancel), Language.StringByID (MyInternationalizationString.confirm));
alert.ResultEventHandler += (sender1, e1) => {
if (e1) {
MainPage.Loading.Start ();
System.Threading.Tasks.Task.Run (() => {
openWeb ("http://" + a31player.IPAddress + "/httpapi.asp?command=multiroom:Ungroup");
seach ((obj) => {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
this.RemoveFromParent ();
MyMusic music = new MyMusic ();
MainPage.MainFrameLayout.AddChidren (music);
music.Show (true);
});
}, 5000, a31player.UniqueDeviceName);
});
} else { }
};
alert.Show ();
} else if (a31player.ServerClientType == -1) {
var alert = new Alert (Language.StringByID (MyInternationalizationString.prompt), Language.StringByID (MyInternationalizationString.Dissolve),
Language.StringByID (MyInternationalizationString.cancel), Language.StringByID (MyInternationalizationString.confirm));
alert.ResultEventHandler += (sender1, e1) => {
if (e1) {
MainPage.Loading.Start ();
System.Threading.Tasks.Task.Run (() => {
openWeb ("http://" + a31player.ServerIP + "/httpapi.asp?command=multiroom:SlaveKickout:" + a31player.IPAddress);
seach ((obj) => {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
this.RemoveFromParent ();
MyMusic music = new MyMusic ();
MainPage.MainFrameLayout.AddChidren (music);
music.Show (true);
});
}, 5000, a31player.UniqueDeviceName);
});
} else { }
};
alert.Show ();
}
*/
};
var a31btnPlayMusic = new Button {
Width = Application.GetMinRealAverage (80),
Height = Application.GetMinRealAverage (80),
X = Application.GetRealWidth (518),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "MusicIcon/MusicPlay.png",
SelectedImagePath = "MusicIcon/MusicPlaySelected.png",
};
a31titelFrameLayout.AddChidren (a31btnPlayMusic);
a31btnPlayMusic.MouseUpEventHandler += (sender1, e1) => {
if (a31btnPlayMusic.IsSelected) {
a31btnPlayMusic.IsSelected = false;
//sendCommand (a31player, "pause");
a31player.Pause ();
a31player.A31PlayStatus.status = "pause";
} else {
a31btnPlayMusic.IsSelected = true;
//sendCommand (a31player, "resume");
a31player.Play ();
a31player.A31PlayStatus.status = "play";
}
};
var a31image = new ImageView {
Width = Application.GetRealWidth (80),
Height = Application.GetRealHeight (80),
X = Application.GetRealWidth (30),
Y = Application.GetRealHeight (220 - 130),
Radius = (uint)Application.GetRealHeight (8),
ImagePath = "MusicIcon/musicplay1.png",
};
a31frameLayout.AddChidren (a31image);
a31image.MouseLongEventHandler += (sender, e) => {
var alert = new Alert (Language.StringByID (MyInternationalizationString.prompt), Language.StringByID (MyInternationalizationString.Deletemusicplayer),
Language.StringByID (MyInternationalizationString.cancel), Language.StringByID (MyInternationalizationString.confirm));
alert.ResultEventHandler += (sender1, e1) => {
if (e1) {
A31MusicModel.A31MusicModelList.Remove (a31player);
a31frameLayout.RemoveFromParent ();
A31MusicModel.Save ();
//RemoveFromParent ();
//MyMusic mymusic = new MyMusic ();
//MainPage.MainFrameLayout.AddChidren (mymusic);
//mymusic.Show (true);
UserMiddle.DevicePageView.PageIndex = 0;
var myMusic = new MyMusic ();
UserMiddle.DevicePageView.AddChidren (myMusic);
myMusic.Show (true);
UserMiddle.DevicePageView.PageIndex = 1;
} else { }
};
alert.Show ();
};
var a31currentMusic = new Button {
Width = Application.GetRealWidth (350),
Height = Application.GetRealHeight (50),
X = Application.GetRealWidth (135),
Y = Application.GetRealHeight (190 - 130),
TextSize = 12,
TextAlignment = TextAlignment.CenterLeft,
Text = "UnKown",
TextColor = SkinStyle.Current.MusicTextColor,
};
a31frameLayout.AddChidren (a31currentMusic);
var a31currentartist = new Button {
Height = Application.GetRealHeight (50),
X = Application.GetRealWidth (135),
Y = Application.GetRealHeight (225 - 130),
TextSize = 12,
//TextColor = 0x80ffffff,
TextAlignment = TextAlignment.CenterLeft,
Text = "UnKown",
TextColor = SkinStyle.Current.MusicArtistTextColor,
};
a31frameLayout.AddChidren (a31currentartist);
EventHandler sharing = (sender, e) => {
if (-1 == a31player.ServerClientType) {
return;
}
A31MusicModel.Current = a31player;
A31PlayMusicPage a31playMusic = new A31PlayMusicPage ();
MainPage.MainFrameLayout.AddChidren (a31playMusic);
a31playMusic.Show (MusicInfo.MusicInfoList);
};
a31palyername.MouseUpEventHandler += sharing;
a31image.MouseUpEventHandler += sharing;
a31currentMusic.MouseUpEventHandler += sharing;
a31currentartist.MouseUpEventHandler += sharing;
var a31voice = new Button {
Width = Application.GetRealWidth (33),
Height = Application.GetRealHeight (31),
X = Application.GetRealWidth (135),
Y = Application.GetRealHeight (275 - 130),
UnSelectedImagePath = "MusicIcon/MusicVoice.png",
};
a31frameLayout.AddChidren (a31voice);
var a31horizontalSeekBarvol = new HorizontalSeekBar {
Width = Application.GetRealWidth (380),
Height = Application.GetRealHeight (50),
Radius = (uint)Application.GetRealHeight (25),
Y = Application.GetRealHeight (275 - 140),
X = Application.GetRealWidth (190),
SleepTime = 1000,
ProgressColor = 0xffFE5E00,
ThumbRadius = 9,
Max = 100,
ThumbColor = SkinStyle.Current.HorizontalSeekBarThumbColor,
};
a31frameLayout.AddChidren (a31horizontalSeekBarvol);
///主从显示效果
if (a31player.ServerClientType == -1) {
a31frameLayout.X = Application.GetRealWidth (38);
a31frameLayout.Width = Application.GetRealWidth (640 - 50);
serverOrClientBtn.X = Application.GetRealWidth (410 - 26);
a31btnPlayMusic.X = Application.GetRealWidth (518 - 26);
a31horizontalSeekBarvol.Width = Application.GetRealWidth (380 - 26);
if (a31player.IsEnd) {
a31player.IsEnd = false;
var Line1 = new Button {
Height = Application.GetRealHeight (115),
Width = Application.GetRealWidth (20 + 6 + 5),
X = Application.GetRealWidth (12),
//Y = Application.GetRealHeight (115),
//TextAlignment = TextAlignment.CenterLeft,
UnSelectedImagePath = "MusicIcon/line1.png",
};
BackgroundColorfra.AddChidren (Line1);
} else {
var Line2 = new Button {
Height = Application.GetRealHeight (210),
Width = Application.GetRealWidth (20 + 6),
X = Application.GetRealWidth (12),
//TextAlignment = TextAlignment.CenterLeft,
UnSelectedImagePath = "MusicIcon/line.png",
};
BackgroundColorfra.AddChidren (Line2);
}
}
a31horizontalSeekBarvol.ProgressChanged += (sender1, e1) => {
a31player.ControlVolume (a31horizontalSeekBarvol.Progress);
a31player.A31PlayStatus.vol = a31horizontalSeekBarvol.Progress.ToString ();
};
//a31titelFrameLayout.AddChidren (serverOrClientBtn);
refreshUI (a31image, a31player, a31horizontalSeekBarvol, a31btnPlayMusic, a31palyername, a31currentMusic, a31currentartist, serverOrClientBtn, a31frameLayout, colorBtn);
System.Threading.Tasks.Task.Run (() => {
System.Threading.Thread.CurrentThread.Name = "A31";
threadLists.Add (System.Threading.Thread.CurrentThread);
string album = null, artist = null;
while (true) {
System.Threading.Thread.Sleep (1000);
if (!a31player.IsOnLine) {
continue;
}
readStatus (a31player);
Application.RunOnMainThread (() => {
try {
#region 刷新歌手图片
if (a31player.A31PlayStatus.Album != album || a31player.A31PlayStatus.Artist != artist) {
album = a31player.A31PlayStatus.Album;
artist = a31player.A31PlayStatus.Artist;
var path = "MusicImage/AlbumArtistImage_";
if (Shared.IO.FileUtils.Exists (path + a31player.A31PlayStatus.Album)) {
a31image.ImagePath = path + a31player.A31PlayStatus.Album;
} else if (Shared.IO.FileUtils.Exists (path + a31player.A31PlayStatus.Artist)) {
a31image.ImagePath = path + a31player.A31PlayStatus.Artist;
} else {
a31image.ImagePath = "MusicIcon/musicplay1.png";
System.Threading.Tasks.Task.Run (() => {
var filePath = Shared.IO.FileUtils.DownLoadImageFormBaidu (album, artist);
if (filePath != null) {
Application.RunOnMainThread (() => {
a31image.ImagePath = filePath;
});
}
});
}
}
#endregion
} catch (Exception e) {
System.Console.WriteLine (e.Message);
}
refreshUI (a31image, a31player, a31horizontalSeekBarvol, a31btnPlayMusic, a31palyername, a31currentMusic, a31currentartist, serverOrClientBtn, a31frameLayout, colorBtn);
});
}
});
}
///
/// 读取主从关系
///
void readServerOrClientMode (bool readType)
{
try {
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++) {
var a31player = A31MusicModel.A31MusicModelList [i];
if (!a31player.IsOnLine) {
continue;
}
try {
if (readType) {
readMusicType (a31player);
}
a31player.ServerClientType = 0;
var result = openWeb ("http://" + a31player.IPAddress + "/httpapi.asp?command=multiroom:getSlaveList");
if (result == null) {
result = openWeb ("http://" + a31player.IPAddress + "/httpapi.asp?command=multiroom:getSlaveList");
}
if (result != null) {
a31player.Slave = Newtonsoft.Json.JsonConvert.DeserializeObject (result);
}
} catch (Exception e) {
var d = e.Message;
}
}
//分析主从关系
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++) {
var a31player = A31MusicModel.A31MusicModelList [i];
if (!a31player.IsOnLine) {
continue;
}
try {
if (a31player.Slave != null && "0" != a31player.Slave.slaves) {
a31player.ServerClientType = 1;//主的
for (int j = 0; j < a31player.Slave.slave_list.Count; j++) {
var slave = a31player.Slave.slave_list [j];
var tempA31Player = A31MusicModel.A31MusicModelList.Find ((obj) => slave.uuid.Replace ("uuid:", "") == obj.UniqueDeviceName);
if (tempA31Player == null) {
A31MusicModel.A31MusicModelList.Add (new A31MusicModel {
ServerClientType = -1,
IPAddress = slave.ip,
ServerIP = a31player.IPAddress,
UniqueDeviceName = slave.uuid.Replace ("uuid:", ""),
Name = slave.name,
IsCanShow = true,//
});
}
//如果找到就更新为从的
else {
tempA31Player.ServerClientType = -1;//从的
tempA31Player.IPAddress = slave.ip;
tempA31Player.ServerIP = a31player.IPAddress;
tempA31Player.Name = slave.name;//
tempA31Player.UniqueDeviceName = slave.uuid.Replace ("uuid:", "");
tempA31Player.IsCanShow = true;
}
}
}
} catch (Exception e) {
var ss = e.Message;
}
}
#region 重新排列播放器列表顺序.....
var MusicList = new List ();
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++) {
var music = A31MusicModel.A31MusicModelList [i];
if (!music.IsOnLine) {
continue;
}
///先找出主播放器的,在从主播放器找出从的播放器;
if (music.ServerClientType == 1) {
MusicList.Add (music);
if (music.Slave != null && "0" != music.Slave.slaves) {
for (int j = 0; j < music.Slave.slave_list.Count; j++) {
var Slave = music.Slave.slave_list [j];
var a31MusicModel = A31MusicModel.A31MusicModelList.Find ((obj) => obj.UniqueDeviceName == Slave.uuid.Replace ("uuid:", ""));
if (a31MusicModel != null) {
a31MusicModel.ServerClientType = -1;//从的
a31MusicModel.IPAddress = Slave.ip;
a31MusicModel.ServerIP = music.IPAddress;
a31MusicModel.Name = Slave.name;//
a31MusicModel.UniqueDeviceName = Slave.uuid.Replace ("uuid:", "");
a31MusicModel.IsCanShow = true;
MusicList.Add (a31MusicModel);
if (music.Slave.slave_list.Count - 1 == j) {
///标记从播放器最后那个
a31MusicModel.IsEnd = true;
}
} else {
MusicList.Add (new A31MusicModel {
ServerClientType = -1,
IPAddress = Slave.ip,
Name = Slave.name,
UniqueDeviceName = Slave.uuid.Replace ("uuid:", ""),
ServerIP = music.IPAddress,
IsCanShow = true,
});
if (music.Slave.slave_list.Count - 1 == j) {
///标记从播放器最后那个
a31MusicModel.IsEnd = true;
}
}
}
}
}
}
if (MusicList.Count != 0) {
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++) {
var a31player = A31MusicModel.A31MusicModelList [i];
if (!a31player.IsOnLine) {
continue;
}
///找出正常播放器的
if (a31player.ServerClientType == 0) {
MusicList.Add (A31MusicModel.A31MusicModelList [i]);
}
}
A31MusicModel.A31MusicModelList.Clear ();
A31MusicModel.A31MusicModelList = MusicList;
}
///标记从播放器最后那个
//A31MusicModel tempA31MusicModel = null;
//bool d = false;
//for (int i = 0; i < A31MusicModel.A31MusicModelList.Count;i++) {
// var a31player = A31MusicModel.A31MusicModelList [i];
// if(!a31player.IsCanShow){
// continue;
// }
// if(d){
// if(a31player.ServerClientType ==-1){
// tempA31MusicModel = a31player;
// if(i==A31MusicModel.A31MusicModelList.Count-1){
// tempA31MusicModel.IsEnd = true;
// }
// }else{
// if(tempA31MusicModel!=null){
// tempA31MusicModel.IsEnd = true;
// }
// if(a31player.ServerClientType == 0){
// d = false;
// }
// }
// }
// if(a31player.ServerClientType ==1){
// d = true;
// }
//}
#endregion
} catch (Exception e) {
var ss = e.Message;
}
}
///
/// 刷新当前音乐的界面
///
/// A31image.
/// A31player.
/// A31horizontal seek barvol.
/// A31btn play music.
/// A31palyername.
/// A31current music.
/// A31currentartist.
void refreshUI (ImageView a31image, A31MusicModel a31player, HorizontalSeekBar a31horizontalSeekBarvol, Button a31btnPlayMusic, Button a31palyername, Button a31currentMusic, Button a31currentartist, Button serverOrClientBtn, FrameLayout a31frameLayout, Button colorBtn)
{
try {
if (a31player.A31PlayStatus.IsMute) {
a31horizontalSeekBarvol.Progress = 0;
} else {
a31horizontalSeekBarvol.Progress = int.Parse (a31player.A31PlayStatus.vol);
}
a31btnPlayMusic.IsSelected = a31player.A31PlayStatus.status == "play";
a31palyername.Text = a31player.Name;
a31currentMusic.Text = a31player.A31PlayStatus.Title;
a31currentartist.Text = a31player.A31PlayStatus.Artist;
{
a31btnPlayMusic.Alpha = 1.0f;
a31btnPlayMusic.Enable = true;
a31image.Alpha = 1.0f;
a31palyername.Enable = true;
a31currentMusic.Alpha = 1.0f;
a31currentMusic.Enable = true;
}
if (1 == a31player.ServerClientType) {
colorBtn.Visible = true;
serverOrClientBtn.Visible = true;
serverOrClientBtn.UnSelectedImagePath = "MusicIcon/lifeSelected.png";
} else if (-1 == a31player.ServerClientType) {
a31btnPlayMusic.IsSelected = false;
a31currentMusic.Text = "";
a31currentartist.Text = "";
colorBtn.Visible = false;
serverOrClientBtn.Visible = true;
serverOrClientBtn.UnSelectedImagePath = "MusicIcon/life.png";
a31btnPlayMusic.Alpha = 0.5f;
a31btnPlayMusic.Enable = false;
a31image.Alpha = 0.5f;
a31palyername.Enable = false;
a31currentMusic.Alpha = 0.5f;
a31currentMusic.Enable = false;
}
} catch (Exception e) {
System.Console.WriteLine (e.Message);
}
}
//刷新播放器设备列表
void refreshDevice (MusicVerticalScrolViewLayout verticalScrolViewLayout)
{
clearA31Threads ();
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++) {
var a31player = A31MusicModel.A31MusicModelList [i];
a31player.IsCanShow = false;
a31player.IsOnLine = false;
}
MainPage.Loading.Start ();
seach ((obj) => {
if (obj == null) {
readServerOrClientMode (true);
A31MusicModel.Save ();
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++) {
var a31player = A31MusicModel.A31MusicModelList [i];
//这个状态是之前保存的,加载完成后要标记为不在线,后面再读取正确的状态
if (!a31player.IsCanShow) {
continue;
}
addViews (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;
}
});
}
//标记播放器类型是否有蓝牙功能。
void readMusicType (A31MusicModel a31player)
{
int tempDeviceType = 0;
Action action = (subnetID, deviceId, deviceType, command, targetSubnetID, targetDeviceID, usefullBytes, ipEndPoint) => {
if (command == Command.ReadRemarkACK && ipEndPoint.Address.ToString () == a31player.IPAddress.ToString ()) {
tempDeviceType = deviceType;
}
};
Packet.ReceviceAllDadaAction += action;
DateTime dateTime = DateTime.Now;
while ((DateTime.Now - dateTime).TotalMilliseconds <= 1000) {
///(byte)new Random ().Next (0, 255)表示随机数;
Control.ControlBytesSend (Command.ReadRemark, 255, 255, new byte [] { (byte)new Random ().Next (0, 255), (byte)new Random ().Next (0, 255) }, SendCount.One, new System.Net.IPEndPoint (System.Net.IPAddress.Parse (a31player.IPAddress), 6000));
System.Threading.Thread.Sleep (200);
if (tempDeviceType != 0) {
a31player.A31DeviceType = tempDeviceType;
break;
}
}
Packet.ReceviceAllDadaAction -= action;
}
///
/// 更新A31播放器的状态
///
///
void readStatus (A31MusicModel a31MusicModel)
{
try {
if (a31MusicModel.ServerClientType == -1) {
var result = openWeb ("http://" + a31MusicModel.ServerIP + "/httpapi.asp?command=multiroom:getSlaveList");
if (result != null) {
var slaves = Newtonsoft.Json.JsonConvert.DeserializeObject (result);
if (slaves != null && slaves.slave_list != null) {
var slave = slaves.slave_list.Find ((obj) => obj.uuid.Replace ("uuid:", "") == a31MusicModel.UniqueDeviceName);
if (slave != null) {
a31MusicModel.A31PlayStatus.vol = slave.volume;
}
}
}
return;
}
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient (1000);
webClient.Headers.Add ("Soapaction", "\"urn:schemas-upnp-org:service:AVTransport:1#GetInfoEx\"");
webClient.Headers.Add ("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
var recevieBytes = webClient.UploadData (new Uri ("http://" + a31MusicModel.IPAddress + ":" + a31MusicModel.Port + "/upnp/control/rendertransport1"), "POST", System.Text.Encoding.UTF8.GetBytes ("0"));
a31MusicModel.LastDateTime = DateTime.Now;
var se = System.Security.SecurityElement.FromString (System.Text.Encoding.UTF8.GetString (recevieBytes)).SearchForChildByTag ("s:Body").SearchForChildByTag ("u:GetInfoExResponse");
if ("PLAYING" == se.SearchForTextOfTag ("CurrentTransportState")) {
a31MusicModel.A31PlayStatus.status = "play";
} else {
a31MusicModel.A31PlayStatus.status = "stop";
}
a31MusicModel.A31PlayStatus.totlen = (DateTime.Parse (se.SearchForTextOfTag ("TrackDuration")) - DateTime.Parse ("00:00:00")).TotalMilliseconds.ToString ();
var trackMetaData = se.SearchForTextOfTag ("TrackMetaData");
if (string.IsNullOrEmpty (trackMetaData)) {
return;
}
if (A31MusicModel.IsJson (trackMetaData)) {
var a31QQSong = Newtonsoft.Json.JsonConvert.DeserializeObject (trackMetaData);
a31MusicModel.A31PlayStatus.Title = a31QQSong.title;
a31MusicModel.A31PlayStatus.Album = a31QQSong.album;
a31MusicModel.A31PlayStatus.Artist = a31QQSong.creator;
} else {
var metadata = trackMetaData.Replace ("", "").Replace ("&", "&");
var item = SecurityElement.FromString (metadata).SearchForChildByTag ("item");
a31MusicModel.A31PlayStatus.Title = item.SearchForTextOfTag ("dc:title");
a31MusicModel.A31PlayStatus.Artist = item.SearchForTextOfTag ("upnp:artist");
a31MusicModel.A31PlayStatus.Album = item.SearchForTextOfTag ("upnp:album");
}
a31MusicModel.A31PlayStatus.curpos = (DateTime.Parse (se.SearchForTextOfTag ("RelTime")) - DateTime.Parse ("00:00:00")).TotalMilliseconds.ToString ();
a31MusicModel.A31PlayStatus.vol = se.SearchForTextOfTag ("CurrentVolume");
a31MusicModel.A31PlayStatus.loop = se.SearchForTextOfTag ("LoopMode");
a31MusicModel.A31PlayStatus.Source = se.SearchForTextOfTag ("PlayMedium");
a31MusicModel.A31PlayStatus.playSource = se.SearchForTextOfTag ("TrackSource");
a31MusicModel.A31PlayStatus.TrackURL = se.SearchForTextOfTag ("TrackURI");
} catch (Exception ex) {
Console.WriteLine (ex.Message);
}
}
///
/// 获取A31的名称
///
///
///
///
string getDeviceName (string ip, int port)
{
string deviceName = null;
System.IO.StreamReader sr = null;
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
try {
var receviceBytes = webClient.DownloadData (new Uri ("http://" + ip + ":" + port + "/description.xml"));
sr = new System.IO.StreamReader (new System.IO.MemoryStream (receviceBytes), Encoding.UTF8);
string line = null;
string deviceType = null;
while ((line = sr.ReadLine ()) != null) {
//System.Console.WriteLine (line);
if (line.StartsWith ("")) {
deviceName = line.Replace ("", "").Replace ("", "");
} else if (line.StartsWith ("")) {
deviceType = line.Replace ("", "").Replace ("", "");
}
}
switch (deviceType) {
case "iEAST":
case "Linkplay Technology Inc.":
break;
//不是A31的音乐数据
default:
deviceName = null;
break;
}
} catch (Exception e) {
System.Console.WriteLine (e.Message);
} finally {
if (sr != null) {
sr.Close ();
}
}
return deviceName;
}
///
/// 1秒更新一次
///
public static System.DateTime dateTime = System.DateTime.Now.AddSeconds (-1);
void sendCommand (A31MusicModel a31, string cmd)
{
System.Threading.Tasks.Task.Run (() => {
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
try {
byte [] recevieBytes1 = webClient.DownloadData (new Uri ("http://" + a31.IPAddress + "/httpapi.asp?command=setPlayerCmd:" + cmd));
} catch (Exception e) {
System.Console.WriteLine (e.Message);
}
});
}
private void back_MouseDownEventHandler (object sender, MouseEventArgs e)
{
this.RemoveFromParent ();
}
[System.Serializable]
class a31wifimessage
{
public string res;
public List aplist;
}
[System.Serializable]
class a31message
{
public string ssid;
public string channel;
public string auth;
public string encry;
public string extch;
}
}
}