【问题标题】:Videos start but crash after a few seconds with MPMoviePlayerViewController使用 MPMoviePlayerViewController 视频开始播放但几秒钟后崩溃
【发布时间】:2014-04-30 09:00:37
【问题描述】:

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

-(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)}";

【问题讨论】:

  • 原来我尝试播放的视频已损坏,下次我会先检查一下。

标签: ios objective-c uiviewcontroller mpmovieplayercontroller mpmovieplayer


【解决方案1】:

尝试简单的步骤:

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

【讨论】:

    【解决方案2】:

    试试这个代码一次:

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

    并确保

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

    【讨论】:

      猜你喜欢
      • 2013-12-23
      • 1970-01-01
      • 2014-10-04
      • 2022-08-19
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多