0

I created a sample video app using AVPlayerViewController. It is working fine. But the problems are after finishing the video the AVPlayerViewController view is not removing. Other problem is set the constraints for AVPlayerViewController for portrait default size in landscape mode full screen. I am new for constraints. Please download the project and run the project logger shows the constraint problems.

Source code project : http://www.filedropper.com/avplayerdemos

Ramesh_T
  • 1,251
  • 8
  • 19

1 Answers1

1

I see two problem with your code:

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

this code above you add observer and remove after. so should change two:

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



                   });
}];

Problem 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;
    }
}

In this loop just check eveything load and return if have failed. In this case have 2 key, you code like this will add two childviewcontroller and it will play two item player. So change code to it:

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

I pretty sure that with your demo. Change like this, it will work ok.

vien vu
  • 4,277
  • 2
  • 17
  • 30
  • Thanks buddy. It's working. One doubt, if I written the code in AVKeyValueStatusLoaded what is the problem? as well as Can you help on constraints? – Ramesh_T Jan 08 '16 at 08:44
  • You can check with code add in AVKeyValuStatusLoaded you will see problem. About constraint you can check this question: http://stackoverflow.com/questions/17772922/can-i-use-autolayout-to-provide-different-constraints-for-landscape-and-portrait – vien vu Jan 08 '16 at 09:02
  • Hello bro @vienvu, could you help me my problem http://stackoverflow.com/questions/43132122/avplayer-rotate-view-makes-cannot-tap-to-the-screen ? – May Phyu Mar 31 '17 at 05:57