AVPlayer playerWithURL not working after app restart

给你一囗甜甜゛ 提交于 2019-12-25 08:48:19

问题


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!


回答1:


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!




回答2:


you need to take evertime 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 coreData 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



来源:https://stackoverflow.com/questions/46494774/avplayer-playerwithurl-not-working-after-app-restart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!