2012-08-23 45 views
8

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

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转换器和Xcode中扮演OK(所以它不是显卡的问题);视频的路径是可以的,因为当我指定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 
+0

您是否尝试过使用其他任何视频。也许一个mp4从vimeo下载? –

+0

史蒂夫,视频真的很好,因为它在我的另一个应用程序中播放的代码完全相同。但仍然无法弄清楚差异。顺便说一句,我添加了Xcode输出。也许它会给你一些想法是什么问题。 – maxgrinev

回答

3

我解决加playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

+0

+1:这帮助了我......感谢一吨... –

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]; 
} 
+0

+1保留。太多的教程显示了临时变量的用法。 – DanneManne

13

的我只是解决了类似的问题,这行代码。播放器控制器现在出现并且视频播放完美:

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

修改为适合您的环境。

+2

在iPad/iOS6上调用“prepareToPlay”为我解决了这个问题。没有它,“MPMoviePlayerLoadStateDidChangeNotification”没有被发送,因为播放器没有预先加载视频。 – diachedelic

+1

对我来说也是这样,在iOS6上有黑屏之前(在iOS5和更低版本上工作正常)。 – Dado

+0

这也解决了我的问题,但是,当在5秒内从splitView中加载另一个视频时,我仍然可以听到前一个视频的音频,直到5秒钟之后...任何人都有这个问题?我正在考虑添加AVFoundation框架,以便我可以尝试并控制音频会话? – whyoz

-2

另外检查视频格式。我不断收到我的示例视频.m4v(我从苹果网站下载)出现此错误。最后,我用一个不同的视频剪辑.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 

但是仍然播放视频。