using Shared;
|
using Shared.SimpleControl;
|
using Shared.SimpleControl.R;
|
using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using System.Xml;
|
using SmartHome;
|
|
namespace Shared.SimpleControl.Phone.Music
|
{
|
/// <summary>
|
/// DLNA服务器播放音乐列表
|
/// </summary>
|
class A31DlnaListMusic : FrameLayout
|
{
|
bool isExited;
|
public void show (DLNAServer dlnaServer, string listName, List<MusicInfo> list)
|
{
|
AddChidren (new Button {
|
Height = Application.GetRealHeight (36),
|
BackgroundColor = SkinStyle.Current.MusicTopFrameLayout,
|
});
|
|
var topFrameLayout = new FrameLayout {
|
Height = Application.GetRealHeight (100),
|
Y = Application.GetRealHeight (36),
|
BackgroundColor = SkinStyle.Current.MusicTopFrameLayout,
|
};
|
AddChidren (topFrameLayout);
|
|
var musiclist = new Button {
|
Text = listName,
|
TextColor=SkinStyle.Current.MusicTextColor,
|
};
|
topFrameLayout.AddChidren (musiclist);
|
|
var hdl = new Button {
|
Width = Application.GetRealWidth (154),
|
Height = Application.GetRealHeight (90),
|
X = Application.GetRealWidth (486),
|
Gravity = Gravity.CenterVertical,
|
UnSelectedImagePath = MainPage.LogoString,
|
};
|
topFrameLayout.AddChidren (hdl);
|
|
var back = new Button {
|
Width = Application.GetRealWidth (82),
|
Height = Application.GetRealHeight (89),
|
X = Application.GetRealWidth (10),
|
Gravity = Gravity.CenterVertical,
|
UnSelectedImagePath = "MusicIcon/HomepageBack.png",
|
};
|
topFrameLayout.AddChidren (back);
|
back.MouseDownEventHandler += (sender, e) => {
|
RemoveFromParent ();
|
isExited = true;
|
};
|
|
VerticalScrolViewLayout middle = new VerticalScrolViewLayout ();
|
middle.Y = topFrameLayout.Bottom;
|
middle.Height = Application.GetRealHeight (Application.DesignHeight - 136);
|
//middle.BackgroundColor = 0xff2F2F2F;
|
middle.BackgroundColor = SkinStyle.Current.MusicVerticalScrolViewLayout;
|
AddChidren (middle);
|
|
var listMusicInfo = new List<MusicInfo> ();
|
List<Button> buttonList = new List<Button> (100);
|
foreach (var music in list) {
|
if (!music.IsDirectory) {
|
listMusicInfo.Add (music);
|
var dlnarowLayout = new RowLayout {
|
Height = Application.GetRealHeight (100),
|
LineColor = SkinStyle.Current.MusicRowLayoutLineColor,
|
};
|
middle.AddChidren (dlnarowLayout);
|
|
var title = new Button {
|
Width = Application.GetRealWidth (61),
|
Height = Application.GetRealHeight (61),
|
X = Application.GetRealWidth (20),
|
Gravity = Gravity.CenterVertical,
|
Radius = (uint)Application.GetRealHeight (4),
|
};
|
dlnarowLayout.AddChidren (title);
|
|
var path = "";
|
if (Shared.Application.IsPad) {
|
path = "Pad/" + "MusicImage/AlbumArtistImage_";
|
} else {
|
path = "Phone/" + "MusicImage/AlbumArtistImage_";
|
}
|
if (Shared.IO.FileUtils.Exists (path + music.Album)) {
|
title.UnSelectedImagePath = path + music.Album;
|
} else if (Shared.IO.FileUtils.Exists (path + music.Artist)) {
|
title.UnSelectedImagePath = path + music.Artist;
|
} else {
|
title.UnSelectedImagePath = "MusicIcon/musicplay1.png";
|
title.AddTag ("Album", music.Album);
|
title.AddTag ("Artist", music.Artist);
|
buttonList.Add (title);
|
}
|
|
var titles = new Button {
|
Width = Application.GetRealWidth (400),
|
Height = Application.GetRealHeight (50),
|
TextAlignment = TextAlignment.BottomLeft,
|
X = Application.GetRealWidth (100),
|
Text = music.Title,
|
Tag = music,
|
TextColor = SkinStyle.Current.MusicTextColor,
|
};
|
dlnarowLayout.AddChidren (titles);
|
|
var artists = new Button {
|
Width = Application.GetRealWidth (400),
|
Height = Application.GetRealHeight (50),
|
TextAlignment = TextAlignment.TopLeft,
|
X = Application.GetRealWidth (100),
|
Y = titles.Bottom,
|
//TextColor = 0x75ffffff,
|
Text = music.Artist + "-" + music.Album,
|
Tag = music,
|
TextColor = SkinStyle.Current.MusicArtistTextColor,
|
};
|
dlnarowLayout.AddChidren (artists);
|
|
var addlikelits = new Button {
|
Width = Application.GetRealWidth (72),
|
Height = Application.GetRealHeight (58),
|
X = Application.GetRealWidth (560),
|
UnSelectedImagePath = "MusicIcon/add.png",
|
Gravity = Gravity.CenterVertical,
|
};
|
dlnarowLayout.AddChidren (addlikelits);
|
|
addlikelits.MouseUpEventHandler += (sender, e) => {
|
A31MyList A31MyList = new A31MyList ();
|
MainPage.MainFrameLayout.AddChidren (A31MyList);
|
A31MyList.Show (new MusicInfo { SourceType = "URL", Title = music.Title, URL = music.URL, Artist = music.Artist, Album = music.Album });
|
};
|
|
EventHandler<MouseEventArgs> musicsource = (sender, e) => {
|
dlnarowLayout.BackgroundColor = 0xffFE5E00;
|
System.Threading.Tasks.Task.Run (() => {
|
System.Threading.Thread.Sleep (50);
|
|
Application.RunOnMainThread (() => {
|
for (int i = MainPage.MainFrameLayout.ChildrenCount - 1; 0 <= i; i--) {
|
var view = MainPage.MainFrameLayout.GetChildren (i);
|
if (view.GetType () == typeof (A31DlnaListMusic)) {
|
view.RemoveFromParent ();
|
} else {
|
break;
|
}
|
}
|
MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1);
|
MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1);
|
MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1);
|
MainPage.MainFrameLayout.RemoveAt (MainPage.MainFrameLayout.ChildrenCount - 1);
|
|
A31MusicModel.Current.CurrentPlayMusicInfoList = listMusicInfo;
|
A31PlayMusicPage a31PlayMusicPage = new A31PlayMusicPage ();
|
MainPage.MainFrameLayout.AddChidren (a31PlayMusicPage);
|
a31PlayMusicPage.Show (listMusicInfo);
|
// a31PlayMusicPage.Source = "SONGLIST-NETWORK";
|
A31MusicModel.Current.A31PlayStatus.Title = titles.Text;
|
|
System.Threading.Tasks.Task.Run (() => {
|
|
pushList (titles.Tag as MusicInfo, listName);
|
//MyMusic.dateTime = System.DateTime.Now.AddSeconds (-5);
|
});
|
|
|
});
|
});
|
};
|
|
artists.MouseUpEventHandler += musicsource;
|
titles.MouseUpEventHandler += musicsource;
|
|
|
}
|
//if (music.ID == "object.container.storageFolder")
|
else {
|
var dlnarow = new RowLayout {
|
Height = Application.GetRealHeight (100),
|
LineColor = SkinStyle.Current.MusicRowLayoutLineColor,
|
};
|
middle.AddChidren (dlnarow);
|
|
var radius = new Button {
|
Width = Application.GetRealWidth (36),
|
Height = Application.GetRealHeight (36),
|
TextAlignment = TextAlignment.CenterLeft,
|
X = Application.GetRealWidth (20),
|
UnSelectedImagePath = "MusicIcon/radius.png",
|
Gravity = Gravity.CenterVertical,
|
};
|
dlnarow.AddChidren (radius);
|
|
var dlnaback1 = new Button {
|
Width = Application.GetRealWidth (87),
|
Height = Application.GetRealHeight (100),
|
UnSelectedImagePath = "MusicIcon/Next.png",
|
SelectedImagePath = "MusicIcon/NextSelecte.png",
|
X = Application.GetRealWidth (550),
|
Gravity = Gravity.CenterVertical,
|
};
|
dlnarow.AddChidren (dlnaback1);
|
|
var dlnalists1 = new Button {
|
Width = Application.GetRealWidth (400),
|
TextAlignment = TextAlignment.CenterLeft,
|
X = Application.GetRealWidth (80),
|
Text = music.Title,
|
Tag = music.ID,
|
TextColor = SkinStyle.Current.MusicTextColor,
|
|
};
|
dlnarow.AddChidren (dlnalists1);
|
|
dlnalists1.MouseUpEventHandler += (sender, e) => {
|
MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load));
|
System.Threading.Tasks.Task.Run (() => {
|
try {
|
var dlnaString = dlnalistsong (dlnaServer, dlnalists1.Tag.ToString ());
|
if (dlnaString == null) {
|
return;
|
}
|
list.Clear ();
|
dlnaString = dlnaString.Replace ("&", "&").Replace ("<", "<").Replace (">", ">").Replace (""", "\"").Replace ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").Replace ("&", "");
|
|
int StartIndex = dlnaString.IndexOf ("<DIDL-Lite");
|
int endIndex = dlnaString.IndexOf ("</DIDL-Lite>") + "</DIDL-Lite>".Length;
|
if (endIndex <= StartIndex) {
|
return;
|
}
|
var xml = new XmlDocument ();
|
|
xml.LoadXml (dlnaString.Substring (StartIndex, endIndex - StartIndex));
|
|
foreach (XmlNode node in xml.ChildNodes [0].ChildNodes) {
|
MusicInfo musicInfo = new MusicInfo ();
|
musicInfo.ID = node.Attributes ["id"].Value;
|
foreach (XmlNode temnode in node.ChildNodes) {
|
switch (temnode.Name) {
|
case "upnp:class":
|
musicInfo.IsDirectory = temnode.InnerText != "object.item.audioItem.musicTrack";
|
break;
|
case "upnp:album":
|
musicInfo.Album = temnode.InnerText;
|
break;
|
case "dc:title":
|
musicInfo.Title = temnode.InnerText;
|
break;
|
case "upnp:artist":
|
musicInfo.Artist = temnode.InnerText;
|
break;
|
case "res":
|
if (musicInfo.URL== null) {///
|
musicInfo.URL = temnode.InnerText;
|
}
|
var duration = temnode.Attributes ["duration"];
|
if (duration != null) {
|
musicInfo.Duration = duration.Value;
|
}
|
break;
|
}
|
}
|
list.Add (musicInfo);
|
}
|
} catch { } finally {
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Hide ();
|
A31DlnaListMusic a31DlnaListMusic = new A31DlnaListMusic ();
|
MainPage.MainFrameLayout.AddChidren (a31DlnaListMusic);
|
a31DlnaListMusic.show (dlnaServer, music.Title, list);
|
});
|
}
|
});
|
};
|
}
|
}
|
|
System.Threading.Tasks.Task.Run (() => {
|
for (int i = 0; i < buttonList.Count; i++) {
|
try {
|
if (isExited) {
|
return;
|
}
|
var button = buttonList [i];
|
var album = button.GetTagByKey ("Album");
|
var artist = button.GetTagByKey ("Artist");
|
if (album == null || artist == null) {
|
continue;
|
}
|
var filePath = Shared.IO.FileUtils.DownLoadImageFormBaidu (album.ToString (), artist.ToString ());
|
if (filePath != null) {
|
Application.RunOnMainThread (() => {
|
button.UnSelectedImagePath = filePath;
|
});
|
}
|
} catch {
|
|
}
|
}
|
});
|
}
|
|
public void pushList (MusicInfo musicInfo, string listname)//MEDIA_ITEM/MP3-0/ORIGINAL
|
{
|
//最大发送的条数
|
int maxCount = 50;
|
//找出当前点击音乐的索引
|
int statIndex = A31MusicModel.Current.CurrentPlayMusicInfoList.FindIndex ((obj) => { return obj == musicInfo; });
|
//点击后面还有多少条音乐
|
int count = A31MusicModel.Current.CurrentPlayMusicInfoList.Count - statIndex;
|
int endIndex = 0;
|
if (maxCount < count) {
|
//如果后面的音乐大于最大数,设置后面数据的最大索引
|
endIndex = statIndex + maxCount - 1;
|
} else {
|
//如果后面数据不多,就用总数减去最大条数,得到开始索引
|
statIndex = A31MusicModel.Current.CurrentPlayMusicInfoList.Count - maxCount - 1;
|
endIndex = A31MusicModel.Current.CurrentPlayMusicInfoList.Count - 1;
|
}
|
//如果开始索引小于0,那就设置为第一条音乐
|
if (statIndex < 0) {
|
statIndex = 0;
|
}
|
|
StringBuilder sb = new StringBuilder ();
|
sb.AppendLine ("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
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><?xml version=\"1.0\"?>");
|
sb.AppendLine ("<PlayList>");
|
sb.AppendLine ("<ListName>DLNA" + listname + "</ListName>");
|
sb.AppendLine ("<ListInfo>");
|
sb.AppendLine ("<Radio>0</Radio>");
|
sb.AppendLine ("<SourceName>UPnPServer</SourceName>");
|
sb.AppendLine ("<MarkSearch>0</MarkSearch>");
|
sb.AppendLine ("<TrackNumber>" + (endIndex - statIndex + 1) + "</TrackNumber>");
|
sb.AppendLine ("<Quality>0</Quality>");
|
sb.AppendLine ("<UpdateTime>5367</UpdateTime>");
|
sb.AppendLine ("<LastPlayIndex>2</LastPlayIndex>");
|
sb.AppendLine ("<SwitchPageMode>0</SwitchPageMode>");
|
sb.AppendLine ("<CurrentPage>0</CurrentPage>");
|
sb.AppendLine ("<TotalPages>0</TotalPages>");
|
sb.AppendLine ("</ListInfo>");
|
sb.AppendLine ("<Tracks>");
|
|
int number = 1;
|
for (int i = statIndex, trackNumber = 1; i <= endIndex; i++, trackNumber++) {
|
var tempMusicInfo = A31MusicModel.Current.CurrentPlayMusicInfoList [i];
|
|
if (musicInfo == tempMusicInfo) {
|
number = trackNumber;
|
}
|
|
sb.AppendLine ("<Track" + trackNumber + ">");
|
|
sb.AppendLine ("<URL>" + tempMusicInfo.URL + "</URL>");
|
sb.AppendLine ("<Metadata>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;");
|
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 id=&quot;0&quot;&gt;");
|
sb.AppendLine ("&lt;song:subid&gt;&lt;/song:subid&gt;");
|
sb.AppendLine ("&lt;song:description&gt;unknown&lt;/song:description&gt;");
|
sb.AppendLine ("&lt;song:skiplimit&gt;6&lt;/song:skiplimit&gt;");
|
sb.AppendLine ("&lt;song:id&gt;" + tempMusicInfo.ID + "&lt;/song:id&gt;");
|
sb.AppendLine ("&lt;song:like&gt;0&lt;/song:like&gt;");
|
sb.AppendLine ("&lt;song:singerid&gt;0&lt;/song:singerid&gt;");
|
sb.AppendLine ("&lt;song:albumid&gt;" + tempMusicInfo.AlbumId + "&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;" + "0:0:0.0" + "&quot;&gt;" + tempMusicInfo.URL + "&lt;/res&gt;");
|
|
sb.AppendLine ("&lt;dc:title&gt;" + tempMusicInfo.Title + "&lt;/dc:title&gt;");
|
sb.AppendLine ("&lt;dc:creator&gt;DJ Sanny J&lt;/dc:creator&gt;");
|
sb.AppendLine ("&lt;upnp:artist&gt;" + tempMusicInfo.Artist + "&lt;/upnp:artist&gt;");
|
sb.AppendLine ("&lt;upnp:album&gt;" + tempMusicInfo.Album + "&lt;/upnp:album&gt;");
|
sb.AppendLine ("&lt;upnp:albumArtURI&gt;unknown&lt;/upnp:albumArtURI&gt;");
|
sb.AppendLine ("&lt;/item&gt;");
|
sb.AppendLine ("&lt;/DIDL-Lite&gt;");
|
sb.AppendLine ("</Metadata>");
|
sb.AppendLine ("<Id>" + tempMusicInfo.ID + "</Id>");
|
sb.AppendLine ("<Source>UPnPServer</Source>");
|
sb.AppendLine ("<Key>" + tempMusicInfo.URL + "</Key>");
|
sb.AppendLine ("</Track" + trackNumber + ">");
|
}
|
sb.AppendLine ("</Tracks>");
|
sb.AppendLine ("</PlayList>");
|
sb.AppendLine ("</QueueContext>");
|
sb.AppendLine ("</u:CreateQueue>");
|
sb.AppendLine ("</s:Body>");
|
sb.AppendLine ("</s:Envelope>");
|
|
sendMusicLists (A31MusicModel.Current.IPAddress, A31MusicModel.Current.Port, "CreateQueue", sb.ToString ());
|
|
StringBuilder playString = new StringBuilder ();
|
playString.AppendLine ("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
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>DLNA" + listname + "</QueueName>");
|
playString.AppendLine ("<Index>" + number + "</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)
|
{
|
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
|
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)
|
{
|
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
|
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 {
|
}
|
}
|
|
string dlnalistsong (DLNAServer dlnaServer, string id)
|
{
|
|
string aa = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\"><ObjectID>" + id + "</ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter>*</Filter><StartingIndex>0</StartingIndex><RequestedCount>999</RequestedCount><SortCriteria>+dc:title</SortCriteria></u:Browse></s:Body></s:Envelope>";
|
|
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
|
webClient.Proxy = null;
|
webClient.Headers.Add ("Content-type", "text/xml;charset=\"utf-8\"");
|
webClient.Headers.Add ("Soapaction", "\"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"");
|
|
try {
|
byte [] recevieBytes = webClient.UploadData (new Uri ("http://" + dlnaServer.URL + dlnaServer.ControlURL), "POST", System.Text.Encoding.UTF8.GetBytes (aa));
|
return System.Text.Encoding.UTF8.GetString (recevieBytes, 0, recevieBytes.Length);
|
} catch (Exception e) {
|
string s = e.Message;
|
}
|
return null;
|
}
|
}
|
}
|