2012-05-18 73 views
1

我正在嘗試創建一個將顯示視頻文件的AVPlayer。使用AVFoundation播放視頻時遇到問題

這是我到目前爲止的代碼:

- (IBAction) player { 
     NSURL* m = [[NSBundle mainBundle] URLForResource:@"Cache" withExtension:@"mov"]; 
     AVURLAsset* asset = [AVURLAsset URLAssetWithURL:m options:nil]; 
     AVPlayerItem* item = [AVPlayerItem playerItemWithAsset:asset]; 
     AVPlayer* p = [AVPlayer playerWithPlayerItem:item]; 
     self.player = p; 
     AVPlayerLayer* lay = [AVPlayerLayer playerLayerWithPlayer:p]; 
     lay.frame = CGRectMake(10, 76, 303, 265); 
     [self.view.layer addSublayer:lay]; 
     [p play]; 
} 

我已經掛AVFoundation.frameworkCoreMedia.framework,我已經進口<AVFoundation/AVFoundation.h>

首先,我得到一個錯誤,說property 'player' not found on object of type 'ViewController*(線爲self.player = p;)。我需要做些什麼才能使其運行?

此錯誤修復後,此代碼將正確顯示視頻?

在此先感謝

回答

1

你的viewController顯然不包含這樣的實例變量。

要解決這個問題,以下內容添加到您的viewController頭文件(.H):

@property (nonatomic, strong) AVPlayer *player; 

然後添加以下到您的viewController實現文件(.M):

@synthesize player; 

第一步將確保你的viewController對象實際上擁有一個名爲player的實例變量。第二步將創建一個匹配的setter和getter。

+0

修復了這個問題。但是,視頻仍然不顯示。我試圖在模擬器中運行這個。任何其他想法? –

相關問題