NSString *path = [[NSBundle mainBundle] pathForResource:@"comp1" ofType:@"mov"];
// 返回 nil,但图片、音频同方法正常
ofType: 与实际扩展名大小写完全一致(如 MOV vs mov)。comp1.mov,但实际全名可能为 comp1.mov.mov。在终端 ls -la 核实。pathForResource:ofType:inDirectory:。// 方式一:指定子目录
NSString *path = [[NSBundle mainBundle] pathForResource:@"comp1" ofType:@"mov" inDirectory:@"Videos"];
// 方式二:遍历 Bundle 查找(调试用)
NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"mov" inDirectory:nil];
NSLog(@"All mov files: %@", paths);
获取到路径后,推荐使用 AVPlayer 播放:
NSURL *url = [NSURL fileURLWithPath:path];
AVPlayer *player = [AVPlayer playerWithURL:url];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
[layer setFrame:self.view.bounds];
[self.view.layer addSublayer:layer];
[player play];