MPMoviePlayerController在iOS7上的问题

5

我正在尝试通过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];

在所有iOS版本中都可以很好地运行,除了iOS7。在iOS7上,当我尝试播放视频时,会出现以下错误:

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

@IronManGill,你可以看到我已经在exportURL中使用了fileURLWithPath - EXC_BAD_ACCESS
尝试使用NSLog打印exportUrl。 - Mutawe
@Mutawe 是的,它给了我正确的URL 看这个:file:///var/mobile/Applications/696925E2-BA2E-4359-AA81-41D98AA594DB/Documents/video-8.mov - EXC_BAD_ACCESS
请尝试设置:moviePlayer.movieSourceType = MPMovieSourceTypeFile; - Mutawe
@Mutawe 谢谢您的回复,但第二个答案也没有起作用 :( - EXC_BAD_ACCESS
显示剩余3条评论
1个回答

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];

它崩溃并给我这个日志:-[NSURL initFileURLWithPath:]: nil字符串参数。 - EXC_BAD_ACCESS
那你为什么接受了这个答案?它不起作用吗? - mattsson
他的问题与答案无关,而是与URL有关。 - Mutawe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接