using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using Shared.SimpleControl;
using Shared.SimpleControl.Phone;
using Shared.SimpleControl.Phone.Music;
using Shared.SimpleControl.R;
namespace SmartHome.UI.SimpleControl.Phone.Music
{
class A31RadioList : FrameLayout
{
///
/// 判断是否可以直接播放
///
///
///
bool isRadios (object [] ob)
{
bool result = false;
foreach (object o in ob) {
if (o.ToString () == "radio") {
result = true;
break;
} else if (o.ToString () == "feed.episode") {
result = true;
break;
} else if (o.ToString () == "track") {
result = true;
break;
}
}
return result;
}
public void show (A31MusicModel a31, Airable airable, string currname)
{
AddChidren (new Button {
Height = Application.GetRealHeight (36),
BackgroundColor = SkinStyle.Current.MainColor,
});
var topFrameLayout = new FrameLayout {
Height = Application.GetRealHeight (90),
Y = Application.GetRealHeight (36),
BackgroundColor = SkinStyle.Current.MainColor,
};
AddChidren (topFrameLayout);
var LocallistName = new Button {
Text = currname,
};
topFrameLayout.AddChidren (LocallistName);
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 ();
};
var middle = new VerticalScrolViewLayout ();
middle.Y = topFrameLayout.Bottom;
middle.Height = Application.GetRealHeight (Application.DesignHeight - 126);
middle.BackgroundColor = 0xff2F2F2F;
AddChidren (middle);
if (airable == null) {
return;
}
if (airable.content == null) {
return;
}
if (airable.content.entries == null) {
return;
}
int munber = 0;
a31.CnRadioInfoList = new List ();
foreach (var entrie in airable.content.entries) {
if (isRadios (entrie.id)) {
a31.CnRadioInfoList.Add (new MusicInfo { URL = entrie.url, Title = entrie.title });
var rowsong = new RowLayout {
Height = Application.GetRealHeight (100),
};
middle.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 radioname = new Button {
Width = Application.GetRealWidth (400),
Text = entrie.title,
TextAlignment = TextAlignment.CenterLeft,
X = Application.GetRealWidth (130),
Tag = munber++,
};
rowsong.AddChidren (radioname);
radioname.MouseUpEventHandler += (sender, e) => {
rowsong.BackgroundColor = 0xffFE5E00;
System.Threading.Tasks.Task.Run (() => {
System.Threading.Thread.Sleep (10);
Application.RunOnMainThread (() => {
for (int i = MainPage.MainFrameLayout.ChildrenCount - 1; 0 <= i; i--) {
var view = MainPage.MainFrameLayout.GetChildren (i);
if (view.GetType () == typeof (A31RadioList)) {
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);
A31PlayMusicPage a31playmusic = new A31PlayMusicPage ();
MainPage.MainFrameLayout.AddChidren (a31playmusic);
a31playmusic.Show (new List ());
//a31playmusic.Source = "RADIO-NETWORK";
a31.A31PlayStatus.Title = radioname.Text;
System.Threading.Tasks.Task.Run (() => {
pushList (a31, radioname.Tag, currname);
});
});
});
};
} else {
//目录列表
if (entrie.title != "搜索" || entrie.title != "Suche") {
var rowtrue = new RowLayout {
Height = Application.GetRealHeight (100)
};
middle.AddChidren (rowtrue);
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,
};
rowtrue.AddChidren (radius);
var radioname = new Button {
Width = Application.GetRealWidth (440),
Gravity = Gravity.CenterVertical,
Text = entrie.title,
X = Application.GetRealWidth (80),
TextAlignment = TextAlignment.CenterLeft,
Tag = entrie.url,
};
rowtrue.AddChidren (radioname);
var radionext = new Button {
Width = Application.GetRealWidth (87),
Height = Application.GetRealHeight (100),
UnSelectedImagePath = "MusicIcon/Next.png",
SelectedImagePath = "MusicIcon/NextSelecte.png",
X = Application.GetRealWidth (550),
};
rowtrue.AddChidren (radionext);
EventHandler useshare = (sender, e) => {
var radioConfig = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (RadioConfig.FileName)));
MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load));
System.Threading.Tasks.Task.Run (() => {
Airable airablelist = null;
try {
airablelist = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, radioname.Tag.ToString ());
if (airablelist == null) {
airablelist = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, radioname.Tag.ToString ());
}
} catch { } finally {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
A31RadioList a31RadioList = new A31RadioList ();
MainPage.MainFrameLayout.AddChidren (a31RadioList);
a31RadioList.show (a31, airablelist, radioname.Text);
});
}
});
};
radius.MouseUpEventHandler += useshare;
radioname.MouseUpEventHandler += useshare;
radionext.MouseUpEventHandler += useshare;
}
#region .......搜索
////.......搜索
//else
//{
// var searchrowtrue = new RowLayout
// {
// Height = Application.GetRealHeight(100)
// };
// middle.AddChidren(searchrowtrue);
// 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,
// };
// searchrowtrue.AddChidren(radius);
// var search = new Button
// {
// Gravity = Gravity.CenterVertical,
// Text = entrie.title,
// X = Application.GetRealWidth(80),
// TextAlignment = TextAlignment.CenterLeft,
// Tag = entrie.url,
// };
// searchrowtrue.AddChidren(search);
// var searchnext = new Button
// {
// Width = Application.GetRealWidth(87),
// Height = Application.GetRealHeight(100),
// UnSelectedImagePath = "MusicIcon/Next.png",
// SelectedImagePath = "MusicIcon/NextSelecte.png",
// X = Application.GetRealWidth(550),
// };
// searchrowtrue.AddChidren(searchnext);
// EventHandler searchs = (sender, e) =>
// {
// A31Search A31search = new A31Search();
// MainPage.MainFrameLayout.AddChidren(A31search);
// A31search.Show(a31);
// };
// radius.MouseUpEventHandler += searchs;
// search.MouseUpEventHandler += searchs;
// searchnext.MouseUpEventHandler += searchs;
//}
#endregion
}
}
}
public static string GetPlayUrl (string url)
{
System.DateTime dateTime = DateTime.Now;
var radioConfig = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (RadioConfig.FileName)));
var airable = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, url);
if (airable != null && airable.streams != null) {
airable = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, airable.streams [0].url);
if (airable == null) {
airable = A31CommonLogin.ReadRadiohome (radioConfig.UserName, radioConfig.Passwrod, airable.streams [0].url);
}
}
if (airable == null) {
return "";
} else {
return airable.url;
}
}
public void pushList (A31MusicModel a31, object tag, string listName)
{
StringBuilder sb = new StringBuilder ();
sb.AppendLine ("");
sb.AppendLine ("");
sb.AppendLine ("");
sb.AppendLine ("");
sb.AppendLine ("");
sb.AppendLine ("<?xml version="1.0" ?>");
sb.AppendLine ("<PlayList>");
sb.AppendLine ("<ListName>" + listName + "</ListName>");
sb.AppendLine ("<ListInfo>");
sb.AppendLine ("<SourceName></SourceName>");
sb.AppendLine ("<TrackNumber>1</TrackNumber>");
sb.AppendLine ("<SearchUrl>searchurl</SearchUrl>");
sb.AppendLine ("</ListInfo>");
sb.AppendLine ("<Tracks>");
var tempMusicInfo = a31.CnRadioInfoList [int.Parse (tag.ToString ())];
string url = GetPlayUrl (tempMusicInfo.URL);
sb.AppendLine ("<Track" + 1 + ">");
sb.AppendLine ("<URL>" + url + "</URL>");
sb.AppendLine ("<Source></Source>");
sb.AppendLine ("<Metadata>");
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>");
sb.AppendLine ("<song:bitrate>0</song:bitrate>");
sb.AppendLine ("<song:id>0</song:id>");
sb.AppendLine ("<song:singerid>0</song:singerid>");
sb.AppendLine ("<song:albumid>0</song:albumid>");
sb.AppendLine ("<res protocolInfo="http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;" duration="1000">" + url + "</res>");
sb.AppendLine ("<dc:title>" + tempMusicInfo.Title + "</dc:title>");
sb.AppendLine ("<upnp:artist>TuneIn</upnp:artist>");
sb.AppendLine ("<upnp:album></upnp:album>");
sb.AppendLine ("<upnp:albumArtURI>" + tempMusicInfo.Image + "</upnp:albumArtURI>");
sb.AppendLine ("</item>");
sb.AppendLine ("</DIDL-Lite>");
sb.AppendLine ("</Metadata>");
sb.AppendLine ("</Track" + 1 + ">");
sb.AppendLine ("</Tracks>");
sb.AppendLine ("</PlayList>");
sb.AppendLine ("");
sb.AppendLine ("");
sb.AppendLine ("");
sb.AppendLine ("");
//sendMusicLists(a31.IPAddress, a31.Port, "CreateQueue", sb.ToString());
sendMusicLists (a31.IPAddress, a31.Port, "CreateQueue", sb.ToString ());
StringBuilder playString = new StringBuilder ();
playString.AppendLine ("");
playString.AppendLine ("");
playString.AppendLine ("");
playString.AppendLine ("");
playString.AppendLine ("" + listName + "");
playString.AppendLine ("" + 1 + "");
playString.AppendLine ("");
playString.AppendLine ("");
playString.AppendLine ("");
play (a31.IPAddress, a31.Port, playString.ToString ());
}
void sendMusicLists (string ip, int port, string soapAction, string listInfo)
{
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient (5000);
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 (5000);
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 { }
}
}
}