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

发布于 2024-12-04 18:59:40 字数 1328 浏览 0 评论 0原文

我是 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

Im new to xCode programming, ive got this code from an ebook tutorial playing mp4 xCode.

function triggered from a button

(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;
}

called from play movie function

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

Ive got no error and warning after i build it. it just output a blank screen after i click the button that triggers the playMovie function. Im confuse ive google it and still got no idea on how to solve the proble.

Im using xCode 4.2 iOS SDK 5.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-12-11 18:59:40

试试这个....将“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];

}

try this....replace "example" with the name of your file and "m4v" with the type of file e.g. "mp4" (not .mp4 and dont put the .mp4 bit in the name of the file)

-(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];

}

绝影如岚 2024-12-11 18:59:40

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

MPMoviePlayerController *moviePlayerController;

并将其添加到您的 .m 文件中。

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

如果它仍然不起作用,请尝试将其声明为 @property。

I don't know if you are still looking for the answer. Here's what you can try - declare the MPMoviePlayerController in your header(.h) file (i.e. make it a member variable).

MPMoviePlayerController *moviePlayerController;

and add this to your .m file

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

If it still does not work then try to declare it as a @property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文