using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using Shared;
|
using Shared.SimpleControl;
|
using Shared.SimpleControl.Phone;
|
using System.Xml;
|
using Shared.SimpleControl.R;
|
using SmartHome.UI.SimpleControl.Phone.Music;
|
using System.Security;
|
using SmartHome;
|
|
namespace Shared.SimpleControl.Phone.Music
|
{
|
/// <summary>
|
/// 当前播放列表
|
/// </summary>
|
class A31PlayList : FrameLayout
|
{
|
VerticalScrolViewLayout verticalScrolViewLayout;
|
List<Button> buttonList = new List<Button> (100);
|
Button btnListName;
|
string listName;
|
public void InitFace ()
|
{
|
this.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,
|
};
|
this.AddChidren (topFrameLayout);
|
|
btnListName = new Button {
|
TextID = MyInternationalizationString.Musicplaylist,
|
TextColor=SkinStyle.Current.MusicTextColor,
|
};
|
topFrameLayout.AddChidren (btnListName);
|
|
var back = new Button {
|
Width = Application.GetRealWidth (72),
|
Height = Application.GetRealHeight (89),
|
X = Application.GetRealWidth (-1),
|
Gravity = Gravity.CenterVertical,
|
UnSelectedImagePath = "MusicIcon/PlayBack.png",
|
};
|
back.MouseDownEventHandler += (sender, e) => {
|
this.RemoveFromParent ();
|
};
|
topFrameLayout.AddChidren (back);
|
|
verticalScrolViewLayout = new VerticalScrolViewLayout {
|
Height = Application.GetRealHeight (Application.DesignHeight - 136),
|
Y = topFrameLayout.Bottom,
|
BackgroundColor =SkinStyle.Current.MusicVerticalScrolViewLayout,
|
};
|
AddChidren (verticalScrolViewLayout);
|
|
btnListName.Text = Language.StringByID (MyInternationalizationString.Musicplaylist);
|
}
|
|
public override void RemoveFromParent ()
|
{
|
base.RemoveFromParent ();
|
if (readPlayListThread != null && readPlayListThread.IsAlive) {
|
readPlayListThread.Abort ();
|
}
|
if (readImageThread != null && readImageThread.IsAlive) {
|
readImageThread.Abort ();
|
}
|
if (updateSelectedMusicThread != null && updateSelectedMusicThread.IsAlive) {
|
updateSelectedMusicThread.Abort ();
|
}
|
}
|
|
void addView ()
|
{
|
verticalScrolViewLayout.RemoveAll ();
|
buttonList.Clear ();
|
for (int i = 0; i < A31MusicModel.Current.CurrentPlayMusicInfoList.Count; i++) {
|
var musicInfo = A31MusicModel.Current.CurrentPlayMusicInfoList [i];
|
var rowsong = new RowLayout {
|
Height = Application.GetRealHeight (100),
|
LineColor = SkinStyle.Current.MusicRowLayoutLineColor,
|
};
|
verticalScrolViewLayout.AddChidren (rowsong);
|
|
var title = new Button {
|
Width = Application.GetRealWidth (61),
|
Height = Application.GetRealHeight (61),
|
X = Application.GetRealWidth (20),
|
Gravity = Gravity.CenterVertical,
|
Radius = (uint)Application.GetRealHeight (4),
|
UnSelectedImagePath = "MusicIcon/musicplay1.png",
|
};
|
rowsong.AddChidren (title);
|
|
var path = "MusicImage/AlbumArtistImage_";
|
musicInfo.Album = musicInfo.Album == null ?"": musicInfo.Album;
|
musicInfo.Artist = musicInfo.Artist == null ?"": musicInfo.Artist.Split ('/') [0] ;
|
|
if (Shared.IO.FileUtils.Exists (path + musicInfo.Album)) {
|
title.UnSelectedImagePath = path + musicInfo.Album;
|
} else if (Shared.IO.FileUtils.Exists (path + musicInfo.Artist)) {
|
title.UnSelectedImagePath = path + musicInfo.Artist;
|
}
|
else {
|
title.UnSelectedImagePath = "MusicIcon/musicplay1.png";
|
title.AddTag ("Album", musicInfo.Album);
|
title.AddTag ("Artist", musicInfo.Artist);
|
buttonList.Add (title);
|
}
|
|
|
var song = new Button {
|
Width = Application.GetRealWidth (400),
|
Text = musicInfo.Title,
|
Height = Application.GetRealHeight (50),
|
TextAlignment = TextAlignment.BottomLeft,
|
X = Application.GetRealWidth (100),
|
Tag = musicInfo,
|
TextColor = A31MusicModel.Current.A31PlayStatus.Title == musicInfo.Title ? 0xffFE5E00 : SkinStyle.Current.MusicTextColor,
|
};
|
rowsong.AddChidren (song);
|
|
|
var artist = new Button {
|
Width = Application.GetRealWidth (400),
|
Height = Application.GetRealHeight (50),
|
Text = musicInfo.Artist + "-" + musicInfo.Album,
|
TextAlignment = TextAlignment.TopLeft,
|
X = Application.GetRealWidth (100),
|
Y = song.Bottom,
|
Tag = musicInfo,
|
//TextColor = 0x75ffffff,
|
TextColor = SkinStyle.Current.MusicArtistTextColor,
|
};
|
rowsong.AddChidren (artist);
|
|
EventHandler<MouseEventArgs> musics = (sender, e) => {
|
rowsong.BackgroundColor = 0xffFE5E00;
|
System.Threading.Tasks.Task.Run (() => {
|
System.Threading.Thread.Sleep (50);
|
Application.RunOnMainThread (() => {
|
this.RemoveFromParent ();
|
A31MusicModel.Current.A31PlayStatus.Title = song.Text;
|
System.Threading.Tasks.Task.Run (() => {
|
listplay (listName, song.Tag as MusicInfo);
|
//if (A31MusicModel.Current.A31PlayStatus.playSource == "Tidal") {
|
// A31NewPage.tidalpushList (song.Tag as MusicInfo, A31MusicModel.Current.CurrentPlayMusicInfoList);
|
//} else {
|
// listplay (listName, song.Tag as MusicInfo);
|
//}
|
});
|
});
|
});
|
};
|
song.MouseUpEventHandler += musics;
|
artist.MouseUpEventHandler += musics;
|
title.MouseUpEventHandler += musics;
|
|
|
var del = new Button {
|
TextID = MyInternationalizationString.Musicdel,
|
BackgroundColor = 0xFFFF0000,
|
Tag = A31MusicModel.Current.CurrentPlayMusicInfoList [i].ID,
|
};
|
//rowsong.AddRightView(del);
|
del.MouseUpEventHandler += (sender, e) => {
|
for (int j = 0; j < A31MusicModel.Current.CurrentPlayMusicInfoList.Count; j++) {
|
if (A31MusicModel.Current.CurrentPlayMusicInfoList [j].ID == del.Tag.ToString ()) {
|
A31MusicModel.Current.CurrentPlayMusicInfoList.RemoveAt (j);
|
break;
|
}
|
}
|
//这里需要更新一下要发送的列表信息
|
rowsong.RemoveFromParent ();
|
};
|
}
|
}
|
|
public void Musiclist (string listName)
|
{
|
this.listName = listName;
|
addView ();
|
updateSelectedMusic ();
|
readImage ();
|
readPlayList ();
|
}
|
|
List<MusicInfo> playList ()
|
{
|
var list = new List<MusicInfo> ();
|
try {
|
var playString =CurrentPlayList ();
|
var se = System.Security.SecurityElement.FromString (playString);
|
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").Replace ("&", "&amp;");
|
var metadata = track.SearchForTextOfTag ("Metadata");
|
musicInfo.SourceName = track.SearchForTextOfTag ("Source");
|
if (string.IsNullOrEmpty (metadata)) {
|
continue;
|
}
|
if (A31MusicModel.IsJson (metadata)) {
|
var qqSong = Newtonsoft.Json.JsonConvert.DeserializeObject<A31QQSong> (metadata);
|
musicInfo.Album = qqSong.album;
|
musicInfo.Title = qqSong.title;
|
musicInfo.Artist = qqSong.creator;
|
} else {
|
metadata = metadata.Replace ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").Replace ("&", "&amp;");
|
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");
|
}
|
list.Add (musicInfo);
|
}
|
} catch { }
|
return list;
|
}
|
|
/// <summary>
|
/// 定时更新当前播放音乐
|
/// </summary>
|
System.Threading.Thread updateSelectedMusicThread;
|
void updateSelectedMusic ()
|
{
|
updateSelectedMusicThread = new System.Threading.Thread (() => {
|
while (this.Parent != null) {
|
System.Threading.Thread.Sleep (1000);
|
Shared.Application.RunOnMainThread (() => {
|
try {
|
for (int i = 0; i < verticalScrolViewLayout.ChildrenCount; i++) {
|
RowLayout view = (RowLayout)verticalScrolViewLayout.GetChildren (i);
|
var songName = (Button)view.GetChildren (1);
|
if (A31MusicModel.Current.A31PlayStatus.Title == songName.Text) {
|
songName.TextColor = 0xffFE5E00;
|
} else {
|
songName.TextColor = SkinStyle.Current.MusicTextColor;
|
}
|
}
|
} catch { }
|
|
});
|
}
|
});
|
updateSelectedMusicThread.Start ();
|
}
|
/// <summary>
|
/// 定时读取图片线程
|
/// </summary>
|
System.Threading.Thread readImageThread;
|
void readImage ()
|
{
|
readImageThread = new System.Threading.Thread (() => {
|
|
for (int i = 0; i < buttonList.Count;i++) {
|
var button = buttonList [i];
|
var album = button.GetTagByKey ("Album");
|
var artist = button.GetTagByKey ("Artist");
|
if (album.ToString()=="" && artist.ToString()=="") {
|
continue;
|
}
|
|
var filePath = Shared.IO.FileUtils.DownLoadImageFormBaidu (album.ToString (), artist.ToString ());
|
if (filePath != null) {
|
Application.RunOnMainThread (() => {
|
button.UnSelectedImagePath = filePath;
|
});
|
}
|
}
|
});
|
readImageThread.Start ();
|
}
|
/// <summary>
|
/// 定时读取Tida音乐列表
|
/// </summary>
|
System.Threading.Thread readPlayListThread;
|
void readPlayList ()
|
{
|
if (A31MusicModel.Current.A31PlayStatus.playSource == "Tidal") {
|
readPlayListThread = new System.Threading.Thread (() => {
|
while (true) {
|
System.Threading.Thread.Sleep (1000);
|
var list = playList ();
|
if (list.Count <= A31MusicModel.Current.CurrentPlayMusicInfoList.Count) {
|
continue;
|
}
|
A31MusicModel.Current.CurrentPlayMusicInfoList = list;
|
Application.RunOnMainThread (() => {
|
addView ();
|
});
|
}
|
});
|
readPlayListThread.Start ();
|
}
|
}
|
|
public void pushList (MusicInfo musicInfo, string listName, string sourceName)
|
{
|
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>");
|
sb.AppendLine ("<PlayList>");
|
sb.AppendLine ("<ListName>" + listName + "</ListName>");
|
sb.AppendLine ("<ListInfo>");
|
sb.AppendLine ("<SourceName>" + sourceName + "</SourceName>");
|
sb.AppendLine ("<MarkSearch>0</MarkSearch>");
|
sb.AppendLine ("<TrackNumber>" + A31MusicModel.Current.CurrentPlayMusicInfoList.Count + "</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;
|
int i = 0;
|
foreach (var tempMusicInfo in A31MusicModel.Current.CurrentPlayMusicInfoList) {
|
|
i++;
|
if (musicInfo == tempMusicInfo) {
|
number = i;
|
}
|
|
sb.AppendLine ("<Track" + i + ">");
|
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;" + tempMusicInfo.Duration + "&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 ("<Source>" + tempMusicInfo.SourceName + "</Source>");
|
sb.AppendLine ("</Track" + i + ">");
|
|
}
|
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>" + 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 { }
|
}
|
|
void listplay (string listName, MusicInfo musicInfo)
|
{
|
StringBuilder playstrings = new StringBuilder ();
|
int number = 1;
|
int i = 0;
|
foreach (var tempMusicInfo in A31MusicModel.Current.CurrentPlayMusicInfoList) {
|
i++;
|
if (musicInfo == tempMusicInfo) {
|
number = i;
|
}
|
}
|
playstrings.AppendLine ("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");
|
playstrings.AppendLine ("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
|
playstrings.AppendLine ("<s:Body>");
|
playstrings.AppendLine ("<u:PlayQueueWithIndex xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
|
playstrings.AppendLine ("<QueueName>" + listName + "</QueueName>");
|
playstrings.AppendLine ("<Index>" + number + "</Index>");
|
playstrings.AppendLine ("</u:PlayQueueWithIndex>");
|
playstrings.AppendLine ("</s:Body>");
|
playstrings.AppendLine ("</s:Envelope>");
|
|
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://" + 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 { }
|
}
|
|
|
/// <summary>
|
/// 获取当前播放的列表
|
/// </summary>
|
string CurrentPlayList ()
|
{
|
StringBuilder getPlayList = new StringBuilder ();
|
getPlayList.AppendLine ("<?xml version=\"1.0\"encoding=\"utf-8\"?>");
|
getPlayList.AppendLine ("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
|
getPlayList.AppendLine ("<s:Body>");
|
getPlayList.AppendLine ("<u:BrowseQueue xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
|
getPlayList.AppendLine ("<QueueName>CurrentQueue</QueueName>");
|
getPlayList.AppendLine ("</u:BrowseQueue>");
|
getPlayList.AppendLine ("</s:Body>");
|
getPlayList.AppendLine ("</s:Envelope>");
|
|
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
|
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;
|
}
|
|
}
|
}
|