MPMoviePlayer 播放视频后出现错误访问错误

发布于 2024-10-11 04:24:58 字数 2412 浏览 4 评论 0原文

我创建了一个新的 ViewController(仅包含 .h 和 .m 文件)并添加了该代码来播放视频。视频播放完毕后,我收到“Exe_bad_access”错误。

将“NSZombieEnabled=true”添加到可执行文件作为参数时出现错误消息:


“测试播放视频[654:207] -[MPMoviePlayerController stop]:消息发送到已释放的实例 0x63042d0"


这是什么问题?播放视频时如何进行正确的内存管理?

#import "TestPlayingVideoViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@implementation TestPlayingVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor darkGrayColor]];


    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(50 , 50, 200, 25)];
    [btn setTitle:@"press me" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(action:)    forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];
    [btn release];
}

- (void)action:(id)sender
{
    NSLog(@"UIButton was clicked");

     NSString *url   =   [[NSBundle mainBundle] pathForResource:@"mymovie" ofType:@"m4v"];
     MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ];

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController.moviePlayer];
     [moviePlayerController.moviePlayer play];

     //[self.view addSubview:moviePlayerController.view];
    [self presentMoviePlayerViewControllerAnimated:moviePlayerController];
}


- (void) moviePlayBackComplete:(NSNotification*) notification {

    MPMoviePlayerController* player = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; 
    [self dismissMoviePlayerViewControllerAnimated];

    [player stop];
    //[self.view removeFromSuperView];
    [player release];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

I´ve created an new ViewController (only with the .h and .m file) and added that code to play a video. After the video has finished, i get a "Exe_bad_access" error.

Error message when adding "NSZombieEnabled=true" to the excecutable as a argument:


"TestPlayingVideo[654:207]
-[MPMoviePlayerController stop]: message sent to deallocated instance
0x63042d0"


Whats wrong with that? How can i do correct memory management when playing video?

#import "TestPlayingVideoViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@implementation TestPlayingVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor darkGrayColor]];


    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(50 , 50, 200, 25)];
    [btn setTitle:@"press me" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(action:)    forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];
    [btn release];
}

- (void)action:(id)sender
{
    NSLog(@"UIButton was clicked");

     NSString *url   =   [[NSBundle mainBundle] pathForResource:@"mymovie" ofType:@"m4v"];
     MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ];

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController.moviePlayer];
     [moviePlayerController.moviePlayer play];

     //[self.view addSubview:moviePlayerController.view];
    [self presentMoviePlayerViewControllerAnimated:moviePlayerController];
}


- (void) moviePlayBackComplete:(NSNotification*) notification {

    MPMoviePlayerController* player = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; 
    [self dismissMoviePlayerViewControllerAnimated];

    [player stop];
    //[self.view removeFromSuperView];
    [player release];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

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

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

发布评论

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

评论(1

待"谢繁草 2024-10-18 04:24:58

对于您要发布的内容,这里存在很多混乱:例如,这是您的电影播放器​​的主要分配:

 MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ];

但是您要发布的不是这个 moviePlayerController - 您只是发布了 . MPMoviePlayerController 的 moviePlayer 属性。请注意,当您创建 NSNotification 时,您将传递 moviePlayerController.movi​​ePlayer,而不是简单地传递 moviePlayerController。

因此,您没有释放 moviePlayerController,实际上是在尝试释放该对象的属性。你不应该这样做 - 你应该释放该对象,并让它担心释放其属性。

There's a lot of confusion here over what you're releasing: for example, here's your main alloc of your movie player:

 MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ];

But what you are releasing isn't this moviePlayerController - you're only releasing the .moviePlayer property of your MPMoviePlayerController. Notice when you create your NSNotification you're passing moviePlayerController.moviePlayer, not simply moviePlayerController.

So you're not releasing your moviePlayerController, you're in fact attempting to release a property of that object. Which you shouldn't do - you should release the object, and let it worry about releasing its properties.

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