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
{
///
/// 当前播放列表
///
class A31PlayList : FrameLayout
{
VerticalScrolViewLayout verticalScrolViewLayout;
List buttonList = new List (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 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 playList ()
{
var list = new List ();
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 ("&", "&");
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 (metadata);
musicInfo.Album = qqSong.album;
musicInfo.Title = qqSong.title;
musicInfo.Artist = qqSong.creator;
} else {
metadata = metadata.Replace ("", "").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");
}
list.Add (musicInfo);
}
} catch { }
return list;
}
///
/// 定时更新当前播放音乐
///
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 ();
}
///
/// 定时读取图片线程
///
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 ();
}
///
/// 定时读取Tida音乐列表
///
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 ("");
sb.AppendLine ("");
sb.AppendLine ("");
sb.AppendLine ("");
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><?xml version="1.0" encoding="UTF-8"?>");
sb.AppendLine ("<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:song="www.wiimu.com/song/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">");
sb.AppendLine ("<upnp:class>object.item.audioItem.musicTrack</upnp:class>");
sb.AppendLine ("<item id="0">");
sb.AppendLine ("<song:subid></song:subid>");
sb.AppendLine ("<song:description>unknown</song:description>");
sb.AppendLine ("<song:skiplimit>6</song:skiplimit>");
sb.AppendLine ("<song:id>" + tempMusicInfo.ID + "</song:id>");
sb.AppendLine ("<song:like>0</song:like>");
sb.AppendLine ("<song:singerid>0</song:singerid>");
sb.AppendLine ("<song:albumid>" + tempMusicInfo.AlbumId + "</song:albumid>");
sb.AppendLine ("<res protocolInfo="http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;" duration="" + tempMusicInfo.Duration + "">" + tempMusicInfo.URL + "</res>");
sb.AppendLine ("<dc:title>" + tempMusicInfo.Title + "</dc:title>");
sb.AppendLine ("<dc:creator>DJ Sanny J</dc:creator>");
sb.AppendLine ("<upnp:artist>" + tempMusicInfo.Artist + "</upnp:artist>");
sb.AppendLine ("<upnp:album>" + tempMusicInfo.Album + "</upnp:album>");
sb.AppendLine ("<upnp:albumArtURI>unknown</upnp:albumArtURI>");
sb.AppendLine ("</item>");
sb.AppendLine ("</DIDL-Lite>");
sb.AppendLine ("</Metadata>");
sb.AppendLine ("<Source>" + tempMusicInfo.SourceName + "</Source>");
sb.AppendLine ("</Track" + i + ">");
}
sb.AppendLine ("</Tracks>");
sb.AppendLine ("</PlayList>");
sb.AppendLine (" ");
sb.AppendLine (" ");
sb.AppendLine (" ");
sb.AppendLine (" ");
sendMusicLists (A31MusicModel.Current.IPAddress, A31MusicModel.Current.Port, "CreateQueue", sb.ToString ());
StringBuilder playString = new StringBuilder ();
playString.AppendLine ("");
playString.AppendLine ("");
playString.AppendLine ("");
playString.AppendLine ("");
playString.AppendLine ("" + listName + " ");
playString.AppendLine ("" + number + " ");
playString.AppendLine (" ");
playString.AppendLine (" ");
playString.AppendLine (" ");
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 ("");
playstrings.AppendLine ("");
playstrings.AppendLine ("");
playstrings.AppendLine ("");
playstrings.AppendLine ("" + listName + " ");
playstrings.AppendLine ("" + number + " ");
playstrings.AppendLine (" ");
playstrings.AppendLine (" ");
playstrings.AppendLine (" ");
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 { }
}
///
/// 获取当前播放的列表
///
string CurrentPlayList ()
{
StringBuilder getPlayList = new StringBuilder ();
getPlayList.AppendLine ("");
getPlayList.AppendLine ("");
getPlayList.AppendLine ("");
getPlayList.AppendLine ("");
getPlayList.AppendLine ("CurrentQueue ");
getPlayList.AppendLine (" ");
getPlayList.AppendLine (" ");
getPlayList.AppendLine (" ");
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;
}
}
}