using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Security; using System.Text; using Shared; namespace HDL_ON.UI.Music { public class SendMethod { /// ///搜索A31音乐播放器 /// /// /// /// public static 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); } } }); } /// /// 更新A31播放器的状态 /// /// public static 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; } WebClient webClient = new WebClient(); 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("&", "&amp;"); 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); } } public static string OpenWeb(string url) { try { var webClient = new WebClient(); return webClient.DownloadString(url); } catch (Exception e) { System.Console.WriteLine(e.Message); return null; } } /// /// 获取A31的名称 /// /// /// /// static string GetDeviceName(string ip, int port) { string deviceName = null; System.IO.StreamReader sr = null; WebClient webClient = new WebClient(); 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; } /// /// 切换播放器模式,修改音乐名称,切换蓝牙,线路输入 /// /// public static void SendCommand(string url) { System.Threading.Tasks.Task.Run(() => { WebClient webClient = new WebClient(); try { byte[] recevieBytes1 = webClient.DownloadData(new Uri(url)); } catch (Exception ex) { //this.IPAddress = ex.Message; } }); } /// /// 播放 /// public static void Play() { System.Threading.Tasks.Task.Run(() => { StringBuilder sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine("0"); sb.AppendLine("1"); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Play\""); webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); try { webClient.UploadData(new Uri("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString())); } catch { } }); } /// /// 暂停 /// public static void Pause() { System.Threading.Tasks.Task.Run(() => { StringBuilder sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine("0"); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Pause\""); webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); try { webClient.UploadData(new Uri("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString())); } catch { } }); } /// /// 下一曲 /// public static void Next() { System.Threading.Tasks.Task.Run(() => { StringBuilder sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine("0"); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Next\""); webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); try { webClient.UploadData(new Uri("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString())); } catch { } }); } /// /// 上一曲 /// public static void Previous() { System.Threading.Tasks.Task.Run(() => { StringBuilder sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(" "); sb.AppendLine("0"); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Previous\""); webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); try { webClient.UploadData(new Uri("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString())); } catch { } }); } /// /// 快进 /// public static void Seek(string seekvolume) { System.Threading.Tasks.Task.Run(() => { StringBuilder sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(" "); sb.AppendLine("0"); sb.AppendLine("REL_TIME"); sb.AppendLine("" + seekvolume + ""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:AVTransport:1#Seek\""); webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); try { webClient.UploadData(new Uri("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/rendertransport1"), "POST", Encoding.UTF8.GetBytes(sb.ToString())); } catch { } }); } /// /// 获取USb的列表 /// public static List GetUsbList() { var musicInfoList = new List(); musicInfoList.Clear(); var usbString = GetUSBPlayList(); if (usbString == null) { return musicInfoList; } var se = System.Security.SecurityElement.FromString(usbString); if (se == null) { return musicInfoList; } while (se.Children != null) { se = se.Children[0] as System.Security.SecurityElement; } foreach (SecurityElement track in SecurityElement.FromString(se.Text).SearchForChildByTag("Tracks").Children) { MusicInfo musicInfo = new MusicInfo(); musicInfo.URL = track.SearchForTextOfTag("URL"); var metadata = track.SearchForTextOfTag("Metadata").Replace("&", "&"); var item = SecurityElement.FromString(metadata).SearchForChildByTag("item"); musicInfo.Title = item.SearchForTextOfTag("dc:title"); musicInfo.Artist = item.SearchForTextOfTag("upnp:artist"); musicInfo.Album = item.SearchForTextOfTag("upnp:album"); musicInfo.Duration = item.SearchForTextOfTag("res"); musicInfo.AlbumId = item.SearchForTextOfTag("song:albumid"); musicInfoList.Add(musicInfo); } return musicInfoList; } /// ///请求USB列表命令 /// public static string GetUSBPlayList() { StringBuilder getPlayList = new StringBuilder(); getPlayList.AppendLine(""); getPlayList.AppendLine(""); getPlayList.AppendLine(""); getPlayList.AppendLine("USBDiskQueue"); getPlayList.AppendLine(""); getPlayList.AppendLine(""); getPlayList.AppendLine(""); WebClient webClient = new WebClient(); webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#BrowseQueue\""); webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); try { byte[] recevieBytes = webClient.UploadData(new Uri("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(getPlayList.ToString())); return System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length); } catch { } return null; } /// /// 获取当前播放的列表 /// public static string GetCurrentPlayList() { System.Text.StringBuilder getPlayList = new System.Text.StringBuilder(); getPlayList.AppendLine(""); getPlayList.AppendLine(""); getPlayList.AppendLine(""); getPlayList.AppendLine(""); getPlayList.AppendLine("CurrentQueue"); getPlayList.AppendLine(""); getPlayList.AppendLine(""); getPlayList.AppendLine(""); System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#BrowseQueue\""); webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); try { byte[] recevieBytes = webClient.UploadData(new Uri("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(getPlayList.ToString())); return System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length); } catch { } return null; } /// /// 播放音乐命令 /// /// 列表名称 /// 播放索引 public static void ListMusicPlay(string listName, object number) { System.Text.StringBuilder playstrings = new System.Text.StringBuilder(); playstrings.AppendLine(""); playstrings.AppendLine(""); playstrings.AppendLine(""); playstrings.AppendLine(""); playstrings.AppendLine("" + listName + ""); playstrings.AppendLine("" + number + ""); playstrings.AppendLine(""); playstrings.AppendLine(""); playstrings.AppendLine(""); System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#PlayQueueWithIndex\""); webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\""); try { byte[] recevieBytes = webClient.UploadData(new Uri("http://" + A31MusicModel.Current.IPAddress + ":" + A31MusicModel.Current.Port + "/upnp/control/PlayQueue1"), "POST", Encoding.UTF8.GetBytes(playstrings.ToString())); var s = System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length); } catch { } } static System.Threading.Thread thread; /// /// 修改蓝牙名称 /// /// public static void ModifyBluetoothName(string message) { if (thread != null) { thread.Abort(); } thread = new System.Threading.Thread(() => { var tcpClient = new TcpClient() { ReceiveTimeout = 1000 }; try { //进行连接 tcpClient.Connect(new IPEndPoint(IPAddress.Parse(A31MusicModel.Current.IPAddress), 8899)); var buffter = Encoding.UTF8.GetBytes(message); var sendByes = new byte[20 + buffter.Length]; sendByes[0] = 0x18; sendByes[1] = 0x96; sendByes[2] = 0x18; sendByes[3] = 0x20; sendByes[4] = (byte)((buffter.Length & 0x000000FF) >> 0); sendByes[5] = (byte)((buffter.Length & 0x0000FF00) >> 8); sendByes[6] = (byte)((buffter.Length & 0x00FF0000) >> 16); sendByes[7] = (byte)((buffter.Length & 0xFF000000) >> 24); var checkSum = getCheckSum(buffter); sendByes[8] = (byte)((checkSum & 0x000000FF) >> 0); sendByes[9] = (byte)((checkSum & 0x0000FF00) >> 8); sendByes[10] = (byte)((checkSum & 0x00FF0000) >> 16); sendByes[11] = (byte)((checkSum & 0xFF000000) >> 24); sendByes[16] = 0x08; Array.Copy(buffter, 0, sendByes, 20, buffter.Length); tcpClient.GetStream().Write(sendByes, 0, sendByes.Length); tcpClient.GetStream().Flush(); thread = null; } catch { } finally { tcpClient.Close(); } }); thread.Start(); } static uint getCheckSum(byte[] buffer) { uint check_sum = 0; for (int i = 0; i < buffer.Length; i++) { check_sum += buffer[i]; } return check_sum; } public class A31QQSong { public string title = string.Empty; public string album = string.Empty; public string creator = string.Empty; } } }