2011-10-14 42 views
1

我有MPMoviePlayerControllerself.mp)問題。當我想訪問duration屬性時,我得到0,當我想訪問thumbnail…時,我得到nil。在iOS 4上它是可以的,而不是iOS 5。沒有內容的MPMoviePlayerController(iOS 5) - 操作無法完成

畢竟app對我說:操作無法完成。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
    if([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]){ 
     NSString *tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; 


     NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
     double hashToNameOfFile = [NSDate timeIntervalSinceReferenceDate] * 1000.0; 
     NSString *finalPath = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat: @"%.0f.%@", hashToNameOfFile, @"MOV"]]; 
     NSString *finalPathToThumbRetina = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat: @"thumb_%[email protected]%@", hashToNameOfFile, @"jpg"]]; 
     NSString *finalPathToThumb = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat: @"thumb_%.0f.%@", hashToNameOfFile, @"jpg"]]; 

     // we do need retina now, we use real name for CoreData 
     NSString *finalImage = [finalPath lastPathComponent]; 
     NSString *finalImageThumb = [finalPathToThumb lastPathComponent]; 


     if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath)){ 
      // Copy it to the camera roll. 
      UISaveVideoAtPathToSavedPhotosAlbum(tempFilePath, self, @selector(video:didFinishSavingWithError:contextInfo:), tempFilePath); 
     } 

     // save video to application directory 
     NSData *videoData = [NSData dataWithContentsOfFile:tempFilePath]; 
     if ([videoData writeToFile:finalPath atomically:YES]) { 
      NSLog(@"SAVED"); 
     } 
     else{ 
      NSLog(@"NOT SAVED"); 
     } 


     // create thumbnail of video 
     MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:finalPath]]; 
     self.mp = moviePlayer; 
     [moviePlayer release]; 

     NSData *videoData2 = [NSData dataWithContentsOfFile:finalPath]; 
     NSLog(@"LENGTH %i", [videoData2 length]); 


     NSLog(@"FINAL PATH %@", finalPath); 
     UIImage *image = [[[UIImage alloc] init] autorelease]; 
     image = [self.mp thumbnailImageAtTime:(NSTimeInterval)1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; 
     NSLog(@"%@", [image size]); 
     UIImage *thumbImageRetina = [image thumbnailImage:400 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationDefault]; 
     UIImage *thumbImage = [image thumbnailImage:200 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationDefault]; 
     NSData *thumbImageRetinaData = [NSData dataWithData:UIImageJPEGRepresentation(thumbImageRetina, 1.0)]; 
     NSData *thumbImageData = [NSData dataWithData:UIImageJPEGRepresentation(thumbImage, 1.0)]; 
     if ([thumbImageRetinaData writeToFile:finalPathToThumbRetina atomically:YES]) { 
      NSLog(@"RETINA THUMB SAVED"); 
     } 
     else{ 
      NSLog(@"RETINA THUMB NOT SAVED"); 
     } 
     if ([thumbImageData writeToFile:finalPathToThumb atomically:YES]) { 
      NSLog(@"THUMB SAVED"); 
     } 
     else{ 
      NSLog(@"THUMB NOT SAVED"); 
     } 

     // duration of video 
     double dur = [self.mp duration]; 
     NSLog(@"DUR %f", dur); 


     TaleRecords *newTaleRecord = (TaleRecords *)[NSEntityDescription insertNewObjectForEntityForName:@"TaleRecords" inManagedObjectContext:_context]; 

     newTaleRecord.content = finalImage; 
     newTaleRecord.date = [NSDate date]; 
     NSDecimalNumber *latNum = [[NSDecimalNumber alloc] initWithDouble:[self.latitude doubleValue]]; 
     NSDecimalNumber *longNum = [[NSDecimalNumber alloc] initWithDouble:[self.longitude doubleValue]]; 
     newTaleRecord.latitude = latNum; // vertical 
     newTaleRecord.longitude = longNum; // horizontal 
     [latNum release]; 
     [longNum release]; 
     newTaleRecord.contentType = [[NSString alloc] initWithFormat:@"cTypeVideo"]; 
     newTaleRecord.thumb = finalImageThumb; 
     newTaleRecord.duration = [NSNumber numberWithDouble:dur]; 

     newTaleRecord.tale = tales; 
     tales.record = [newTaleRecord tale].record; 

     NSError *error = nil; 
     if (![_context save:&error]) { 
      /* 
      Replace this implementation with code to handle the error appropriately. 

      abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
      */ 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 


    [CLController.locationManager stopUpdatingLocation]; 
    [self dismissModalViewControllerAnimated:YES]; 
} 

更簡單的例子

我這樣做:

// save video to application directory 
NSData *videoData = [NSData dataWithContentsOfFile:tempFilePath]; 
if ([videoData writeToFile:finalPath atomically:YES]) { 
    NSLog(@"SAVED"); 
} 
else{ 
    NSLog(@"NOT SAVED"); 
} 

tempFilePath是真正的視頻內容,我把它寫入finalPath。從那以後,我創建MPMoviePlayerController例如:

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:finalPath]]; 
self.mp = moviePlayer; 
[moviePlayer release]; 

self.mp給我任何內容。當我想要[self.mp duration]時,它返回zero/nil。資源工程的路徑,但似乎沒有內容。

謝謝你的幫助。

+0

,如果你提供了一個更簡單的例子這將絕對有幫助...可能會幫助你的! –

+0

好的,我添加了**更簡單的示例**。 – petesalt

回答

3

我想出了我的問題。 它就像iOS5的不舒服:

NSString *tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; 

如此相反,如果你只是用

NSURL *someUrl =[info objectForKey:UIImagePickerControllerMediaURL] ; 

,然後用這個網址初始的MPMoviePlayerController,它工作正常,我, 這正是與iOS4相反。

這裏是我的代碼:

- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 



    mediaURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
    NSLog(@"%@",mediaURL); 
    if (!mymoviePlayer) { 
     mymoviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:mediaURL]; 
     [mymoviePlayer setControlStyle:MPMovieControlStyleNone]; 
     [mymoviePlayer.view setFrame: videoView.bounds]; 
     [videoView addSubview:mymoviePlayer.view]; 
    } 
    else 
    { 

     [mymoviePlayer setContentURL:mediaURL]; 
     [mymoviePlayer stop]; 
     [mymoviePlayer setInitialPlaybackTime:-1]; 
    } 

[self dismissModalViewControllerAnimated:YES]; 


} 
+0

非常感謝。這是我需要的。 iOS5上的視頻還有其他問題,但這是最大的問題。再次感謝你。 – petesalt

+1

唉!與你有同樣的問題,並花了我很長時間才發現MPMoviePlayerController不喜歡某些NSURL,即使它們被系統視爲有效的對象。 「祕密」是將文件路徑作爲NSString,然後使用'[NSURL fileURLWithPath:URLStringPath]' – Rog

+0

Rog的評論是準確的答案。 –

相關問題