MPMoviePlayerViewController 可以工作,MPMoviePlayerController 不能..为什么?

发布于 2024-12-11 05:52:41 字数 986 浏览 0 评论 0原文

我有这段代码,

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"d" ofType:@"mp4"];  
NSURL    *url    =   [NSURL fileURLWithPath:filepath];  

//part 1
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player.view setFrame: self.view.bounds];  
//[player prepareToPlay];
//[player setShouldAutoplay:YES];
//[player setControlStyle:2];
[self.view addSubview: player.view];
[player play];

//part2
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mp];
[self presentMoviePlayerViewControllerAnimated:mp];

第 2 部分可以工作,第 1 部分则不行...他们使用相同的 url,我几乎从

我使用 ios5 的ADC 网站复制并粘贴了代码片段

I have this code

NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"d" ofType:@"mp4"];  
NSURL    *url    =   [NSURL fileURLWithPath:filepath];  

//part 1
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player.view setFrame: self.view.bounds];  
//[player prepareToPlay];
//[player setShouldAutoplay:YES];
//[player setControlStyle:2];
[self.view addSubview: player.view];
[player play];

//part2
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mp];
[self presentMoviePlayerViewControllerAnimated:mp];

the part2 works, part1 not...they are using the same url and I've almost copyed and pasted the snippet from the ADC site

I'm using ios5

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

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

发布评论

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

评论(3

み格子的夏天 2024-12-18 05:52:41

这个问题已经发布很长时间了,但答案是您必须保留 MPMoviePlayerController 变量的引用。

如果函数中有第 1 部分的代码,请在 .h 文件中声明 MPMoviePlayerController *player。如果您不这样做(并且您正在使用 ARC 进行开发),您的玩家变量将在退出函数后立即被释放。请注意,您将 player.view 添加到 self.view,因此玩家的视图会被保留,但玩家的控制器不会,并且会被释放。

所以你的 .h 文件中应该有:

MPMoviePlayerController *player;

在这种情况下,你的函数必须类似于:

-(void) playMovie
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                         pathForResource:@"mymovie" 
                                         ofType:@"mov"]];    

    player = [[MPMoviePlayerController alloc] initWithContentURL: url];
    [player.view setFrame: self.view.bounds];  
    [self.view addSubview: player.view];
    [player play];        
}

It has been a long time since this question was posted, but the answer is that you must keep a reference of the MPMoviePlayerController variable.

If you have the code of part1 in a function, declare MPMoviePlayerController *player in your .h file. If you don't do that (and your are developing with ARC) your player variable will be deallocated as soon as you exit the function. Notice that your are adding player.view to self.view, so the player's view gets retained, but the player's controller does not and it gets deallocated.

So your should have in your .h file:

MPMoviePlayerController *player;

and in this case, your function must be something like:

-(void) playMovie
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                         pathForResource:@"mymovie" 
                                         ofType:@"mov"]];    

    player = [[MPMoviePlayerController alloc] initWithContentURL: url];
    [player.view setFrame: self.view.bounds];  
    [self.view addSubview: player.view];
    [player play];        
}
菩提树下叶撕阳。 2024-12-18 05:52:41

我在 iOS 5 上使用 MPPlayerController 遇到了类似的问题,我检查了 Apple 的示例项目,区别只是设置框架,所以我手动设置框架,它工作得很好。

    [[[self moviePlayer] view] setFrame:CGRectMake(0, 0, 320, 480)];

I had a similar issue on iOS 5 with MPPlayerController and I have checked Apple's example project, the difference was only setting frame so I set frame manually and it worked perfectly.

    [[[self moviePlayer] view] setFrame:CGRectMake(0, 0, 320, 480)];
孤独陪着我 2024-12-18 05:52:41

在视频未加载之前,您无法将播放器视图添加到主视图。

因此,您应该将这一行 : 替换

[self.view addSubview: player.view];

为这一行 :

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(movieLoadStateDidChange:) 
    name:MPMoviePlayerLoadStateDidChangeNotification 
    object:player];

并添加此方法:

-(void)movieLoadStateDidChange: (NSNotification*)notification{
    if (player.loadState & MPMovieLoadStatePlayable == MPMovieLoadStatePlayable) {
        [[NSNotificationCenter defaultCenter] 
            removeObserver:self 
            name:MPMoviePlayerLoadStateDidChangeNotification 
            object:player] ; 
        [self.view addSubview:player.view];
    }
}

You can't add the player view to the main view until the video is not loaded.

So you should replace this line :

[self.view addSubview: player.view];

By this one :

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(movieLoadStateDidChange:) 
    name:MPMoviePlayerLoadStateDidChangeNotification 
    object:player];

And add this method:

-(void)movieLoadStateDidChange: (NSNotification*)notification{
    if (player.loadState & MPMovieLoadStatePlayable == MPMovieLoadStatePlayable) {
        [[NSNotificationCenter defaultCenter] 
            removeObserver:self 
            name:MPMoviePlayerLoadStateDidChangeNotification 
            object:player] ; 
        [self.view addSubview:player.view];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文