简体   繁体   English

在Mac OS X 10.7中播放视频

[英]Play video in Mac OS X 10.7

What is the simplest way to play a video programmatically with Objective-C in Mac OS X 10.7 (Lion)? 在Mac OS X 10.7(Lion)中使用Objective-C以编程方式播放视频的最简单方法是什么? And if I want to support OS X 10.6 (Snow Leopard) too? 如果我也想支持OS X 10.6(Snow Leopard)呢?

I noticed that iOS AV Foundation was introduced to OS X 10.7. 我注意到iOS AV Foundation已经引入了OS X 10.7。 Unfortunately the documentation seems to be written for iOS and I found it confusing. 不幸的是,文档似乎是为iOS编写的,我发现它令人困惑。

Here's a NSView subclass that plays a video given a URL, using AV Foundation (thus Mac OS X 10.7 upwards only). 这是一个NSView子类,使用AV Foundation(仅限Mac OS X 10.7)播放给定URL的视频。 Based on the AVSimplePlayer sample code. 基于AVSimplePlayer示例代码。

Header: 标题:

@interface RMVideoView : NSView

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

- (void) play;

@end

Implementation: 执行:

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) dealloc {
    [self.player pause];
    [self removeObserver:self forKeyPath:@"player.currentItem.status"];
    [self removeObserver:self forKeyPath:@"playerLayer.readyForDisplay"];
    [_player release];
    [_playerLayer release];
    [_videoURL release];
    [super dealloc];
}

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

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

    AVURLAsset *asset = [AVAsset assetWithURL:self.videoURL];
    NSArray *assetKeysToLoadAndTest = [NSArray arrayWithObjects:@"playable", @"hasProtectedContent", @"tracks", @"duration", nil];
    [asset loadValuesAsynchronouslyForKeys:assetKeysToLoadAndTest completionHandler:^(void) {
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [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
}

- (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 = YES;
        [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

"Simplest" depends on exactly what you're trying to do. “最简单”取决于您正在尝试做什么。 If you want more control (eg, rendering the movie as an OpenGL texture) or less (eg, a completely independent window that you can just pop up and ignore), there might be different answers. 如果您想要更多控制(例如,将电影渲染为OpenGL纹理)或更少(例如,您可以弹出并忽略的完全独立的窗口),可能会有不同的答案。

But for most use cases, if you want 10.6+ support, the simplest way to show a movie is QTKit. 但对于大多数用例,如果你想要10.6+支持,最简单的电影放映方式是QTKit。 See the article "Using QTKit for Media Playback" in the Xcode documentation for a good starting point. 请参阅Xcode文档中的文章“使用QTKit进行媒体播放”,以获得良好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM