MPMoviePlayerController iO7问题

时间:2013-10-07 07:48:50

标签: iphone ios objective-c xcode ios7

我正在尝试通过MPMoviePlayerController播放存储在应用程序文档目录中的视频。我使用以下方法播放视频:

NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [directoryPath objectAtIndex:0];
int videoNumber = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"videoSaved"];
NSString* videoName = [NSString stringWithFormat:@"video-%d.mov",videoNumber];
NSString *exportPath = [docsDir stringByAppendingPathComponent:videoName];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath isDirectory:NO];

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:exportUrl];
moviePlayer.fullscreen = YES;
moviePlayer.view.frame = self.view.bounds;
[self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieDidExitFullscreen:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:nil];

除了iOS7之外,它适用于所有iOS版本。在iOS7中,当我尝试播放视频时,我发现了这个错误:

_itemFailedToPlayToEnd: {    kind = 1;    new = 2;    old = 0;}

1 个答案:

答案 0 :(得分:2)

尝试将代码更改为:

int videoNumber = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"videoSaved"];

moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:
    [NSURL fileURLWithPath: [[NSBundle mainBundle] 
    pathForResource: [NSString stringWithFormat:@"video-%d",videoNumber] ofType:@"mov"]]];

moviePlayer.fullscreen = YES;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.view.frame = self.view.bounds;
[self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieDidExitFullscreen:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:nil];