百度Mp3的API在windows phone 7中的使用
来源:互联网 发布:淘宝旺铺专业版怎么用 编辑:程序博客网 时间:2024/05/29 18:31
最近重新写了一个音乐的播放器。其中用到了百度音乐的API:http://cloud21.iteye.com/blog/611914
由于百度MP3的API的地址和我们请求的到得XML文件都是GB2312编码的。所以遇到了点麻烦。好了废话不说,上代码。
namespace BaiduAPI_test
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
public class MusicUpdateState
{
public HttpWebRequest AsyncRequest { get; set; }
public HttpWebResponse AsyncResponse { get; set; }
}
private void btn_search_Click(object sender, RoutedEventArgs e)
{
//string gb_songname = utf_8_to_gb2312(SongName.Text);
//string gb_singername = utf_8_to_gb2312(SingerName.Text);
//由于百度的地址是用GB2312编码的,所以要用HttpUtility.UrlEncode这个函数来编码
string songname = HttpUtility.UrlEncode(SongName.Text);
string singername = HttpUtility.UrlEncode(SingerName.Text);
string Uri = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=" + songname + "$$" + singername + "$$$$";
UriBuilder fullUri = new UriBuilder(Uri);
HttpWebRequest musicRequest = (HttpWebRequest)WebRequest.Create(fullUri.Uri);
MusicUpdateState musicState = new MusicUpdateState();
musicState.AsyncRequest = musicRequest;
musicRequest.BeginGetResponse(new AsyncCallback(HandleMusicResponse), musicState);
}
private void HandleMusicResponse(IAsyncResult asyncResult)
{
MusicUpdateState musicState = (MusicUpdateState)asyncResult.AsyncState;
HttpWebRequest musicRequest = (HttpWebRequest)musicState.AsyncRequest;
musicState.AsyncResponse = (HttpWebResponse)musicRequest.EndGetResponse(asyncResult);
Stream streamResult;
string encode = "";
string decode = "";
streamResult = musicState.AsyncResponse.GetResponseStream();
//这里也是一样的,请求得到的XML是用GB2312编码的,直接用XmlReader读取Stream的话会提示系统不支持GB2312编码
var sr = new StreamReader(streamResult);
string srResult = sr.ReadToEnd();
//----Test----------
//Dispatcher.BeginInvoke(() =>
// {
// tb_decode.Text = srResult;
// });
using (XmlReader reader = XmlReader.Create(new StringReader(srResult)))
{
reader.ReadToFollowing("encode");
encode = reader.ReadElementContentAsString();
//百度地址中间的数字是7位或者8位的,所以要判断最后一个是不是数字
string encodeUrl = encode.Substring(44,8);
string lastNumberUri = IsNumber(encodeUrl);
reader.ReadToFollowing("decode");
decode = reader.ReadElementContentAsString();
UriBuilder fullUri = new UriBuilder("http://zhangmenshiting.baidu.com/data/music/" + lastNumberUri + "/" + decode);
Dispatcher.BeginInvoke(() =>
{
BaiduMusicElement.Source = fullUri.Uri;
BaiduMusicElement.AutoPlay = true;
BaiduMusicElement.Position = TimeSpan.FromMilliseconds(0);
BaiduMusicElement.Play();
});
}
}
private string IsNumber(string str)
{//判断str的最后一个字符是否数字
int result;
string lastOne = str.Substring(7);
if (int.TryParse(lastOne, out result))
{
return str;
}
else
{
return str.Substring(0, 7);
}
}
}
}
- 百度Mp3的API在windows phone 7中的使用
- 百度MP3 API
- [Windows Phone] StackPanel和Grid在布局中的使用
- 通过Java获取百度音乐API中的MP3链接
- Windows Phone 7下ListBox的使用
- Windows Phone 7 图表控件的使用
- 百度mp3接口(baidu mp3 api)
- windows phone:在Windows Phone上使用QQConnect OAuth2
- #Windows Phone:如何在Windows Phone App使用HTML5專案開發
- Windows Phone 学习 LINQ在wp上的使用
- 进击的socket(windows phone socket api)
- 在Windows Phone 7上使用Silverlight Toolkit Chart
- Windows Phone 7(WP7)开发 在ViewModel中使用NavigationService
- Windows Phone 7(WP7)开发 在ViewModel中使用NavigationService
- 在Windows Phone 7上使用Silverlight Toolkit Chart时遇到的错误
- 百度API的使用
- 百度api的使用
- windows phone FluidMoveBehavior的使用
- 02_JDBC编程
- sqlite数据库文件拷贝出错
- js生成web安全色
- Windows Phone 7 中将Gb2312编码转换成UTF-8
- The Hadoop Distributed File System
- 百度Mp3的API在windows phone 7中的使用
- 用Maven建立一个web project
- 木马编写小技巧(转)
- 指针的底层实现
- Ubuntu下Eclipse CDT 代码悬浮提示窗口背景黑色设置方法
- 关于sqlite的特殊字符转义
- JVM调优总结
- DataGrid Control, EditText Control
- 服务器 同一时刻最大在线人数