Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法使用MPMoviePlayerViewController播放视频_Iphone_Ios_Video - Fatal编程技术网

Iphone 无法使用MPMoviePlayerViewController播放视频

Iphone 无法使用MPMoviePlayerViewController播放视频,iphone,ios,video,Iphone,Ios,Video,我用下面的ViewController.m创建了一个新项目。当我运行应用程序时,我可以看到一个预期来源/大小的框(38100250163),但它是黑色的,没有视频播放。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] [M

我用下面的ViewController.m创建了一个新项目。当我运行应用程序时,我可以看到一个预期来源/大小的框(38100250163),但它是黑色的,没有视频播放。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
#导入“ViewController.h”
#进口
@界面视图控制器()
@结束
@实现视图控制器
-(作废)电影回放完成:(NSNotification*)通知
{
MPMoviePlayerController*moviePlayerController=[通知对象];
[[NSNotificationCenter defaultCenter]移除观察者:self
名称:MPMoviePlayerPlaybackDidFinishNotification
对象:moviePlayerController];
[moviePlayerController.view从SuperView移除];
}
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
NSString*文件路径=[[NSBundle mainBundle]路径资源:@“mp4”类型的“演示iPhone”;
NSURL*fileURL=[NSURL fileURLWithPath:filepath];
MPMoviePlayerController*moviePlayerController=[[MPMoviePlayerController alloc]initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@selector(moviePlaybackComplete:)
名称:MPMoviePlayerPlaybackDidFinishNotification
对象:moviePlayerController];
[moviePlayerController.view设置帧:CGRectMake(38,
100,
250,
163)];
[self.view addSubview:moviePlayerController.view];
[moviePlayerController播放];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation!=UIInterfaceOrientation肖像向上向下);
}
@结束

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

是否使用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];
}

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

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

修改以适应您的环境。

还可以查看视频格式。我的示例视频
.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

但视频仍在播放

你试过其他的视频吗。也许是从vimeo下载的mp4?史蒂夫,视频真的很好,因为它在我的另一个应用程序中播放,代码完全相同。但仍然无法找出区别。顺便说一句,我添加了Xcode输出。也许这会让你知道问题出在哪里。在iPad/iOS6上调用“prepareToPlay”为我解决了这个问题。没有它,“MPMoviePlayerLoadStateDidChangeNotification”不会被发送,因为播放机不需要预先加载视频。在我在iOS6上出现黑屏之前(在iOS5及以下版本上工作正常)也为我修复了它。这也修复了我的问题,但是,当在5秒内从splitView加载另一个视频时,我仍然可以听到上一个视频的音频,直到5秒结束…有人有这个问题吗?我正在考虑添加AVFoundation框架,以便我可以尝试控制音频会话?+1用于保留。太多教程显示了临时变量的用法。