0

我使用AVPlayerViewController. 它工作正常。但问题是在完成视频后,AVPlayerViewController视图并没有被删除。AVPlayerViewController其他问题是在横向模式全屏中设置纵向默认大小的约束。我是新来的约束。请下载项目并运行项目记录器显示约束问题。

源代码项目:http ://www.filedropper.com/avplayerdemos

4

1 回答 1

1

我发现您的代码有两个问题:

asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler: ^{
    dispatch_async(
                   dispatch_get_main_queue(), ^{
                       if (!asset.playable) {
                           return;
                       } else {
                           [self prepareToPlayAsset: asset withRequestedKeys: requestedKeys];
                       }

                       if (videoPlayerItem) {
                           [videoPlayerItem removeObserver:self forKeyPath:kkStatusKey];
                           [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                           name:AVPlayerItemDidPlayToEndTimeNotification
                                                                         object: videoPlayerItem];
                       }
                   });
}];

上面的这段代码添加了观察者并在之后删除。所以应该改变两个:

[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler: ^{
    dispatch_async(
                   dispatch_get_main_queue(), ^{

                       if (videoPlayerItem) {
                           [videoPlayerItem removeObserver:self forKeyPath:kkStatusKey];
                           [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                           name:AVPlayerItemDidPlayToEndTimeNotification
                                                                         object: videoPlayerItem];
                       }


                       if (!asset.playable) {
                           return;
                       } else {

                           [self prepareToPlayAsset: asset withRequestedKeys: requestedKeys];
                       }



                   });
}];

问题2:

- (void)prepareToPlayAsset: (AVURLAsset *)asset withRequestedKeys: (NSArray *)requestedKeys {
     for (NSString *thisKey in requestedKeys) {
        NSError *error = nil;
        AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];

        switch (keyStatus) {
            case AVKeyValueStatusUnknown:
                NSLog(@"%@ AVKeyValueStatusUnknown", thisKey);
            break;

            case AVKeyValueStatusFailed:
                NSLog(@"Error! PlayAsset failed.\nAVKey : %@.\nError: %@", thisKey, error);
                return;
            break;

            case AVKeyValueStatusLoading:
                NSLog(@"%@ AVKeyValueStatusLoading", thisKey);
            break;
            case AVKeyValueStatusCancelled:
                NSLog(@"%@ AVKeyValueStatusCancelled", thisKey);
            break;
            case AVKeyValueStatusLoaded: {

                videoPlayerItem = [AVPlayerItem playerItemWithAsset: asset];
                [videoPlayerItem addObserver:self forKeyPath: kkStatusKey options:0 context:nil];

                videoPlayer = [AVPlayer playerWithPlayerItem: videoPlayerItem];

                /**
                 * Creating the videoAdplayer through passing the avplayer object
                */
                [self createVideoPlayer: videoPlayer];

                if ([thisKey isEqualToString: @"duration"]) {

                } else if ([thisKey isEqualToString: @"tracks"]) {
                    NSLog(@"\n\n asset.tracks : %@ \n\n", asset.tracks);
                } else if ([thisKey isEqualToString: @"metadata"]) {
                    NSLog(@"\n\n assetMetadata : %@ \n\n", asset.metadata);
                }
            }
            break;

            default:
            break;
         }
    }

    if (!asset.playable) {
        return;
    }
}

在这个循环中,只需检查一切负载,如果失败则返回。在这种情况下,有 2 个键,您这样的代码将添加两个childviewcontroller,它将播放两个项目播放器。因此,将代码更改为:

- (void)prepareToPlayAsset: (AVURLAsset *)asset withRequestedKeys: (NSArray *)requestedKeys {
   for (NSString *thisKey in requestedKeys) {
        NSError *error = nil;
        AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];

        switch (keyStatus) {
            case AVKeyValueStatusUnknown:
                NSLog(@"%@ AVKeyValueStatusUnknown", thisKey);
            break;

            case AVKeyValueStatusFailed:
                NSLog(@"Error! PlayAsset failed.\nAVKey : %@.\nError: %@", thisKey, error);
                return;
            break;

            case AVKeyValueStatusLoading:
                 NSLog(@"%@ AVKeyValueStatusLoading", thisKey);
            break;
            case AVKeyValueStatusCancelled:
                NSLog(@"%@ AVKeyValueStatusCancelled", thisKey);
            break;
            case AVKeyValueStatusLoaded: {


            }
            break;

            default:
            break;
        }
     }

     videoPlayerItem = [AVPlayerItem playerItemWithAsset: asset];
     [videoPlayerItem addObserver:self forKeyPath: kkStatusKey options:0 context:nil];

    videoPlayer = [AVPlayer playerWithPlayerItem: videoPlayerItem];

    /**
     * Creating the videoAdplayer through passing the avplayer object
     */
     [self createVideoPlayer: videoPlayer];



    if (!asset.playable) {
        return;
    }
}

我很确定你的演示。像这样改变,它会工作的。

于 2016-01-08T07:34:21.097 回答