AVPlayer does not show video

非 Y 不嫁゛ 提交于 2019-12-12 01:47:39

问题


I am trying to play a movie at the beginning of my game. I am using AVPlayer to do this. My problem is, when I register a KVO to check the status of my AVPlayer, my game proceeds as usual without waiting for the video to load and finish. As a result, I can only hear the audio from my .mov file and can't see any video (since my game has already started). I would like the video to load and finish before proceeding with the game. Here's the code:

    @interface RMVideoView : NSView
{
    NSURL* _videoURL;
    AVPlayer* _player;
    AVPlayerLayer* _playerLayer;
}

@property (nonatomic, readonly, strong) AVPlayer* player;
@property (nonatomic, readonly, strong) AVPlayerLayer* playerLayer;
@property (nonatomic, retain) NSURL* videoURL;
 - (void) play;
@end

static void *RMVideoViewPlayerLayerReadyForDisplay = &RMVideoViewPlayerLayerReadyForDisplay;
static void *RMVideoViewPlayerItemStatusContext = &RMVideoViewPlayerItemStatusContext;

@interface RMVideoView()

- (void)onError:(NSError*)error;
- (void)onReadyToPlay;
- (void)setUpPlaybackOfAsset:(AVAsset *)asset withKeys:(NSArray *)keys;

@end

@implementation RMVideoView

@synthesize player = _player;
@synthesize playerLayer = _playerLayer;
@synthesize videoURL = _videoURL;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.wantsLayer = YES;
        _player = [[AVPlayer alloc] init];
        [self addObserver:self forKeyPath:@"player.currentItem.status" options:NSKeyValueObservingOptionNew context:RMVideoViewPlayerItemStatusContext];
    }

    return self;
}

- (void) setVideoURL:(NSURL *)videoURL {
    _videoURL = videoURL;

    [self.player pause];
    [self.playerLayer removeFromSuperlayer];

    AVURLAsset *asset = [AVAsset assetWithURL:self.videoURL];

    [asset retain];


    NSArray *assetKeysToLoadAndTest = [NSArray arrayWithObjects:@"playable", @"hasProtectedContent", @"tracks", @"duration", nil];
    [asset loadValuesAsynchronouslyForKeys:assetKeysToLoadAndTest completionHandler:^{
        dispatch_async(dispatch_get_main_queue(),^{
            [self setUpPlaybackOfAsset:asset withKeys:assetKeysToLoadAndTest];
        });
    }];
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == RMVideoViewPlayerItemStatusContext)
    {
        AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
        switch (status)
        {
            case AVPlayerItemStatusUnknown:
                break;
            case AVPlayerItemStatusReadyToPlay:
                [self onReadyToPlay];
                break;
            case AVPlayerItemStatusFailed:
                [self onError:nil];
                break;
        }
    }
    else if (context == RMVideoViewPlayerLayerReadyForDisplay)
    {
        if ([[change objectForKey:NSKeyValueChangeNewKey] boolValue])
        {
            self.playerLayer.hidden = NO;
        }
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark - Private

- (void)onError:(NSError*)error {
    // Notify delegate
}

- (void)onReadyToPlay {
    // Notify delegate
    [self.player play];
}

- (void)setUpPlaybackOfAsset:(AVAsset *)asset withKeys:(NSArray *)keys {
    for (NSString *key in keys) {
        NSError *error = nil;
        if ([asset statusOfValueForKey:key error:&error] == AVKeyValueStatusFailed) {
            [self onError:error];
            return;
        }
    }

    if (!asset.isPlayable || asset.hasProtectedContent) {
        [self onError:nil];
        return;
    }

    if ([[asset tracksWithMediaType:AVMediaTypeVideo] count] != 0)
    {   // Asset has video tracks
        _playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
        self.playerLayer.frame = self.layer.bounds;
        self.playerLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
        self.playerLayer.hidden = NO;
        [self.layer addSublayer:self.playerLayer];
        [self addObserver:self forKeyPath:@"playerLayer.readyForDisplay" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:RMVideoViewPlayerLayerReadyForDisplay];

    }

    // Create a new AVPlayerItem and make it our player's current item.
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
    [self.player replaceCurrentItemWithPlayerItem:playerItem];
}
#pragma mark - Public

- (void) play {
    [self.player play];
}

@end

I am calling the above code from my entry function's -(void)drawView method this way:

-(void)drawView
{
   if(playVideo)
    {
        RMVideoView *rmVid = [[RMVideoView alloc]init];

        NSURL* MovieURL;
        NSBundle *bundle = [NSBundle mainBundle];
        if(bundle != nil)
        {
            NSString *moviePath = [bundle pathForResource:@"MyVideoResource" ofType:@"mov"];
            if (moviePath)
            {
                MovieURL = [NSURL fileURLWithPath:moviePath];
                [MovieURL retain];
                [rmVid setVideoURL:MovieURL];
            }
        }  
        playVideo = kFalse;
    }
}

The call made to [rmVid setVideoURL:MovieURL] returns when KVO is setup and the game runs forward. Please help!


回答1:


You can listen for a notification, when the video playback reaches the end like this:

         AVPlayerItem  *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
         [[NSNotificationCenter defaultCenter] addObserver:self
                                                  selector:@selector(playerItemDidReachedEnd:)
                                                      name:AVPlayerItemDidPlayToEndTimeNotification
                                                    object:avPlayerItem];


来源:https://stackoverflow.com/questions/24769891/avplayer-does-not-show-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!