无法使用MPMoviePlayerViewController播放视频

max*_*nev 8 iphone video ios

我使用以下ViewController.m创建了一个新项目.当我运行应用程序时,我可以看到一个预期的原点/大小的框(38,100,250,163),但它是黑色的,没有视频播放.Xcode中有一个奇怪的输出:

2012-08-23 15:36:45.559 VideoTest1[11398:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2012-08-23 15:36:45.560 VideoTest1[11398:c07] [MPAVController] Autoplay: Disabling autoplay
2012-08-23 15:37:18.186 VideoTest1[11398:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
Run Code Online (Sandbox Code Playgroud)

请注意,视频使用Videora iPhone Converter进行转换,并在Xcode中播放正常(因此不会出现视频问题); 视频的路径是可以的,因为当我指定demo-iPhone1(不存在)时,我得到一个零例外.我试过在模拟器和iPhone上:总是黑盒子.有任何想法吗?

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>  

@interface ViewController ()

@end

@implementation ViewController
- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];

    [moviePlayerController.view removeFromSuperview];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"demo-iPhone" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];
    [moviePlayerController.view setFrame:CGRectMake(38,
                                                    100,
                                                    250,
                                                    163)];
    [self.view addSubview:moviePlayerController.view];
    [moviePlayerController play];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
Run Code Online (Sandbox Code Playgroud)

vma*_*njz 13

我刚用这行代码解决了类似的问题.播放器控制器现在显示,视频播放完美:

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
//
@synthesize moviePlayer = _moviePlayer;
//
[self.moviePlayer prepareToPlay];
Run Code Online (Sandbox Code Playgroud)

修改以适合您的环境.

  • 在iPad/iOS6上调用"prepareToPlay"为我修复了这个问题.没有它,"MPMoviePlayerLoadStateDidChangeNotification"没有被发送,因为播放器没有费心预加载视频. (2认同)

Bab*_*nda 3

我通过添加解决了类似的问题playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;