Search code examples
iosobjective-cavplayernsfilemanager

Playing Saved Videos in Documents Dictionary With AVPlayer


i have saved a 1mb big buck bunny onto my documents folder, and i tried to load and play it with my AVPlayer,for this i used this code

 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];

but instead of the player i am getting this

simulator screenshot

EDIT File URL looks something like this

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


Solution

  • Formatting comment was killing me, so I just post as answer.

    As I remembered, if you want to play some local files, the prefix of the url usually is file://....

    But if you create url directly, prefix might will be http://.. .

    So you might want to change your code
    [NSURL URLWithString:foofile]
    to
    [NSURL fileURLWithPath:foofile];

    so.. url should had a prefix of file://.., and should be target to local files.