我需要使用objective-c在Xcode中播放视频。我在Xcode中拖了一个mp4文件。并使用了代码:
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"videoname" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[self.avPlayer play];
但在文件路径中将错误显示为nil。
答案 0 :(得分:0)
通过拖放添加项目时你应该注意以下事项
1)拖放到 XCode 时,您必须选中复选框复制项目(如果需要)
2)应使用当前目标检查右侧面板上的目标
答案 1 :(得分:0)
尝试
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"videoname" ofType:@"mp4"];
NSURL *streamURL = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerController *moviplayer =[[MPMoviePlayerController alloc] initWithContentURL: streamURL];
[moviplayer prepareToPlay];
[moviplayer.view setFrame: self.view.bounds];
[self.view addSubview: moviplayer.view];
moviplayer.fullscreen = YES;
moviplayer.shouldAutoplay = YES;
moviplayer.repeatMode = MPMovieRepeatModeNone;
moviplayer.movieSourceType = MPMovieSourceTypeFile;
[moviplayer play];
答案 2 :(得分:0)