using System;
|
using System.Net;
|
using System.Text;
|
using System.Xml;
|
using Shared;
|
namespace HDL_ON.UI.Music
|
{
|
public class A31RadioList : FrameLayout
|
{
|
public A31RadioList()
|
{
|
Tag = "Music";
|
}
|
|
VerticalRefreshLayout middViewLayout;
|
/// <summary>
|
/// 先加载界面出来
|
/// </summary>
|
public void Show(string listName)
|
{
|
|
#region 界面布局------
|
this.BackgroundColor = MusicColor.ViewColor;
|
var topView = new TopView();
|
this.AddChidren(topView.TopFLayoutView());
|
topView.topNameBtn.Text= listName;
|
topView.clickBackBtn.MouseUpEventHandler += (sender, e) =>
|
{
|
this.RemoveFromParent();
|
};
|
middViewLayout = new VerticalRefreshLayout
|
{
|
BackgroundColor = MusicColor.WhiteColor,
|
Y = topView.fLayout.Bottom,
|
Height = Application.GetRealHeight(H_W.H - H_W.T_Height),
|
};
|
this.AddChidren(middViewLayout);
|
middViewLayout.BeginHeaderRefreshingAction += () =>
|
{
|
middViewLayout.EndHeaderRefreshing();
|
};
|
#endregion
|
MusicView(listName);
|
}
|
/// <summary>
|
/// 加载数据的方法
|
/// </summary>
|
public void MusicView(string listName)
|
{
|
middViewLayout.RemoveAll();
|
for (int i = 0; i < A31MusicModel.Current.CnRadioInfoList.Count; i++)
|
{
|
var radioInfo = A31MusicModel.Current.CnRadioInfoList[i];
|
#region Audio (播放列表)
|
if (radioInfo.Cntype == "audio")
|
{
|
|
if (string.IsNullOrEmpty(radioInfo.Title))
|
{
|
//防止歌曲名字为空抛异常
|
radioInfo.Title = " ";
|
}
|
FrameLayout radioViewFl = new FrameLayout
|
{
|
Width = Application.GetRealWidth(375),
|
Height = Application.GetRealHeight(67),
|
};
|
middViewLayout.AddChidren(radioViewFl);
|
//默认电台图标
|
Button radioIconBtn = new Button
|
{
|
X = Application.GetRealWidth(16),
|
Y = Application.GetRealHeight(18),
|
Width = Application.GetMinRealAverage(32),
|
Height = Application.GetMinRealAverage(32),
|
UnSelectedImagePath = "MusicIcon/radioIcon.png",
|
};
|
radioViewFl.AddChidren(radioIconBtn);
|
//电台名称
|
Button radioNameBtn = new Button
|
{
|
X = Application.GetRealWidth(60),
|
Y = Application.GetRealHeight(23),
|
Width = Application.GetRealWidth(250),
|
Height = Application.GetRealHeight(22),
|
TextColor = MusicColor.MusicTxet14Color,
|
TextSize = TextSize.Text16,
|
TextAlignment = TextAlignment.CenterLeft,
|
Tag = radioInfo.URL,
|
Text = radioInfo.Title.Trim()
|
};
|
radioViewFl.AddChidren(radioNameBtn);
|
|
//添加喜爱电台
|
Button loveIcon = new Button
|
{
|
X = Application.GetRealWidth(335),
|
Y = Application.GetRealHeight(20),
|
Width = Application.GetMinRealAverage(28),
|
Height = Application.GetMinRealAverage(28),
|
UnSelectedImagePath = "MusicIcon/love.png",
|
SelectedImagePath = "MusicIcon/loveSelected.png",
|
};
|
radioViewFl.AddChidren(loveIcon);
|
loveIcon.MouseUpEventHandler += (sender, e) =>
|
{
|
loveIcon.IsSelected = !loveIcon.IsSelected;
|
if (loveIcon.IsSelected)
|
{
|
///查找电台是否存在在列表
|
var radioselected= A31MusicModel.Current.LoveRadioInfoList.Find((m) => m.URL == radioInfo.URL);
|
if (radioselected == null)
|
{
|
///没有存在就添加
|
A31MusicModel.Current.LoveRadioInfoList.Add(radioInfo);
|
}
|
}
|
else
|
{
|
///查找音电台是否存在在列表
|
var radioselected = A31MusicModel.Current.LoveRadioInfoList.Find((m) => m.URL == radioInfo.URL);
|
if (radioselected != null)
|
{
|
///存在就删除
|
A31MusicModel.Current.LoveRadioInfoList.Remove(radioInfo);
|
}
|
|
}
|
A31MusicModel.Save();
|
};
|
var v = A31MusicModel.Current.LoveRadioInfoList.Find((m) => m.URL == radioInfo.URL);
|
if (v != null)
|
{
|
loveIcon.IsSelected = true;
|
}
|
else
|
{
|
loveIcon.IsSelected = false;
|
}
|
|
//点击控件
|
Button clickBtn = new Button
|
{
|
Width = Application.GetRealWidth(280),
|
Height = Application.GetRealHeight(44),
|
Tag = radioInfo,//标记播放哪一首歌曲
|
};
|
radioViewFl.AddChidren(clickBtn);
|
///点击播放事件
|
clickBtn.MouseUpEventHandler += (sender, e) =>
|
{
|
|
System.Threading.Tasks.Task.Run(() =>
|
{
|
System.Threading.Thread.Sleep(50);
|
Application.RunOnMainThread(() =>
|
{
|
//移除界面
|
A31MusicModel.Current.A31PlayStatus.Title = radioNameBtn.Text;
|
MainPage.BasePageView.RemoveViewByTag("Music");
|
System.Threading.Tasks.Task.Run(() =>
|
{
|
PushList(clickBtn.Tag as MusicInfo, listName);
|
});
|
});
|
});
|
|
};
|
|
}
|
#endregion
|
|
#region Link(列表)
|
if (radioInfo.Cntype == "link")
|
{
|
RowLayout addFlieRow = new RowLayout
|
{
|
Height = Application.GetRealHeight(78),
|
LineColor = MusicColor.WhiteColor,
|
SubViewWidth = Application.GetRealWidth(90),//改变编辑控件宽度多少;
|
};
|
middViewLayout.AddChidren(addFlieRow);
|
//文件图标
|
Button fileIconBtn = new Button
|
{
|
X = Application.GetRealWidth(16),
|
Y = Application.GetRealHeight(8),
|
Width = Application.GetMinRealAverage(62),
|
Height = Application.GetMinRealAverage(62),
|
UnSelectedImagePath = "MusicIcon/file.png",
|
};
|
addFlieRow.AddChidren(fileIconBtn);
|
//文件名控件
|
Button fileNameBtn = new Button
|
{
|
X = fileIconBtn.Right + Application.GetRealWidth(16),
|
Y = Application.GetRealHeight(29),
|
Width = Application.GetRealWidth(220),
|
Height = Application.GetRealHeight(20),
|
TextColor = MusicColor.TextColor,
|
TextSize = TextSize.Text14,
|
TextAlignment = TextAlignment.CenterLeft,
|
Text = radioInfo.Title,
|
};
|
addFlieRow.AddChidren(fileNameBtn);
|
|
Button clickBtn = new Button
|
{
|
X = fileIconBtn.Right + Application.GetRealWidth(16),
|
Width = Application.GetRealWidth(375 - 94),
|
Height = Application.GetRealHeight(78),
|
Tag = radioInfo.URL,
|
};
|
addFlieRow.AddChidren(clickBtn);
|
clickBtn.MouseUpEventHandler += (sender, e) =>
|
{
|
Loading loading = new Loading();
|
this.AddChidren(loading);
|
loading.Start();
|
System.Threading.Tasks.Task.Run(() =>
|
{
|
try
|
{
|
var redString = SendMethod.ReadRadioList(clickBtn.Tag.ToString());
|
if (redString == null)
|
{
|
return;
|
}
|
int startIndex2 = redString.IndexOf("<body>");
|
int endIndex = redString.IndexOf("</body>") + "</body>".Length;
|
if (endIndex <= startIndex2)
|
{
|
return;
|
}
|
var ss = redString.Substring(startIndex2, endIndex - startIndex2);
|
XmlDocument xml = new XmlDocument();
|
xml.LoadXml(ss);
|
var listString = xml.SelectSingleNode("body");
|
if (listString == null)
|
{
|
return;
|
}
|
A31MusicModel.Current.CnRadioInfoList.Clear();
|
foreach (XmlNode childNodes in listString.ChildNodes)
|
{
|
if (childNodes.Name == "outline" && childNodes.Attributes["URL"] != null)
|
{
|
MusicInfo musicInfoCN = new MusicInfo();
|
musicInfoCN.URL = childNodes.Attributes["URL"] == null ? "" : childNodes.Attributes["URL"].Value;
|
musicInfoCN.Title = childNodes.Attributes["text"] == null ? "" : childNodes.Attributes["text"].Value;
|
//musicInfoCN.Image = childNodes.Attributes["image"] == null ? "" : childNodes.Attributes["image"].Value;
|
|
if (childNodes.Attributes["image"] != null)
|
{
|
#region 初始化网络图片路径
|
string path = "";
|
if (Shared.Application.IsPad)
|
{
|
path = "Pad/" + "MusicImage/";
|
}
|
else
|
{
|
path = "Phone/" + "MusicImage/";
|
}
|
path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, path + childNodes.Attributes["image"].Value.Replace('/', '_').Replace('\\', '_').Replace(':', '_').Replace('*', '_').Replace('?', '_').Replace('"', '_').Replace('<', '_').Replace('>', '_').Replace('|', '_'));
|
musicInfoCN.Image = path;
|
#endregion
|
//Shared.IO.FileUtils.DownLoadImage(musicInfoCN.Image, childNodes.Attributes["image"].Value);
|
}
|
|
musicInfoCN.Cntype = childNodes.Attributes["type"] == null ? "" : childNodes.Attributes["type"].Value;
|
A31MusicModel.Current.CnRadioInfoList.Add(musicInfoCN);
|
}
|
foreach (XmlNode a in childNodes.ChildNodes)
|
{
|
if (a.Name == "outline")
|
{
|
MusicInfo musicInfoCN = new MusicInfo();
|
musicInfoCN.URL = a.Attributes["URL"] == null ? "" : a.Attributes["URL"].Value;
|
musicInfoCN.Title = a.Attributes["text"] == null ? "" : a.Attributes["text"].Value;
|
if (a.Attributes["image"] != null)
|
{
|
#region 初始化网络图片路径
|
string path = "";
|
if (Shared.Application.IsPad)
|
{
|
path = "Pad/" + "MusicImage/";
|
}
|
else
|
{
|
path = "Phone/" + "MusicImage/";
|
}
|
path = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, path + a.Attributes["image"].Value.Replace('/', '_').Replace('\\', '_').Replace(':', '_').Replace('*', '_').Replace('?', '_').Replace('"', '_').Replace('<', '_').Replace('>', '_').Replace('|', '_'));
|
musicInfoCN.Image = path;
|
#endregion
|
// Shared.IO.FileUtils.DownLoadImage(musicInfoCN.Image, a.Attributes["image"].Value);
|
}
|
musicInfoCN.Cntype = a.Attributes["type"] == null ? "" : a.Attributes["type"].Value;
|
A31MusicModel.Current.CnRadioInfoList.Add(musicInfoCN);
|
}
|
}
|
}
|
}
|
catch { }
|
finally
|
{
|
Application.RunOnMainThread(() =>
|
{
|
loading.Hide();
|
A31RadioList a31RadioList = new A31RadioList();
|
MainPage.BasePageView.AddChidren(a31RadioList);
|
MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
|
a31RadioList.Show(fileNameBtn.Text);
|
});
|
}
|
});
|
};
|
}
|
#endregion
|
|
}
|
}
|
|
public void PushList(MusicInfo musicInfo, string listName)
|
{
|
StringBuilder sb = new StringBuilder();
|
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");
|
sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
|
sb.AppendLine("<s:Body>");
|
sb.AppendLine("<u:CreateQueue xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
|
sb.AppendLine("<QueueContext>");
|
sb.AppendLine("<?xml version="1.0" ?>");
|
sb.AppendLine("<PlayList>");
|
sb.AppendLine("<ListName>" + listName + "</ListName>");
|
sb.AppendLine("<ListInfo>");
|
sb.AppendLine("<Radio>1</Radio>");
|
sb.AppendLine("<SourceName>TuneIn</SourceName>");
|
sb.AppendLine("<TrackNumber>1</TrackNumber>");
|
sb.AppendLine("<SearchUrl>http://opml.radiotime.com/Tune.ashx?id=s112201&amp;partnerId=yvcOjvJP</SearchUrl>");
|
sb.AppendLine("<Quality>3</Quality>");
|
sb.AppendLine("<RealIndex>0</RealIndex>");
|
sb.AppendLine("<SrcParent></SrcParent>");
|
sb.AppendLine("<PicUrl></PicUrl>");
|
sb.AppendLine("</ListInfo>");
|
sb.AppendLine("<Tracks>");
|
|
|
var url =SendMethod.ReadRadioList(musicInfo.URL);
|
|
if (url != null)
|
{
|
url = url.TrimEnd('\n').Replace("&", "&amp;");
|
}
|
else
|
{
|
return;
|
}
|
|
sb.AppendLine("<Track" + 1 + ">");
|
sb.AppendLine("<Source>TuneIn</Source>");
|
sb.AppendLine("<URL>" + url + "</URL>");
|
sb.AppendLine("<Source>TuneIn</Source>");
|
sb.AppendLine("<Id>0</Id>");
|
sb.AppendLine("<Key>http://so.ard.iyyin.com/s/song_with_out?q=%E8%8A%B1%E9%83%BD%E7%94%B5%E5%8F%B0+100.5+%28%E6%B5%81%E8%A1%8C%E9%9F%B3%E4%B9%90%E5%89%8D40%29%20TuneIn&amp;size=50&amp;page=1</Key>");
|
sb.AppendLine("<Metadata>");
|
sb.AppendLine("&lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:song=&quot;www.wiimu.com/song/&quot; xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt;");
|
sb.AppendLine("&lt;upnp:class&gt;object.item.audioItem.musicTrack&lt;/upnp:class&gt;");
|
sb.AppendLine("&lt;item&gt;");
|
sb.AppendLine("&lt;song:bitrate&gt;0&lt;/song:bitrate&gt;");
|
sb.AppendLine("&lt;song:id&gt;0&lt;/song:id&gt;");
|
sb.AppendLine("&lt;song:singerid&gt;0&lt;/song:singerid&gt;");
|
sb.AppendLine("&lt;song:albumid&gt;0&lt;/song:albumid&gt;");
|
sb.AppendLine("&lt;res protocolInfo=&quot;http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;&quot; duration=&quot;1000&quot;&gt;" + url + "&lt;/res&gt;");
|
sb.AppendLine("&lt;dc:title&gt;" + musicInfo.Title + "&lt;/dc:title&gt;");
|
sb.AppendLine("&lt;upnp:artist&gt;TuneIn&lt;/upnp:artist&gt;");
|
sb.AppendLine("&lt;upnp:album&gt;&lt;/upnp:album&gt;");
|
sb.AppendLine("&lt;upnp:albumArtURI&gt;" + musicInfo.Image + "&lt;/upnp:albumArtURI&gt;");
|
sb.AppendLine("&lt;/item&gt;");
|
sb.AppendLine("&lt;/DIDL-Lite&gt;");
|
sb.AppendLine("</Metadata>");
|
sb.AppendLine("</Track" + 1 + ">");
|
|
|
sb.AppendLine("</Tracks>");
|
sb.AppendLine("</PlayList>");
|
sb.AppendLine("</QueueContext>");
|
sb.AppendLine("</u:CreateQueue>");
|
sb.AppendLine("</s:Body>");
|
sb.AppendLine("</s:Envelope>");
|
//sendMusicLists(a31.IPAddress, a31.Port, "CreateQueue", sb.ToString());
|
SendMusicLists(A31MusicModel.Current.IPAddress, A31MusicModel.Current.Port, "CreateQueue", sb.ToString());
|
|
StringBuilder playString = new StringBuilder();
|
playString.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");
|
playString.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
|
playString.AppendLine("<s:Body>");
|
playString.AppendLine("<u:PlayQueueWithIndex xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
|
playString.AppendLine("<QueueName>" + listName + "</QueueName>");
|
playString.AppendLine("<Index>" + 1 + "</Index>");
|
playString.AppendLine("</u:PlayQueueWithIndex>");
|
playString.AppendLine("</s:Body>");
|
playString.AppendLine("</s:Envelope>");
|
Play(A31MusicModel.Current.IPAddress, A31MusicModel.Current.Port, playString.ToString());
|
|
}
|
|
void SendMusicLists(string ip, int port, string soapAction, string listInfo)
|
{
|
WebClient webClient = new WebClient();
|
webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#" + soapAction + "\"");
|
webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
|
try
|
{
|
byte[] recevieBytes = webClient.UploadData(new Uri("http://" + ip + ":" + port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(listInfo));
|
var s = System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
|
}
|
catch { }
|
}
|
|
void Play(string ip, int port, string info)
|
{
|
WebClient webClient = new 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://" + ip + ":" + port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(info));
|
var s = System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
|
}
|
catch { }
|
}
|
}
|
}
|