首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内存泄漏: AVplayerViewController

内存泄漏: AVplayerViewController
EN

Stack Overflow用户
提问于 2016-06-11 11:06:18
回答 1查看 1.7K关注 0票数 4

当我们播放视频并返回到父窗口时,我正面临内存泄漏。请参阅下面的分配工具读数的屏幕截图。

每次我弹出视图控制器(显示视频)时,都会有一些与AVFoundation相关的对象保存内存。有趣的是,所有这些对象的负责任库是AVFoundation。内存的增加不是由于应用程序中创建的对象造成的。这种流行的框架不太可能有问题。我在网上看到了一些AVPlayerViewController的例子,但它们似乎也有同样的问题。

有人知道问题出在哪里吗?如果有人想要复制这一点,那么他可以下载上面给出的两个项目中的任何一个。为了使用导航控制器创建根视图控制器,您必须在情节提要中做一些小的更改。

  • 黑子/index.html
  • https://github.com/coolioxlr/PageView-AVPlayer 我就是这样清除记忆的: -(void) dealloc{ self clearCurrentVideo;} -(void)clearCurrentVideo { _playerItem Remove观察者:self forKeyPath:@"status";_currentVideoPlayerViewController.player Remove观察者:self forKeyPath:@"rate";_currentVideoPlayerViewController.player P停顿;_currentVideoPlayerViewController.player _currentVideoPlayerViewController.player=nil;_playerItem=nil;_currentVideoPlayerViewController = nil;}

我就是这样为视频加载资产的:

代码语言:javascript
复制
 -(void)playtheAsset:(AVAsset *)asset{
[asset loadValuesAsynchronouslyForKeys:@[@"playable"] completionHandler:
                 ^{
                     dispatch_async( dispatch_get_main_queue(),
                                    ^{

                                        [self loadTheAsset:asset withKeys:@[@"playable"]];
                                    });
                 }];
}


- (void)loadTheAsset:(AVAsset *)asset withKeys:(NSArray *)requestedKeys{

    /* Make sure that the value of each key has loaded successfully. */
    for (NSString *thisKey in requestedKeys)
    {
        NSError *error = nil;
        AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
        if (keyStatus == AVKeyValueStatusFailed)
        {

            //[self assetFailedToPrepareForPlayback:error];
            if([thisKey isEqualToString:@"playable"]){

                 [self showNetworkErrorLabel];

            }

            return;
        } else if ((keyStatus == AVKeyValueStatusLoaded) || ( keyStatus == AVKeyValueStatusLoading )){

            [self removeNetworkLabel ];

        }
       }

    /* Use the AVAsset playable property to detect whether the asset can be played. */
    if (!asset.playable)
    {
        /* Generate an error describing the failure. */
        NSString *localizedDescription = NSLocalizedString(@"Item cannot be played", @"Item cannot be played description");
        NSString *localizedFailureReason = NSLocalizedString(@"The assets tracks were loaded, but could not be made playable.", @"Item cannot be played failure reason");
        NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                   localizedDescription, NSLocalizedDescriptionKey,
                                   localizedFailureReason, NSLocalizedFailureReasonErrorKey,
                                   nil];
        NSError *assetCannotBePlayedError = [NSError errorWithDomain:@"StitchedStreamPlayer" code:0 userInfo:errorDict];

        NSLog(@"%@",assetCannotBePlayedError);
        [self showNetworkErrorLabel];
        /* Display the error to the user. */
        [self assetFailedToPrepareForPlayback:assetCannotBePlayedError];

        return;
    }

    /* At this point we're ready to set up for playback of the asset. */

    /* Stop observing our prior AVPlayerItem, if we have one. */
    if (_playerItem)
    {
        /* Remove existing player item key value observers and notifications. */

        [_playerItem removeObserver:self forKeyPath:@"status"];

        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:AVPlayerItemDidPlayToEndTimeNotification
                                                      object:_playerItem];

        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:AVPlayerItemPlaybackStalledNotification
                                                      object:_playerItem];


    }


    /* Create a new instance of AVPlayerItem from the now successfully loaded AVAsset. */
    _playerItem = [AVPlayerItem playerItemWithAsset:asset];

    /* Observe the player item "status" key to determine when it is ready to play. */
    [_playerItem addObserver:self
                  forKeyPath:@"status"
                     options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                     context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];

    /* When the player item has played to its end time we'll toggle
     the movie controller Pause button to be the Play button */
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:_playerItem];



       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemFailedToPlayToEndTime:) name:AVPlayerItemPlaybackStalledNotification object:_playerItem];



    // Remove the movie player view controller from the "playback did finish" notification observers
    // Observe ourselves so we can get it to use the crossfade transition
    [[NSNotificationCenter defaultCenter] removeObserver:_currentVideoPlayerViewController
                                                    name:kPlayerViewDismissedNotification
                                                  object:_currentVideoPlayerViewController.player];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(videoFinishedCallback:)
                                                 name:kPlayerViewDismissedNotification
                                               object:_currentVideoPlayerViewController.player];



    /* Create new player, if we don't already have one. */
    if (!_currentVideoPlayerViewController.player)
    {
        /* Get a new AVPlayer initialized to play the specified player item. */
        _currentVideoPlayerViewController.player=[AVPlayer playerWithPlayerItem:self->_playerItem];


         [_currentVideoPlayerViewController.player addObserver:self
         forKeyPath:@"rate"
         options:NSKeyValueObservingOptionNew
         context:AVPlayerDemoPlaybackViewControllerRateObservationContext];

    }


}
EN

回答 1

Stack Overflow用户

发布于 2016-07-06 11:47:35

我不知道这背后的原因。我尝试使用AVPlayer,并创建了自己的UI使用(参考苹果AVPlayer演示应用程序。),但我没有发现任何漏洞。只是起作用了。如果有人遇到类似的问题,只需尝试引用AVPlayer演示应用程序的代码即可。如果有人知道这个问题的答案,我在这条线上。请让我知道。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37763004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档