hao0089 发表于 2015-5-9 08:25:26

百度Mp3的API在windows phone 7中的使用

  最近重新写了一个音乐的播放器。其中用到了百度音乐的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);
            }
      }
    }
}
  至于为什么用StreamReader读取Stream后,再用xmlreader读取string就可以。。。。求牛人解答。
  
  刚刚在做的时候发现,百度的API歌曲名和歌手名的首字母用大写和小写得到的XML居然不一样。。。。。。无语!
页: [1]
查看完整版本: 百度Mp3的API在windows phone 7中的使用