[英]Playing a local video using AVPlayer
我只是在这上面浪费了3个小时,却看不到我要去哪里。
我正在尝试播放使用AVPlayer本地存储的视频。 这是我启动播放器的方式:
- (void)openVideo:(NSURL *)videoURL {
NSLog(@"Playing video with the url:\n%@", videoURL);
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
这是我要传递的NSURL
:
+ (NSURL *)getURL:(NSString *)itemKey withType:(NSString *)itemType {
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.%@", itemKey, itemType]]];
NSURL *itemURL;
if ([[NSFileManager defaultManager] fileExistsAtPath: dataFilePath]){ // if data exists
itemURL = [NSURL fileURLWithPath:dataFilePath];
}
else {
NSLog(@"The data requested does not exist! Returning an empty url file...");
itemURL = nil;
}
return itemURL;
}
当我运行openVideo
,我得到的输出是:
Playing video with the url:
/var/mobile/Containers/Data/Application/72C35DC4-9EF1-4924-91F4-EDA4BDB6AAD3/Documents/sample.vid
但是我一直在买一个残疾的视频播放器。
我究竟做错了什么?
终于解决了这个问题。 我正在下载文件并将其存储为NSData
,并且必须使用writeToFile
并指定正确的扩展名才能正确读取它们。
NSString *saveFilePath; = [NSTemporaryDirectory()stringByAppendingPathComponent:@"temp.mp4"];
[fileData writeToFile:saveFilePath atomically:YES];
NSURL *filepath = [NSURL fileURLWithPath:saveFilePath];
AVPlayer *player = [AVPlayer playerWithURL:filepath];
希望这可以帮助某个人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.