使用 AVPlayer 播放文档字典中保存的视频

Playing Saved Videos in Documents Dictionary With AVPlayer

我已经在我的文档文件夹中保存了一个 1mb 的 big buck bunny,我尝试用我的 AVPlayer 加载和播放它,为此我使用了这个代码

 NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
             NSString* foofile = [documentsPath stringByAppendingPathComponent:string]; // string is file name

        NSURL *url = [NSURL URLWithString:foofile];
         NSLog(@" exsits %@ :",foofile);


        AVPlayer*player1 = [AVPlayer playerWithURL:url];
        AVPlayerViewController *Controller = [[AVPlayerViewController alloc] init];
        Controller.player = player1;
        [player1 play];
        [self presentViewController:Controller animated:YES completion:nil];

但我得到的不是播放器而是这个

simulator screenshot

编辑 文件 URL 看起来像这样

/Users/usrname/Library/Developer/CoreSimulator/Devices/4453818A-0617-479B-B98C-3FC544A24D00/data/Containers/Data/Application/D7449628-0A67-4294-9471-98F5596FE596/Documents/tt1823672.mp4

格式化评论让我很烦,所以我只是 post 作为答案。

我记得,如果你想播放一些本地文件,url的前缀通常是file://...

但是如果你直接创建url,前缀可能会是http://..

所以您可能想要更改您的代码
[NSURL URLWithString:foofile]

[NSURL fileURLWithPath:foofile];

所以.. url 应该有 file://.. 的前缀,并且应该是本地文件的目标。