视频无法在iPhone 5.0模拟器上播放

时间:2011-09-16 09:58:57

标签: iphone ios xcode ipad video

我是xCode编程的新手,我从mp4 xCode的电子书教程中获得了这段代码。

从按钮触发的功能

(IBAction)playMovie:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"videoSample" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view]; 
moviePlayerController.fullscreen=YES;
[moviePlayerController play];
moviePlayerController.scalingMode = MPMovieScalingModeFill;
}

从播放电影功能调用

(void)moviePlaybackComplete:(NSNotification *)notification{
MPMoviePlayerController *moviePlayerController = [notification
                                  object];
[[NSNotificationCenter defaultCenter]removeObserver:self
                                 name:MPMoviePlayerPlaybackDidFinishNotification
                               object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
}

我构建它后没有错误和警告。单击触发playMovie功能的按钮后,它只输出一个空白屏幕。我混淆了谷歌它仍然不知道如何解决问题。

我正在使用xCode 4.2 iOS SDK 5.0

2 个答案:

答案 0 :(得分:1)

尝试这个....将“example”替换为您的文件名称,将“m4v”替换为文件类型,例如“mp4”(不是.mp4,不要将.mp4位放在文件名中)

-(IBAction)playVideo:(id)sender;
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"m4v"];


MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];

if (tmpMoviePlayViewController) {

    tmpMoviePlayViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    [self presentModalViewController:tmpMoviePlayViewController animated:YES];

    tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

    [[NSNotificationCenter defaultCenter] addObserver:self

                                        selector:@selector(myMovieViewFinishedCallback:)

                                        name:MPMoviePlayerPlaybackDidFinishNotification

                                        object:tmpMoviePlayViewController];

    [tmpMoviePlayViewController.moviePlayer play];
}

}

-(void)myMovieFinishedCallback:(NSNotification*)theNotification
{
    MPMoviePlayerController *moviePlayer=[theNotification object];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:moviePlayer];

[moviePlayer.view removeFromSuperview];

}

答案 1 :(得分:0)

我不知道你是否还在寻找答案。以下是您可以尝试的内容 - 在标题(.h)文件中声明 MPMoviePlayerController (即使其成为成员变量)。

MPMoviePlayerController *moviePlayerController;

并将其添加到.m文件

moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

如果仍然无效,请尝试将其声明为@property。

相关问题