using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Shared;
namespace HDL_ON.UI.Music
{
public class A31LoveRadioList : FrameLayout
{
public A31LoveRadioList()
{
Tag = "Music";
}
VerticalRefreshLayout middViewLayout;
///
/// 先加载界面出来
///
public void Show()
{
#region 界面布局------
this.BackgroundColor = MusicColor.ViewColor;
var topView = new TopView();
this.AddChidren(topView.TopFLayoutView());
topView.topNameBtn.TextID = StringId.radioLove;
topView.clickBackBtn.MouseUpEventHandler += (sender, e) =>
{
if (updateSelectedMusicThread != null)
{
try
{
if (updateSelectedMusicThread.IsAlive)
{
updateSelectedMusicThread.Abort();
///关闭线程
}
}
catch { }
}
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(Language.StringByID(StringId.radioLove));
}
///
/// 加载数据的方法
///
public void MusicView(string listName)
{
middViewLayout.RemoveAll();
for (int i = 0; i < A31MusicModel.Current.LoveRadioInfoList.Count; i++)
{
var radioInfo = A31MusicModel.Current.LoveRadioInfoList[i];
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/loveSelected.png",
};
radioViewFl.AddChidren(loveIcon);
loveIcon.MouseUpEventHandler += (sender, e) =>
{
var radioselected = A31MusicModel.Current.LoveRadioInfoList.Find((m) => m.URL == radioInfo.URL);
if (radioselected != null)
{
///存在就删除
A31MusicModel.Current.LoveRadioInfoList.Remove(radioInfo);
}
A31MusicModel.Save();
radioViewFl.RemoveFromParent();
};
//点击控件
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(() =>
{
CnpushList(clickBtn.Tag as MusicInfo, listName);
});
});
});
};
}
}
///
/// 定时更新当前播放音乐
///
System.Threading.Thread updateSelectedMusicThread;
public void UpdateSelectedMusic()
{
updateSelectedMusicThread = new System.Threading.Thread(() =>
{
while (this.Parent != null)
{
System.Threading.Thread.Sleep(1000);
Application.RunOnMainThread(() =>
{
try
{
for (int i = 0; i < middViewLayout.ChildrenCount; i++)
{
FrameLayout view = (FrameLayout)middViewLayout.GetChildren(i);
var songNameBtn = (Button)view.GetChildren(1);//直接FrameLayout父控件找到该控件Button
if (A31MusicModel.Current.A31PlayStatus.Title == songNameBtn.Text.ToString())
{
songNameBtn.TextColor = MusicColor.SelectedColor;
}
else
{
songNameBtn.TextColor = MusicColor.MusicTxet14Color;
}
}
}
catch { }
});
}
});
updateSelectedMusicThread.Start();
}
///
///收藏列表电台播放
///
///
public void CnpushList(MusicInfo musicInfo, 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("<Radio>1</Radio>");
sb.AppendLine("<SourceName>TuneIn</SourceName>");
sb.AppendLine("<TrackNumber>1</TrackNumber>");
sb.AppendLine("<SearchUrl>http://opml.radiotime.com/Tune.ashx?id=s112201&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 = musicInfo.URL;
if (url != null)
{
url = url.TrimEnd('\n').Replace("&", "&");
}
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&size=50&page=1</Key>");
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>" + musicInfo.Title + "</dc:title>");
sb.AppendLine("<upnp:artist>TuneIn</upnp:artist>");
sb.AppendLine("<upnp:album></upnp:album>");
sb.AppendLine("<upnp:albumArtURI>" + musicInfo.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(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("" + 1 + "");
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)
{
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
{
}
}
}
}