微信服務開發——讀取百度音樂介面返迴音樂,百度音樂

來源:互聯網
上載者:User

服務開發——讀取百度音樂介面返迴音樂,百度音樂



            
           最近用了很多網上的坑爹介面,都淚奔了,不說了,都是淚啊~


            前兩天做了個返迴音樂的介面,先來看下效果:



            


            出來之後在手機裡面就可以實現音樂播放了。


            技術很簡單,首先訪問百度音樂介面,傳入音樂名稱和歌手名稱,然後百度會返回一個XML格式給你,裡麵包括音樂的連結地址等資訊。但是比較坑爹的是,這裡的連結地址是寫在兩個node裡面的,我要先從一個encode裡面拿到前半段,然後再從deconde裡面拿到後半段,然後拼起來,拿到URL之後,就要把這個資訊放到返回給使用者的音樂資訊裡面。


          先來看下裡面返回給使用者的音樂訊息的XML的格式:


         



          其中,MusicUrl跟HQMusicUrl都寫成從百度拿到的音樂地址就ok了。對於一般公眾號,縮圖是不需要的。

           其實返回給使用者的資訊是比較坑爹的,除了音樂連結地址是真的,音樂名稱和歌手都是根據使用者輸入的東西顯示的。

           當@歌手的時候,上述XML的描述裡面寫的是就是被@的歌手。


           下面是具體代碼:


            先來看看調用百度音樂介面返回的XML裡是什麼


         如下,紅字的部分,為歌曲名稱和歌手名稱:


            http://box.zhangmen.baidu.com/x?op=12&count=1&title=東風破$$周杰倫$$$$


          調用完成之後,返回XML為:


          

<result><count>1</count><url><encode>http://zhangmenshiting.baidu.com/data2/music/120892918/YmRjbG5ocGlpYKCkaHWvm6CWmHFub5qbaJeWlWponG9lmGVkmWppcGZiaGpomphrYWZqlmhuam9la2uWbG6acWpklJZbo6CcbmJhbGxubm5ha2xka2dpcGcy</encode><decode>120892918.mp3?xcode=99cc7eca52e74f20d42850563da3047b3837498b78c992ab&mid=0.87876099061286</decode><type>8</type><lrcid>29026</lrcid><flag>1</flag></url><durl><encode>http://zhangmenshiting2.baidu.com/data2/music/120892919/YmRjbG5ocGlqYKCkaHWvm6CWmHFub5qbaJeWlWponG9lmGVkmWppcGZiaGpomphrYWZqlm6bb5xhlWiXZ2ybbJJkY5Zbo6CcbmJhbGxubm5ha2xka2dpcGcy</encode><decode>120892919.mp3?xcode=99cc7eca52e74f20d42850563da3047b9e8d0c5c26d4a20b&mid=0.87876099061286</decode><type>8</type><lrcid>29026</lrcid><flag>1</flag></durl><p2p><hash>acf1ed82375d608f50d630248cf3d04a72ec6699</hash><url></url><type>mp3</type><size>7674555</size><bitrate>192</bitrate></p2p></result>

           之後,拿到拼接地址:http://zhangmenshiting.baidu.com/data2/music/120892918/120892918.mp3?xcode=99cc7eca52e74f20d42850563da3047b3837498b78c992ab

           注意,這裡要去掉:&mid=0.87876099061286

          

            代碼如下:(c#版):

 

 private string strSonger = null; //歌手        private string strSongName = null; //歌曲名稱        /// <summary>        /// 擷取音樂        /// </summary>        /// <param name="strType">服務類型</param>        /// <param name="strOpenId">使用者OpenId</param>        /// <param name="strWxAccount">公眾號</param>        /// <param name="strParams">傳入參數:查詢歌曲所用</param>        /// <param name="strXML">返回的XML</param>        public void GetMusic(string strType, string strOpenId, string strWxAccount, string strParams, out string strXML)        {            strXML = null;            try            {                ComponentServiceMessge comServiceMessage = ComponentServiceMessgeAdapter.Instance.GetUrlByComType(strType);                if (comServiceMessage == null)  //未找到服務                {                    return;                }                else //找到服務                {                    //發給使用者                    strXML = this.ReturnWxMusic(strOpenId, strWxAccount, this.GetMusicFromBaiDu(comServiceMessage.ComponentURL, strParams));                }            }            catch            {                strXML = null;            }        }        /// <summary>        /// 從百度介面拿到音樂資訊        /// </summary>        /// <param name="strParams">傳入的參數</param>        /// <returns></returns>        public string GetMusicFromBaiDu(string strUrl, string strParams)        {            string strMusicXml = null;            //拆參數            int posOne = strParams.IndexOf("@");            if (posOne >= 0)  //有歌手            {                strSongName = strParams.Substring(0, posOne);//歌曲名稱                strSonger = strParams.Substring(posOne + 1, strParams.Length - posOne - 1);//歌手                strUrl = strUrl + strSongName + "$$" + strSonger + "$$$$";                strMusicXml = base.RequestMyWebClient(EnumSubmitMethod.Get, "", strUrl);            }            else//只有歌曲的名稱            {                strSongName = strParams;                strUrl = strUrl + strParams + "$$";                strMusicXml = base.RequestMyWebClient(EnumSubmitMethod.Get, "", strUrl);            }            return strMusicXml;        }        /// <summary>        /// 得到發給的XML        /// </summary>        /// <param name="strXml">傳入從百度拿到的XML</param>        /// <returns></returns>        private string ReturnWxMusic(string strOpenId, string strWxAccount, string strXml)        {           string strWxMusicXml = null;            //拼入,返回給使用者            strWxMusicXml = string.Format(@"<xml>                        <ToUserName><![CDATA[{0}]]></ToUserName>                        <FromUserName><![CDATA[{1}]]></FromUserName>                        <CreateTime>{2}</CreateTime>                        <MsgType><![CDATA[music]]></MsgType>                        <Music>                        <Title><![CDATA[{3}]]></Title>                        <Description><![CDATA[{4}]]></Description>                        <MusicUrl><![CDATA[{5}]]></MusicUrl>                        <HQMusicUrl><![CDATA[{6}]]></HQMusicUrl>                        </Music>                        </xml>", strOpenId, strWxAccount, this.DateTimeToStamp(DateTime.Now).ToString(), strSongName, strSonger, this.GetMusicUrl(strXml), this.GetMusicUrl(strXml));            return strWxMusicXml;        }        /// <summary>        /// 擷取百度音樂連結的URL地址        /// </summary>        /// <param name="strXml">訪問百度音樂得到的XML</param>        /// <returns></returns>        private string GetMusicUrl(string strXml)        {                        string strMusicUrl = null;            //讀取xml            XDocument doc = XDocument.Parse(strXml);            XElement RootEle = doc.Root;                      List<XElement> XEleColl = RootEle.Elements().ToList();            strMusicUrl = HttpUtility.UrlDecode(XEleColl.ElementAt(1).Element("encode").Value);            //拼接地址:取前面的一段地址+decode節點地址            int posFlag = strMusicUrl.LastIndexOf("/");            strMusicUrl = strMusicUrl.Substring(0, posFlag + 1);            string strValue = XEleColl.ElementAt(1).Element("decode").Value;            strValue = strValue.Substring(0, strValue.IndexOf("&"));            strMusicUrl += strValue;//加上後一段地址            return strMusicUrl;        }        /// <summary>        /// DateTime時間格式轉換為Unix時間戳記格式        /// </summary>        /// <param name="time"></param>        /// <returns></returns>        private int DateTimeToStamp(System.DateTime time)        {            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));            return (int)(time - startTime).TotalSeconds;        }

           

            

             接著,在配置的伺服器位址

     

            

                 中的處理使用者訊息頁面做出對音樂訊息的處理,然後伺服器處理後將音樂訊息返給使用者。



               






         

        

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.