获取本地路径 和播放视频

获取沙盒目录

//获取沙盒目录带(Documents)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths  firstObject];
 NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"video-test.mp4"];

 //NSHomeDirectory() 与 documentsDirectory少(/Documents)
NSString *fullPath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), @"video-test.mp4"];
//

获取本地文件(自己理解就是项目里的文件)

 NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"video_test" ofType:@"mp4"];

播放视频

    NSURL *url = [NSURL URLWithString:urlStr];
// 播放本来视频
//  NSURL *url = [NSURL fileURLWithPath:fullPath];
    /// 视频播放器
    _videoPlayer = [AVPlayer playerWithURL:url];
    /// 视频播放渲染图层
    AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:_videoPlayer];
    [videoLayer setVideoGravity:AVLayerVideoGravityResizeAspect];//显示模式
    [videoLayer setBounds:self.bounds];
    [videoLayer setPosition:CGPointMake(self.width * 0.5, self.height * 0.5)];
    [self.layer addSublayer:videoLayer];

你可能感兴趣的:(获取本地路径 和播放视频)