Mig70
Mig70

Reputation: 61

AVPlayer playerWithURL not working after app restart

I am picking a video from the user photo library and than I save the video in the user Documents Folder to be able to play the video even if the user deletes this video from his photo Library. The URL to this file is stored in Core Data. Everything works fine until the next time I run the App. Somehow it seems like the URL is no longer valid, which is strange because I am able to delete the video file when [AVPlayer playerWithURL:videoURL] fails. Here is how I pick the video URL:

- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSURL* videoURL = info[UIImagePickerControllerMediaURL];}

This is how I save the video:

+ (NSURL*) saveVideoInDocumentsFolder:(NSURL*)videoURL name:(NSString*)name {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* pathComponent = [NSString stringWithFormat:@"/%@.%@", name, [videoURL pathExtension]];
NSString* path = [documentsDirectory stringByAppendingPathComponent:pathComponent];

NSError* error = nil;
NSData* videoData = [NSData dataWithContentsOfURL:videoURL options:0 error:&error];
if (error)
    return nil;

BOOL success = [videoData writeToFile:path options:NSDataWritingAtomic error:&error];
if (success)
    return [NSURL fileURLWithPath:path];

return nil;}

This is how I play the video:

AVPlayer* player = [AVPlayer playerWithURL:videoURL]; // <- AFTER I RESTART THE APP THIS METHOD ALWAYS RETURNS nil!!

AVPlayerViewController* viewController = [[AVPlayerViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
viewController.player = player;
[player play];

Many thanks in advance!

Upvotes: 0

Views: 564

Answers (2)

Raj Oriya
Raj Oriya

Reputation: 78

You need to take every time the path directory when close and open app , because when app close it it removes documentDirectory, so you have to take it another time , and if you want to get your video file then save the FILE NAME of that video into preference or Core Data then get documentDirectory path by appending this video name file and you will get your video.

Just have a look: - (code is in swift you can convert it objective c easily)

let fileName = "recording.mp4"

let tempPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)

let tempDocumentsDirectory: AnyObject = tempPath[0] as AnyObject

let tempDataPath = tempDocumentsDirectory.appendingPathComponent(fileName) as String as String

that FILE NAME you will save in preference or coreData

and when you close and open the app just check do you have FILE NAME save in preference or in core data if yes then take it by appending with documentsDirectory and you will get your video

Upvotes: 0

matt
matt

Reputation: 535138

The URL to this file is stored in Core Data

That's the problem. The documents directory URL changes every time you run the app (because you are sandboxed), so it isn't valid the second time. Never never never save an absolute file URL in iOS!

Upvotes: 2

Related Questions