将视频从应用上传到youTube iphone sdk

时间:2012-12-03 05:29:55

标签: video youtube-api gdata-api uploading

我想把我的应用中拍摄的视频上传到YouTube。我已将标题,用户名,密码,电影路径,长度,关键字和类别与我的应用密钥相关联。然而,当我尝试上传时,我收到以下消息。

2012-12-02 21:29:33.235我的小世界应用程序[12790:907] * -GDataServiceBase中的断言失败fetchObjectWithURL:objectClass:objectToPost:ETag:httpMethod:delegate:didFinishSelector:completionHandler:retryInvocationValue:ticket :,/ Users / nilesh / gdata-objectivec-client-read-only/Source/BaseClasses/GDataServiceBase.m:549 2012-12-02 21:29:33.238我的小世界应用程序[12790:907] * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'GTMHTTPUploadFetcher需要'

这是我用来尝试上传的代码。

- (IBAction)publish: (id) sender;{

    self.mProgressView.hidden =NO;

    NSString *audioName = [pictureDictionary4 objectForKey:@"photoVideokey"];
    NSArray *pathsa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectorya = [pathsa objectAtIndex:0];
    //Get a full path to the image in the documents directory.
    NSString *fullPatha = [documentsDirectorya stringByAppendingPathComponent:audioName];
    NSError *error = nil;
    NSDictionary *attributes = [[NSFileManager defaultManager]
                                attributesOfItemAtPath:fullPatha error:&error];

    if (!error) {
        NSNumber *size = [attributes objectForKey:NSFileSize];
    }

    NSString *devKey = @"xxxxxx";

    GDataServiceGoogleYouTube *service = [self youTubeService];
    [service setYouTubeDeveloperKey:devKey];

    NSString *username = self.accountView.text;
  NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username];
    NSData *data = [NSData dataWithContentsOfFile:fullPatha];
    NSString *filename = [pictureDictionary4 objectForKey:@"photoVideokey"];

    // gather all the metadata needed for the mediaGroup
    NSString *titleStr =self.movieNameField.text;
    GDataMediaTitle *title = [GDataMediaTitle textConstructWithString:titleStr];

    NSString *categoryStr = self.catagoryField.text;
    GDataMediaCategory *category = [GDataMediaCategory mediaCategoryWithString:categoryStr];
    [category setScheme:kGDataSchemeYouTubeCategory];

    NSString *descStr = self.descpView.text;
    GDataMediaDescription *desc = [GDataMediaDescription textConstructWithString:descStr];

    NSString *keywordsStr = self.tagsField.text;
    GDataMediaKeywords *keywords = [GDataMediaKeywords keywordsWithString:keywordsStr];

    BOOL isPrivate = NO;

    GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
    [mediaGroup setMediaTitle:title];
    [mediaGroup setMediaDescription:desc];
    [mediaGroup addMediaCategory:category];
    [mediaGroup setMediaKeywords:keywords];
    [mediaGroup setIsPrivate:isPrivate];

    NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:fullPatha
                                               defaultMIMEType:@"video/mp4"];

    // create the upload entry with the mediaGroup and the file data
    GDataEntryYouTubeUpload *entry;
    entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup
                                                          data:data
                                                      MIMEType:mimeType
                                                          slug:filename];

    SEL progressSel = @selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);
    [service setServiceUploadProgressSelector:progressSel];

    GDataServiceTicket *ticket;
    ticket = [service fetchEntryByInsertingEntry:entry
                                      forFeedURL:url
                                        delegate:self
                               didFinishSelector:@selector(uploadTicket:finishedWithEntry:error:)];

    [self setUploadTicket:ticket];

    NSLog(@"%@",length.text);
    NSLog(@"%@",number.text);
     NSLog(@"%@",accountView.text);
     NSLog(@"%@",tagsField.text);
     NSLog(@"%@",catagoryField.text);
     NSLog(@"%@",movieNameField.text); 
     NSLog(@"%@",descpView.text);
     NSLog(@"%@",PasswordDisplayField.text);
     NSLog(@"%@",ViewingField.text);

    NSLog(@"%@",username);
    NSLog(@"%@",url);
    NSLog(@"%@",filename);
    NSLog(@"%@",titleStr);
    NSLog(@"%@",category);
    NSLog(@"%@",categoryStr);
    NSLog(@"%@",descStr);
    NSLog(@"%@",desc);
    NSLog(@"%@",keywords);
     NSLog(@"%@",keywords);
     NSLog(@"%@",keywordsStr);



}





    - (GDataServiceGoogleYouTube *)youTubeService {

        static GDataServiceGoogleYouTube* service = nil;

        if (!service) {
            service = [[GDataServiceGoogleYouTube alloc] init];


            [service setServiceShouldFollowNextLinks:YES];
            [service setIsServiceRetryEnabled:YES];
        }

        // update the username/password each time the service is requested
        NSString *username = [accountView text];
        NSString *password = [PasswordDisplayField text];

        if ([username length] > 0 && [password length] > 0) {
            [service setUserCredentialsWithUsername:username
                                           password:password];
        } else {
            // fetch unauthenticated
            [service setUserCredentialsWithUsername:nil
                                           password:nil];
        }

        NSString *devKey =  @"AI39si5rnzA1o82wzQj_kFFJBQRNEPv29OKD9hx6RxLPzvacvfvLFi_uI9eNj0uq8Cul8VQnBbDs4CAvq7ViKq-RnVCgh3OVBQ";
        [service setYouTubeDeveloperKey:devKey];

        return service;
    }

    // progress callback
    - (void)ticket:(GDataServiceTicket *)ticket
hasDeliveredByteCount:(unsigned long long)numberOfBytesRead
ofTotalByteCount:(unsigned long long)dataLength {

    [mProgressView setProgress:(double)numberOfBytesRead / (double)dataLength];
}

    // upload callback
    - (void)uploadTicket:(GDataServiceTicket *)ticket
finishedWithEntry:(GDataEntryYouTubeVideo *)videoEntry
error:(NSError *)error {
    if (error == nil) {
        // tell the user that the add worked
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uploaded!"
                                                        message:[NSString stringWithFormat:@"%@ succesfully uploaded",
                                                                 [[videoEntry title] stringValue]]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];

        [alert show];

    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                        message:[NSString stringWithFormat:@"Error: %@",
                                                                 [error description]]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];

        [alert show];

    }
    [mProgressView setProgress: 0.0];

    [self setUploadTicket:nil];
}

#pragma mark -
#pragma mark Setters

    - (GDataServiceTicket *)uploadTicket {
        return mUploadTicket;
    }

    - (void)setUploadTicket:(GDataServiceTicket *)ticket {

        mUploadTicket = ticket;
    }

我真的很感激这方面的一些方向。感谢

2 个答案:

答案 0 :(得分:1)

听起来你错过了Google Toolbox for Mac - HTTP Fetcher依赖?

一般情况下,如果您在专用issue trackerdiscussion group中提问,您可能会对Objective-C GData客户端库特有的问题提供更多帮助,因为它实际上是一个实现问题,而不是一般问题YouTube API问题。

答案 1 :(得分:0)

我终于通过将所有正确的.h文件添加到我的应用程序并导入它们来完成此部分工作,包括正确的GTMHTTPUploadFetcher文件。谢谢你的帮助。