我正在开发一款需要在应用中播放教程视频的mac应用。 当用户按下应用程序上的按钮时,它应该播放视频 这是xcode 4.3的代码(与xcode 5.1不兼容)
@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 for osx 10.8
答案 0 :(得分:5)
从OS X 10.7开始,Apple开始从QuickTime API(包括QTKit)过渡到AVFoundation。关于这种转变有一个detailed technical note。
在现代Mac应用中播放视频的最简单方法是通过AVKit(自10.9开始提供) 要获得显示与您的应用捆绑在一起的视频的基本视图,您必须:
AVPlayerView
拖到Interface Builder AVPlayer
这是加载资产的代码:
NSURL* videoURL = [[NSBundle mainBundle] URLForResource:@"video name" withExtension:@"mp4"];
self.playerView.player = [AVPlayer playerWithURL:videoURL];
答案 1 :(得分:0)
Thomas在Swift 5中回答
if let videoURL = Bundle.main.url(forResource: "video",
withExtension: "mp4") {
self.playerView.player = AVPlayer.init(url: videoURL)
self.playerView.player?.play()
}