Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of uploading a track to the Echo Nest for analysis without ENIOS
- (void) uploadTrack: (Track *) track {
NSDictionary *params = @{
@"filetype" : @"m4a",
@"api_key" : self.apiKey,
};
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *audioPath = [self pathToAudio:track];
NSString *path = [audioPath path];
NSData *data = [fm contentsAtPath:path];
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://developer.echonest.com/api/v4/"]];
NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path: @"track/upload"
parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:@"track"
fileName:[path lastPathComponent] mimeType:@"multipart/form-data"];
}];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
NSLog(@"done");
NSLog(@"response: %@", json);
NSString *trackID = json[@"response"][@"track"][@"id"];
NSLog(@"Track ID is %@", trackID);
track.trackID = trackID;
[track asyncUpdateState:STATE_UPLOADED];
[track asyncUpdateStatus:STATUS_OK];
} failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id json) {
[track asyncUpdateDetails:@"upload error"];
[track asyncUpdateStatus:STATUS_NON_FATAL_ERROR];
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
int progress = rint(totalBytesWritten * 100.0 / (float) totalBytesExpectedToWrite);
NSLog(@"Upload progress %d", progress);
if (progress > 99) {
progress = -50;
[track asyncUpdateDetails:@"Queueing for analysis"];
}
[track asyncUpdateProgress:progress];
}];
[track asyncUpdateDetails:@"Uploading to The Echo Nest"];
[operation start];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment