使用MPMoviePlayerViewController几秒后视频开始但崩溃

时间:2014-04-30 09:00:38

标签: ios objective-c uiviewcontroller mpmovieplayercontroller mpmovieplayer

我用来播放视频的方法是:

-(void)playVideo:(NSString*)title {

NSLog(@"NAME: %@", title);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *moviePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", title]];

MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];

mp.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[self presentMoviePlayerViewControllerAnimated:mp];

[mp release];
}

视频停止大约4或5秒后,显示上一个屏幕,我收到以下错误:

_itemFailedToPlayToEnd: {
AVPlayerItemFailedToPlayToEndTimeErrorKey = "Error Domain=AVFoundationErrorDomain Code=-11800 \"The operation could not be completed\" UserInfo=0xb19a220 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0xb17fac0 \"The operation couldn\U2019t be completed. (OSStatus error -12551.)\", NSLocalizedFailureReason=An unknown error occurred (-12551)}";

2 个答案:

答案 0 :(得分:0)

尝试简单步骤:

第1步:导入MediaPlayer框架 #import <MediaPlayer/MediaPlayer.h>

第2步:在.h文件中设置委托 UIViewController<MPMediaPlayback>

第3步:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *fileLocation= [bundle pathForResource:@"file_video" ofType:@"m4v"];
    NSURL *vedioURL =[NSURL fileURLWithPath:fileLocation];
    MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
    [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
    [videoPlayerView.moviePlayer play];
}

-(void)endSeeking
{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)stop
{
    [self.navigationController popViewControllerAnimated:NO];
}

答案 1 :(得分:0)

尝试使用此代码一次:

-(void)playVideo
{
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:VideoURL]];
]
[self presentMoviePlayerViewControllerAnimated:mp];
[mp.moviePlayer play];
}

并确保

  1. 您已加入MediaPlayer.framework
  2. 在您的班级中导入MediaPlayer / MediaPlayer.h文件