Use GraceNote SDK in IOS (ii) Get complete information on music

Source: Internet
Author: User

After the demand was completely clarified, plus the transfer from Musicfans to Gracenote, and then from Gracenote's gnsdk to the iOS SDK, the ability to get complete information on iOS via part of the music was finally completed. (Well, I admit it's relatively complete ...) )


The iOS SDK that configures Gracenote in the project is introduced first.

SDK:Mobile Client

Note To log in before you can see the download link for the file. In addition, the official website also provides an SDK configuration document , completely follow the walk in Xcode 5 is not go through, but also has a certain guiding role, suggest look.

After downloading the extract, create a new project and add Gracenotemusicid.framework to the project:


Create a new header file GraceNote.h, import the header files in the framework (all the header files that need to be used in this project have been imported):

#ifndef mfdemo_ios_gracenote_h#define mfdemo_ios_gracenote_h#import <GracenoteMusicID/GNConfig.h> #import <GracenoteMusicID/GNOperations.h> #import <GracenoteMusicID/GNSearchResultReady.h> #import < gracenotemusicid/gnsearchresponse.h> #import <GracenoteMusicID/GNSearchResult.h> #import < gracenotemusicid/gnimage.h> #import <GracenoteMusicID/GNCoverArt.h> #import <gracenotemusicid/ Gndescriptor.h> #endif

Then configure the engineering environment to include the following system library files in Build phases:



Configuration is complete.


In fact, the use of this SDK is very simple.

The first step is to configure the Gnconfig class with your Gracenote account (I put it directly in the appdelegate so that it can be configured globally):

#import <UIKit/UIKit.h> #import <GracenoteMusicID/GNConfig.h> @interface Appdelegate:uiresponder < Uiapplicationdelegate> @property (Strong, nonatomic) UIWindow *window; @property (retain, nonatomic) Gnconfig *app_ Gnconfig; @end
#import "AppDelegate.h" static NSString * Kclientid = @ "4541440-79efbf4e21724d084ba87ff9b242f0c9"; static NSString * Kcoverartproperty = @ "Content.coverart"; static NSString * Kcoverartsizeproperty = @ "Content.coverArt.sizePreference"; Static NSString * kyesbooleanstring = @ "1", static NSString * Kcoverartsizelarge = @ "LARGE"; static NSString * kcoverartsize Thumbnail = @ "Thumbnail"; static NSString * Kcoverartsizesmall = @ "SMALL"; @implementation appdelegate@synthesize App_ Gnconfig = _app_gnconfig;-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: ( Nsdictionary *) launchoptions{    self.app_gnconfig = [Gnconfig Init:kclientid];//<client ID>-<Client ID Tag >    [_app_gnconfig setproperty:kcoverartproperty value:kyesbooleanstring];    [_app_gnconfig Setproperty:kcoverartsizeproperty value:kcoverartsizethumbnail];        return YES;}

The client ID is the ID of the application, and it is unclear to use the GraceNote SDK in IOS (i) to query the album art through the serialized Gdo . The Content.coverart property is then set to open, otherwise there will be no album art in the Returned data.

The second step is to initiate a query request using the following method:

+ (void) Searchbytext: (id<gnsearchresultready>) Resultready               config: (gnconfig*) config               artist: ( nsstring*) Artist           Albumtitle: (nsstring*) Albumtitle           tracktitle: (nsstring*) Tracktitle;

In the demo, there is a method in the button:

-(ibaction) Check: (ID) Sender {    [_checking_activityindicator startanimating];    Self.view.alpha = 0.75;    self.view.userInteractionEnabled = NO;    [gnoperations searchbytext:self                        config: ((appdelegate *) [[uiapplication Sharedapplication] delegate]). App_ Gnconfig                        artist:_artist_textfield.text                    albumtitle:_album_textfield.text                    tracktitle:_tracktitle_ Textfield.text];}

Note The Resultready parameter is set to an object that adheres to the Gnsearchresultready protocol, which is self.

The config parameter is set to the global configuration.

Artist,albumtitle,tracktitle are artists, album names, music names, etc., these are key searches, and three parameters can be up to two by default.

In the third step, after the query succeeds, we can get the data returned from the server from the Gnresultready: method, stripping out the information we need. However, it is very strange that for each Gnsearchresponse object in the returned result, its albumcoverart are nil. As shown, note that the value of M_coverart for each object in 10 objects is nil.


The alternative is to record the ID information of the Gnsearchresponse object, and then initiate two requests through albumid to get the complete album information from the server (this is not a good idea, but for the moment I only find this workaround)

#pragma mark-gnsearchresultready protocol-(void) Gnresultready: (Gnsearchresult *) result{Nsarray *responses = [result    Responses];  if (!responses | |!responses.count) {[[[Uialertview alloc] initwithtitle:@ "Sorry" message:@ "no matching results found" Delegate:nil        cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil] show];        [_checking_activityindicator stopanimating];        self.view.userInteractionEnabled = YES;        Self.view.alpha = 1.0;        _check_button.hidden = NO;    Return        } [_albumidarray removeallobjects];    int i = 0;        For (Gnsearchresponse *resp in responses) {if (i = =) {break;        } nsstring *albumid = Resp.albumid;            if (albumid) {[_albumidarray addobject:albumid];        i++; }} if (!_albumidarray.count) {[[[Uialertview alloc] initwithtitle:@ "Sorry" message:@ "no matching results found" delegate        : nil cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil] show];  Return  } else {[Self performseguewithidentifier:@ "Check_segue" sender:self];        [_checking_activityindicator stopanimating];        self.view.userInteractionEnabled = YES;        Self.view.alpha = 1.0;    _check_button.hidden = NO; }}

two requests are initiated through albumID in the next view:

-(void) getalbumlists {    [_albuminfo removeallobjects];        For (NSString *album_id in _albumids) {        [gnoperations fetchbyalbumid:self                              config: ((appdelegate *) [[ UIApplication Sharedapplication] delegate]). App_gnconfig                             albumid:album_id];    }}


Last several run result graphs:



This demo is based on the album cover, Song style, complete song name, complete artist list, song style, distribution information and other relatively complete information, the focus is to get the album cover. The real machine debugging, no problem, it seems I can be done.


The complete code I will not post out, interested in downloading the Demo to see.

Description: Since the Gracenote SDK has 71M, I removed it in the demo, so the demo is not working. Please download the gracenotemusicid.framework to the Gracenote website and add it to the project yourself.




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.