Using AFNetworking to upload an Audio File

2019-05-21 08:10发布

I'm trying to use the AFNetworking library to upload a file to Clypit. I've looked at their documentation here: https://github.com/AFNetworking/AFNetworking

And have configured my code to upload an audio file like so:

        NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:cellIndexPath];
        ICIRecordingCell *c = (ICIRecordingCell *)cell;
        NSString *fileName = c.title.text;

        NSURL *filePath = [NSURL fileURLWithPath:fileName];

        NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://upload.clyp.it/upload"
            parameters:nil
             constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                 [formData appendPartWithFileURL:filePath name:@"audioFile" fileName:@"audio.m4a" mimeType:@"audio/m4a" error:nil];
             } error:nil];

        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

        NSProgress *progress = nil;
        NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                NSLog(@"%@ %@", response, responseObject);
            }
        } ];
        [uploadTask resume];

But neither responseObject or error are ever firing. I'm not even certain if this is the type of request I should be sending. Clypit Api says they accept request as follows:

Upload an audio file. Accepted file types are mp3, ogg, m4a, wav, aiff, aif and 3gpp. Regardless of file type when uploading, a resulting mp3 and ogg will be created and made available. The audio file can be added to a playlist by providing a playlistId and the playlistUploadToken. Otherwise, a new playlist will automatically be created. A playlist can contain a maximum of 20 audio files. The title of the audio file will be set to that of the name of the uploaded file.

Parameters: audioFile - The binaries of the audio file. playlistId - Optional. The playlist that the audio file will be added to. If this value is specified, the correct playlistUploadToken must also be included in the request. If this value is not specified, a playlist will be automatically created. playlistUploadToken - Optional. Given to you after you create a playlist. When adding an audio file to an already existing playlist, this value must be provided. order - Optional. The position in which you would like this audio file to appear in the playlist. description - Optional. The description of the audio file. Maximum allowed length is 420 characters. longitude - Optional. The longitude of where the audio file was recorded. If passed in, latitude becomes required. Value must be between -15069 and 15069 degrees. latitude - Optional. The latitude of where the audio file was recorded. If passed in, longitude becomes required. Value must be between -90 and 90 degrees.

Uploads are done via a multipart/form-data POST. Consider the following form:

It will create a request that looks like this: POST http://upload.clyp.it/upload HTTP/1.1 Host: upload.clyp.it Connection: keep-alive Content-Type: multipart/form-data; boundary=---------------------------21632794128452 Content-Length: 5005 -----------------------------21632794128452 Content-Disposition: form-data; name="audioFile"; filename="MyAudioFile.mp3" Content-Type: audio/mpeg (Audio file data goes here)

Am I using the right approach? Thanks in advance

0条回答
登录 后发表回答