2014-09-04 154 views
1

我正在开发一个需要在应用程序中播放教程视频的mac应用程序。当用户按下应用按钮应该发挥的视频 这里是对的Xcode 4.3代码在OSX中播放视频

@synthesize movieView; 
-(IBAction)playVideo:(id)sender { 
NSString *videoPath = [[NSBundle mainBundle] pathForResource:"video name" ofType:@"mp4"; 
NSURL *videoURL = [NSURL fileURLWithPath: videoPath]; 
NSError *error = nil; 

NSDictionary *attrib = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], 
         QTMovieOpenForPlaybackAttribute, 
         videoURL, QTMovieURLAttribute, 
         [NSNumber numberWithBool:YES], 
         QTMovieOpenAsyncRequiredAttribute, nil]; 
QTMovie *newMovie = [[QTMovie alloc] initWithAttributes: attrib error: &error]; 
[movieView setMovie:newMovie]; 
[movieView play:newMovie]; 

} 

我使用的Xcode 5.1 OSX 10.8

回答

5

苹果开始(在Xcode 5.1不兼容)从QuickTime API(包括QTKit)到从OS X 10.7开始的AVFoundation。关于该转换有一个detailed technical note

在现代Mac应用程序中播放视频的最简单方法是通过AVKit(自10.9开始可用)。
为了得到显示与您的应用程序捆绑视频的基本看法,你必须:

  1. 添加AVKit & AVFoundation到项目中的部分“链接的框架和库”
  2. 拖动AVPlayerView到窗口在Interface Builder
  3. 连接的插座,播放器视图在控制器中的一个
  4. 实例化从您的包加载视频资源的AVPlayer

这是加载您的资产代码:

NSURL* videoURL = [[NSBundle mainBundle] URLForResource:@"video name" withExtension:@"mp4"]; 
self.playerView.player = [AVPlayer playerWithURL:videoURL]; 
+0

感谢会看看它是否工作 – 2014-09-05 00:31:20

+0

是它与OSX 10.8 Xcode的5一样的吗? – 2014-09-05 01:47:33

+0

如何在窗口加载时自动播放视频? – 2014-10-01 05:35:17