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

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

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

1 回答 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];
于 2013-10-07T08:23:43.470 回答