8

我使用以下 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)

请注意,视频是使用 Videora iPhone Converter 转换的,并且在 Xcode 中可以正常播放(所以这不是视频问题);视频的路径没问题,因为当我指定 demo-iPhone1 (不存在)时,我得到一个 nil 异常。我在模拟器和 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
4

4 回答 4

13

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

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

修改以适应您的环境。

于 2012-10-01T21:55:58.777 回答
3

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

于 2012-09-07T08:07:07.540 回答
2

你用ARC吗?如果是这样,您必须保留 MPMoviePlayerController!

将此添加到您的界面

@property (nonatomic, strong) MPMoviePlayerController *controller;

检查最后一行 viewDidLoad

- (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];
    [self setController:moviePlayerController];
}
于 2012-09-26T09:34:46.827 回答
-2

另请查看视频格式。我的示例视频.m4v(我从 Apple 的网站上下载的)不断收到此错误。最终,我尝试了一个不同的视频剪辑,.mp4并且效果很好。许多错误仍然出现在我的控制台上。

2013-01-22 15:44:04.850 VideoTesting[4497:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-22 15:44:04.851 VideoTesting[4497:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2013-01-22 15:44:04.853 VideoTesting[4497:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2013-01-22 15:44:04.853 VideoTesting[4497:c07] [MPAVController] Autoplay: Disabling autoplay
2013-01-22 15:44:04.861 VideoTesting[4497:c07] [MPAVController] Autoplay: Enabling autoplay

然而,视频仍然播放。

于 2013-01-22T20:50:22.853 回答