Learn a little flash every day (76) Baidu MP3 music API use

Source: Internet
Author: User
Baidu MP3 music API interface and Application

Http://cloud21.javaeye.com/blog/611914

 

 

Here is an application about the Baidu MP3 music API. I wrote an application about this Baidu interface, which provides a small part of the search function, using it for MP3 search is still interesting. But the provided interface method still has some defects. However, it is quite interesting to use it on Weibo. Sina Weibo does not provide this search information interface when it does not cooperate with the official team. It can only be used by partners. Baidu has powerful search capabilities, but data is not open to individuals. Therefore, this small interface can only be used.

 

As the author mentioned above, it is impossible to search for a song that requires both the song name and the song name. The problem is that searching is inconvenient.

Another saying is that inputting these two keywords is helpful for positioning, But you need to remember the singer and song name, and the return time is relatively fast.

 

However, temporary use is also possible.

 

Http://box.zhangmen.baidu.com/X? OP = 12 & COUNT = 1 & Title = about winter $ Qi Qin $

 

 

Using this interface, we need to modify it first.

Http://box.zhangmen.baidu.com/X? OP = 12 & COUNT = 1 & Title = songname $ songauthor $

 

Modify two keywords to facilitate replacement.

Use a string to separate the strings, and use the "#" symbol to mark the strings.

In this way, you can replace the desired link address when searching.

VaR infoarray: array = info. split ("#"); </P> <p> var newstr: String = searapiurl. replace ("songname", infoarray [0]); <br/> newstr = newstr. replace ("songauthor", infoarray [1]); </P> <p>

 

Remove unnecessary spaces when replacing them, and you can enter them at will.

// Remove the blank <br/> private function replacspace (STR: string): String <br/>{< br/> return STR = Str. replace (/s) */GI, ""); </P> <p>}

 

 

Remotely calling this interface will return a small number of Song links. At this time, we will splice the songs for search. Because we only need MP3 files, therefore, WMA and RM are not what we need. Therefore, we need to make a handsome selection when searching for the returned results. After selection, we need to bind it to an array. We want to get the lyrics, at the same time, the practice is to refer to the practice mentioned in the previous article to splice the LRC address. The LRC address may be blank, so you can add a condition to make the selection.

// Crop the MP3 address <br/> private function getsongaddress (URL: String, songname: string): String <br/>{< br/> var end: Int = URL. lastindexof ("/", URL. length-1); <br/> return URL. substring (0, end + 1) + songname; <br/>}</P> <p> // returns the LRC address <br/> private function getlrc (URL: string): String <br/>{< br/> return lrcurl + int (INT (URL)/100 ). tostring () + "/" + URL + ". LRC "; <br/>}< br/>

 

 

An XML file is returned by calling this interface:

For example:

<? XML version = "1.0" encoding = "gb2312"?> <Br/>-<result> <br/> <count> 5 </count> <br/>-<URL> <br/> <encode> disable </encode> <br/> <decode> response? Xcode = bfdfd728537234893c350b4e1ff22833 </decode> <br/> <type> 1 </type> <br/> <lrcid> 14706 </lrcid> <br/> </URL> <br/>-<URL> <br/> <encode> http://individual.utoronto.ca/xmc84/fpOslpp ~ Py-aokezpzy1_limit </encode> <br/> <decode> maybeinwinterlimit </decode> <br/> <type> 1 </type> <br/> <lrcid> 14706 </lrcid> <br/> </URL> <br/>-<URL> <br/> <encode> http://vietnamese.cri.cn/mmsource/audio/2004/07/27/lZOsqZqwmKGVoaGbn583.rm </encode> <br/> <decode> dayuezaidongji. rm </decode> <br/> <type> 2 </type> <br/> <lrcid> 14706 </lrcid> <br/> </URL> <br/ >-<URL> <br/> <encode> scheme </encode> <br/> <decode> about winter solstice </decode> <br/> <type> 1 </Type> <br/> <lrcid> 14706 </lrcid> <br/> </URL> <br/>-<URL> <br/> <encode> HTTP: // 120.35.4.23/data/MP3/logs </encode> <br/> <decode> encoding </decode> <br/> <type> 1 </type> <br/> <lrcid> 14706 </lrcid> <br/> </URL> <br/>-<P2P> <br/> <pash> 5babc209cd096f0a3529a3dc8f66754bc58d0e29 </HASH> <br/> </P2P> <br/> </result>

 

 

The XML operation returns the desired node:

 

Encode: Song address path

Decode: song name

Type: Type

Lrcid: lrc

Encode and

Data on the decode node is spliced. For details, refer to the preceding section.

Http://cloud21.javaeye.com/blog/611914

The method described in this article.

 

 

 

 

Basic testing:

 

 <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <s: group xmlns: FX = "http://ns.adobe.com/mxml/2009" <br/> xmlns: S = "Library: // ns.adobe.com/flex/spark" <br/> xmlns: MX = "Library: // ns.adobe.com/flex/mx" width = "386" Height = "222" creationcomplete = "Init ()"> </P> <p> <s: textinput id = "MSG" text = "Love and sincerity # Gu Juji" x = "14" Y = "22" width = "255"/> <br/> <s: button x = "296" Y = "23" label = "Search" Click = "Search ()"/> <br/> <s: label x = "14" Y = "52" text = "NOTE: When searching, use the song name # singer name" color = "0xf F0000 "/> <br/> <mx: DataGrid id =" DG "x =" 5 "Y =" 72 "width =" 380 "> <br/> <mx: columns> <br/> <mx: datagridcolumn headertext = "Address" width = "250" datafield = "Address" editable = "true"> <br/> </MX: datagridcolumn> <br/> <mx: datagridcolumn headertext = "action"> <br/> <mx: itemrenderer> <br/> <FX: component> <br/> <mx: linkbutton label = "" width = "20" Height = "12" Click = "this. parentdocument. playsong (event) "/> <br/> </FX: component> <B R/> </MX: itemrenderer> <br/> </MX: datagridcolumn> <br/> <mx: datagridcolumn headertext = "lyrics" datafield = "LRC"> <br/> <mx: itemrenderer> <br/> <FX: component> <br/> <mx: linkbutton label = "lyrics" width = "20" Height = "12" Click = "this. parentdocument. loadsonglrc (event) "/> <br/> </FX: component> <br/> </MX: itemrenderer> <br/> </MX: datagridcolumn> <br/> </MX: columns> <br/> </MX: DataGrid> <br/> <s: labelx = "252" Y = "52" id = "alert MSG "text =" "width =" 200 "color =" 0xff0000 "/> </P> <p> <FX: SCRIPT> <br/> <! -- [CDATA [<br/> Import flash. events. event; <br/> Import flash. events. ioerrorevent; <br/> Import flash. events. keyboardevent; <br/> Import flash. events. mouseevent; <br/> Import flash. events. progressevent; <br/> Import flash. media. sound; <br/> Import flash. media. soundchannel; <br/> Import flash. media. soundloadercontext; <br/> Import flash. media. soundmixer; <br/> Import flash.net. *; <br/> Import flash. UI. keyboard; <Br/> Import flash. utils. bytearray; </P> <p> Import MX. collections. arraycollection; <br/> Import MX. controls. *; <br/> Import MX. controls. datagridclasses. datagridcolumn; <br/> Import MX. core. uicomponent; </P> <p> Import spark. components. window; </P> <p> private var searapiurl: String = "http://box.zhangmen.baidu.com/X? OP = 12 & COUNT = 1 & Title = songname $ songauthor $ "; <br/> private var lrcurl: String =" http://box.zhangmen.baidu.com/bdlrc "; // LRC <br/> private var Loader: urlloader; </P> <p> [Bindable] <br/> private var songlist: arraycollection = new arraycollection (); </P> <p> private var sound: sound; <br/> private var soundchannel: soundchannel = new soundchannel (); <br/> private function search (): void <br/> {<br/> If (MSG. TEXT = "") Re Turn; <br/> songlist. removeall (); <br/> var info: String = replacspace (MSG. text); <br/> var infoarray: array = info. split ("#"); <br/> var newstr: String = searapiurl. replace ("songname", infoarray [0]); <br/> newstr = newstr. replace ("songauthor", infoarray [1]); <br/> loader. load (New URLRequest (newstr); <br/>}</P> <p> // clear the blank <br/> private function replacspace (STR: string ): string <br/>{< br/> return STR = Str. replace (/s) */GI, ""); </P> <p >}</P> <p> // love and Cheng # Gu Juji <br/> private function Init (): void <br/>{< br/> loader = new urlloader (); <br/> loader. addeventlistener (event. complete, ondata); <br/> MSG. setfocus (); <br/> addeventlistener (keyboardevent. key_down, onkeydownhandler); <br/> This. addeventlistener (event. removed_from_stage, onremoveself); </P> <p >}< br/> private function onremoveself (Event: Event): void <br/>{< br/> removeeventlist Ener (event. removed_from_stage, onremoveself); <br/> removeeventlistener (keyboardevent. key_down, onkeydownhandler); <br/> loader = NULL; <br/>}</P> <p> private function onkeydownhandler (Event: keyboardevent ): void <br/>{< br/> If (event. keycode = keyboard. enter) <br/>{< br/> Search (); <br/>}</P> <p> private function ondata (Event: Event): void <br/>{< br/> var XML: xml = XML (loader. data); <br/> trace (XML); <br/> VaR Count: Int = int (XML. count); <br/> for (var I: Int = 0; I <count; I ++) <br/>{< br/> If (XML. URL [I]. type. tostring () = "1") <br/>{< br/> var songurl: String = getsongaddress (XML. URL [I]. encode, XML. URL [I]. decode); <br/> var lrcurl: String = getlrc (XML. URL [I]. lrcid); // The loaded LRC may not exist. <br/> var item: Object = {address: songurl, LRC: lrcurl}; <br/> songlist. additem (item); <br/>}< br/> DG. dataprovider = songlist; <br/>}</P> <p> // cut Cut MP3 address <br/> private function getsongaddress (URL: String, songname: string): String <br/>{< br/> var end: Int = URL. lastindexof ("/", URL. length-1); <br/> return URL. substring (0, end + 1) + songname; <br/>}</P> <p> // returns the LRC address <br/> private function getlrc (URL: string): String <br/>{< br/> return lrcurl + int (INT (URL)/100 ). tostring () + "/" + URL + ". LRC "; <br/>}</P> <p> // playback sound <br/> Public Function playsong (Event: mouseevent): voi D <br/> {<br/> var URL: String = event. currenttarget. parentdocument. DG. selecteditem. address; <br/> soundmixer. stopall (); <br/> loadsong (URL); <br/>}</P> <p> // view the lyrics <br/> Public Function loadsonglrc (event: mouseevent): void <br/>{< br/> var URL: String = event. currenttarget. parentdocument. DG. selecteditem. LRC; <br/> // trace (URL); <br/> var Loader: urlloader = new urlloader (); <br/> loader. dataformat = urlloaderdataformat. Binary; <br/> loader. addeventlistener (event. complete, onloadlrccompletehandler); <br/> loader. addeventlistener (ioerrorevent. io_error, onloadlrcerrorhandler); <br/> loader. load (New URLRequest (URL); <br/>}</P> <p> private function onloadlrccompletehandler (Event: Event ): void <br/>{< br/> event. currenttarget. removeeventlistener (event. complete, onloadlrccompletehandler); <br/> event. currenttarget. removeeventlist Ener (ioerrorevent. io_error, onloadlrcerrorhandler); <br/> var Bytes: bytearray = bytearray (event. currenttarget. data); <br/> var stringlength: Int = bytes. bytesavailable; <br/> var outputstr: String = bytes. readmultibyte (stringlength, "gb2312"); <br/> trace (outputstr); <br/>}</P> <p> private function onloadlrcerrorhandler (Event: ioerrorevent ): void <br/> {<br/> alertmsg. TEXT = "loading failed"; <br/>}</P> <p> // load the song <br/> priv Ate function loadsong (URL: string): void <br/>{< br/> If (sound! = NULL) <br/>{< br/> soundchannel. stop (); <br/> sound = NULL; <br/>}< br/> sound = new sound (null, new soundloadercontext (2000 )); <br/> sound. addeventlistener (event. complete, loadsongcomplete); <br/> sound. addeventlistener (ioerrorevent. io_error, onloaderror); <br/> sound. addeventlistener (progressevent. progress, onloadprogress); <br/> sound. load (New URLRequest (URL); <br/> alertmsg. TEXT = "loading"; </P> <p >}< br/> private function onloaderror (Event: Event): void <br/>{< br/> alertmsg. TEXT = "loading failed"; <br/>}</P> <p> private function onloadprogress (Event: progressevent): void <br/>{< br/> alertmsg. TEXT = "loading" + int (event. bytesloaded/event. bytestotal * 100 ). tostring () + "/%"; <br/>}</P> <p> private function loadsongcomplete (Event: Event ): void <br/>{< br/> event. currenttarget. removeeventlistener (event. complete, loadsongcomplete); <br/> soundchannel = event. currenttarget. play (); <br/> alertmsg. TEXT = "loaded successfully"; <br/>}</P> <p>] --> <br/> </FX: SCRIPT> <br/> </S: group> <br/>

 

 

Create an air program and run the file.

 <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <s: Drawing wedapplication xmlns: FX = "http://ns.adobe.com/mxml/2009" <br/> xmlns: S = "Library: // ns.adobe.com/flex/spark" <br/> xmlns: MX = "Library: // ns.adobe.com/flex/mx" xmlns: NS1 = "*"> <br/> <FX: declarations> <br/> <! -- Place non-visual elements (e.g ., services, value objects) Here --> <br/> </FX: declarations> <br/> <NS1: mp3serch x = "0" Y = "77"> <br/> </NS1: mp3serch> </P> <p> </S: windowedapplication> <br/>

 

 

After editing, you can perform this operation to enrich the data used in the search.

After being integrated into the Weibo client, you can share songs and view the lyrics.

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.