使用 tabbarcontroller 时释放 MPMoviePlayer

发布于 2024-10-20 11:18:44 字数 1496 浏览 1 评论 0原文

我正在使用 tabbarcontroller,其中视图之一具有 MPMoviePlayer。它工作正常,除了如果我更改选项卡,电影不会停止并继续在后台播放。然后,如果我尝试返回电影选项卡,它就会崩溃。

我认为我必须释放 MPMoviePlayer 的唯一代码是在播放完毕时,但我希望在更改视图时释放它。然后,如果我返回“电影”选项卡,我们就会重新开始。

在我的 .h 文件中设置为:


import < UIKit/UIKit.h>

import < MediaPlayer/MediaPlayer.h>


@interface SecondViewController : UIViewController {
    MPMoviePlayerController *player;
}

@end  

在我的 .m 文件中设置为: 有


- (void)viewDidLoad {

 NSString *url = [[NSBundle mainBundle]
 pathForResource:@"vid"
 ofType:@"m4v"];

 player = [[MPMoviePlayerController alloc]
 initWithContentURL:[NSURL fileURLWithPath:url]];

 [[NSNotificationCenter defaultCenter]

 addObserver:self
 selector:@selector(movieFinishedCallback:)
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];

 //--called when the movie view and then add it to the View window--
 player.view.frame = CGRectMake(10, 10, 300, 300);
 [self.view addSubview:player.view];

    //--play movie--
    [player pause];

    [super viewDidLoad];
}

//--called when the movie is done playing--
- (void) movieFinishedCallback:(NSNotification*) aNotification {
    MPMoviePlayerController *moviePlayer = [aNotification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:moviePlayer];
    [moviePlayer.view removeFromSuperview];
    [player release];
}

什么建议吗?谢谢 :)

I'm using a tabbarcontroller in which one of the views has an MPMoviePlayer. It works fine, except that if I change tab, the movie doesn't stop and keeps playing in the background. Then if I try to tab back to the movie tab, it crashes.

I think the only code I have to release the MPMoviePlayer is when it's finished playing, but I want it to be released when I change views instead. Then if I go back to the Movie tab, we start fresh.

In my .h file have set up as:


import < UIKit/UIKit.h>

import < MediaPlayer/MediaPlayer.h>


@interface SecondViewController : UIViewController {
    MPMoviePlayerController *player;
}

@end  

and in my .m file have:


- (void)viewDidLoad {

 NSString *url = [[NSBundle mainBundle]
 pathForResource:@"vid"
 ofType:@"m4v"];

 player = [[MPMoviePlayerController alloc]
 initWithContentURL:[NSURL fileURLWithPath:url]];

 [[NSNotificationCenter defaultCenter]

 addObserver:self
 selector:@selector(movieFinishedCallback:)
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];

 //--called when the movie view and then add it to the View window--
 player.view.frame = CGRectMake(10, 10, 300, 300);
 [self.view addSubview:player.view];

    //--play movie--
    [player pause];

    [super viewDidLoad];
}

//--called when the movie is done playing--
- (void) movieFinishedCallback:(NSNotification*) aNotification {
    MPMoviePlayerController *moviePlayer = [aNotification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:moviePlayer];
    [moviePlayer.view removeFromSuperview];
    [player release];
}

Any suggestions? Thank you :)

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

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

发布评论

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

评论(1

飘逸的'云 2024-10-27 11:18:44

如果您确实想在选项卡切换时释放 MPMoviePlayer,请在 viewWillDisappearviewDidDisappear 中执行。现在,正如您所描述的,它仍然活跃在后台。当您返回选项卡时,您尝试再次创建它。

很难说坠机的确切原因是什么,似乎有几种可能性。下次用调用堆栈写一个“为什么会崩溃”问题。

也许您可以考虑只是暂停/恢复,这样您就不需要在每次用户更改选项卡时重新分配新的 moviePlayer ?在 viewDidLoad 和 viewDidUnload 中进行分配/释放,但在 viewWillAppear 和 viewWillDisappear 中进行播放/暂停。

If you really want to release MPMoviePlayer at tab switch, then do it in viewWillDisappear or viewDidDisappear. Now it's left alive at background, as you described. When you come back to tab, you try to create it again.

Difficult to say what would be the exact reason for crash, there seems to be several possibilities. Next time write a "Why did this crash" question with a call stack.

Maybe you could think about just pause/resume, so you wouldn't need to reallocate new moviePlayer every time user changes tabs? Do alloc/release in viewDidLoad and viewDidUnload, but play/pause in viewWillAppear and viewWillDisappear.

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