2013-06-18 42 views
1

我看到一個類似的問題,我正在問如何將AVPlayerLayer添加到NS視圖,但我看不到有完整的源代碼來了解它是如何工作的。我的問題的核心是我創建一個球員對象,並通過移動NSURL來初始化它。我創建一個AVPlayerLayer對象並用播放器初始化它。當我嘗試將AVPlayerLayer添加到自己(我是NSView)並運行該程序時,我什麼都沒有收到。開始圖形...添加AVPlayerLayer到NSView

NSURL *movie = [[NSURL alloc]  initWithString:@"file://localhost/Users/matthewmichal/Desktop/A%20Good%20To%20Die%20Hard.mov"]; 
[player initWithURL:movie]; 
[self setWantsLayer:YES]; 
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; 
[self.layer addSublayer:playerLayer]; 
    [player play]; 

回答

2

你或許應該考慮讀從蘋果AVFoundation指南:

https://developer.apple.com/library/ios/DOCUMENTATION/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html

它並不專指進入細微的差別是了AppKit和UIKit之間什麼,但是你已經有它([NSView setWantsLayer:],[NSView addSublayer])。什麼你是不是做正確的註冊,以觀察更新到AVPlayer的狀態屬性:

與AVAsset,雖然只是初始化播放器的項目並不一定意味着它已經準備好立即播放。您可以觀察(使用鍵值觀察)一個項目的狀態屬性來確定是否以及何時準備播放。

假設所有其他的事情都是正確的(這是很難從你的代碼告訴 - 例如,你肯定被正確地形成NSURL?),這是最有可能出了什麼問題 - 你要添加的層在玩家準備好玩之前的NSView。如果沒有,那麼我會確保你的NSURL實際上指向你正在尋找的資源。

0

進口AVFoundation

#import <AVFoundation/AVFoundation.h> 

創建AVPlayer對象和玩家層(我在標題中定義這些)

AVPlayer *avPlayer; 
AVPlayerLayer* playerLayer; 

告訴的NSView要使用層,假設在你的班級的NSView

[self setWantsLayer:YES]; 

現在的功能...

[avPlayer pause]; 
avPlayer = nil; 
playerLayer = nil; 

NSURL *videoURL = [NSURL fileURLWithPath:@"file://localhost/Users/matthewmichal/Desktop/A%20Good%20To%20Die%20Hard.mov"]; 
avPlayer = [AVPlayer playerWithURL:videoURL]; 
playerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer]; 
playerLayer.frame = self.frame;//or your desired CGRect 
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
//playerLayer.masksToBounds = YES; 
[self.layer addSublayer:playerLayer]; 

[avPlayer play];