简体   繁体   English

MPMoviePlayerController垃圾邮件“收到的内存警告”

[英]MPMoviePlayerController spams 'Received memory warning'

I'm adding a MPMoviePlayerController as background view and while it plays I get spams of the log-message 'Received memory warning'. 我正在将MPMoviePlayerController添加为背景视图,并且在播放时会收到日志消息“接收到的内存警告”的垃圾邮件。 I don't now why, and maybe there is a workaround or a better solution. 我现在不知道为什么,也许有解决方法或更好的解决方案。

Here is my code: 这是我的代码:

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [[self navigationController]setNavigationBarHidden:YES animated:YES];

    [moviePlayer play];

}

- (void) viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];

    [moviePlayer pause];

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    //self.view.backgroundColor = [UIColor appStyleLightOrangeColor];

    //Add Video playback
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"happy-female-friends-smartphon" ofType:@"m4v"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    moviePlayer.controlStyle = MPMovieControlStyleNone;
    moviePlayer.shouldAutoplay = YES;
    moviePlayer.repeatMode = MPMovieRepeatModeOne;
    moviePlayer.fullscreen =  YES;
    moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
    //set the frame of movie player
    moviePlayer.view.frame = self.view.bounds;


    [self.view insertSubview:moviePlayer.view atIndex:0];


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appBecameActive)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];

    [self performSelector:@selector(animationCode) withObject:nil afterDelay:0.1f];
}

-(void)appBecameActive{
    [moviePlayer play];
}

The log says it all. 日志说明了一切。 You are receiving a printed warning that you are using too much memory and your app will shutdown if you do not free up space. 您将收到打印警告,表示您使用了过多的内存,并且如果不释放空间,则应用程序将关闭。 You need to take immediate steps to lower your memory line, so don't just shrug off those warnings. 您需要立即采取措施降低内存容量,因此不要仅仅忽略这些警告。 Yeah, your app might not crash immediately, but it is often a sign of a much bigger problem in your code setup. 是的,您的应用程序可能不会立即崩溃,但这通常是代码设置中更大问题的征兆。

Run the Allocations Instrument in XCode to see where the bulk of your memory is being used. 在XCode中运行“分配工具”,以查看在哪里使用了您的大量内存。 I'd first check the size of that m4v video also. 我首先还要检查该m4v视频的大小。 You should be streaming the video if it is a significant size. 如果视频很大,则应该流式传输视频。 Additionally, ensure that you are not leaking memory using the Leaks instrument. 此外,请确保您没有使用Leaks仪器泄漏内存。 But once again, when you receive the didReceiveMemoryWarning callback, take immediate action. 但是,当再次收到didReceiveMemoryWarning回调时,请立即执行操作。 Either pick up the notification in the AppDelegate or subscribe to the UIApplicationDidReceiveMemoryWarningNotification and release items/viewControllers that can be recreated later. 可以在AppDelegate获取通知,也可以订阅UIApplicationDidReceiveMemoryWarningNotification并发布可以在以后重新创建的项目/ viewController。

Here's the Memory Management Guide if you want to consult. 如果您想参考的话,这是《 内存管理指南》

Thannks first to @tdevoy for your advices. 首先向@tdevoy表示感谢。

I finally got rid of the warning. 我终于摆脱了警告。 It was the file type! 那是文件类型! I had to convert it to .3gp and now it works much smoother and without warnings. 我不得不将其转换为.3gp ,现在它可以更流畅地运行且没有警告。

On strange thing is I even use now 4mb more memory than before. 奇怪的是,我现在甚至比以前使用了4mb的内存。 But everything works great.. 但是一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM