【问题标题】:MPMoviePlayerController with no content (iOS 5) - The Operation Could Not Be Completed没有内容的 MPMoviePlayerController (iOS 5) - 操作无法完成
【发布时间】:2023-03-17 05:14:02
【问题描述】:

我对@9​​87654321@ (self.mp) 有疑问。当我想访问duration 属性时,我得到0,当我想访问thumbnail… 时,我得到nil。 iOS 4 可以,iOS 5 不行。

毕竟应用对我说:操作无法完成。

- (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_%.0f@2x.%@", 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。资源路径有效,但似乎没有内容。

感谢您的帮助。

【问题讨论】:

  • 如果您提供一个更简化的示例,那肯定会有所帮助...也可能对您有所帮助!
  • 好的,我已经添加了更简单的例子

标签: iphone video video-capture ios5


【解决方案1】:

我发现了我的问题。 好像iOS5不适应:

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

所以,如果你只是使用

NSURL *someUrl =[info objectForKey:UIImagePickerControllerMediaURL] ;

然后用这个 url 初始化 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];   


}

【讨论】:

  • 非常感谢。这就是我需要的。我在 iOS5 上的视频还有其他问题,但这是最大的问题。再次感谢您。
  • 啊!和你有同样的问题,我花了很长时间才弄清楚 MPMoviePlayerController 不喜欢某些 NSURL,即使它们被系统视为有效对象。 “秘密”是将文件路径获取为 NSString 然后使用[NSURL fileURLWithPath:URLStringPath]
  • 罗格的评论是准确的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-15
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 2013-07-25
  • 2019-11-22
相关资源
最近更新 更多