MPMoviePlayerController垃圾邮件'收到内存警告'

时间:2014-04-16 19:52:21

标签: ios objective-c memory-management mpmovieplayercontroller

我添加了MPMoviePlayerController作为背景视图,当它播放时,我收到了日志消息的垃圾邮件'已收到内存警告'。我现在不知道为什么,也许有解决方法或更好的解决方案。

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

日志说明了一切。您收到一条打印警告,表示您使用的内存过多,如果您没有腾出空间,您的应用程序将会关闭。您需要立即采取措施降低记忆力,所以不要轻视这些警告。是的,你的应用程序可能不会立即崩溃,但它通常表明代码设置中存在更大的问题。

在XCode中运行分配工具,以查看大部分内存的使用位置。我首先要检查那个m4v视频的大小。如果视频数量很大,您应该将视频流式传输。此外,请确保使用Leaks仪器不会泄漏内存。但是,当您收到didReceiveMemoryWarning回调时,请立即采取措施。在AppDelegate中选择通知或订阅UIApplicationDidReceiveMemoryWarningNotification并发布可以在以后重新创建的项目/ viewControllers。

如果你想咨询,这是Memory Management Guide

答案 1 :(得分:0)

首先向@tdevoy索取您的建议。

我终于摆脱了警告。这是文件类型!我不得不将其转换为.3gp,现在它工作得更顺畅,没有任何警告。

奇怪的是,我现在使用的内存比以前多了4mb。但一切都很好......