ios - 从存储在 Core Data 中的 NSData 播放视频

标签 ios xcode core-data mpmovieplayercontroller nsdata

我正在尝试播放存储在核心数据中的视频。 fetch 后显示,有一个对象,objects.video 返回一个值,但 dataString 打印为 null。我不确定我可能做错了什么。这是播放视频的正确方式还是我可以做得更好?

我在核心数据中有单个对象。

我已将视频作为 NSData 存储在核心数据中。我想获取存储的视频并播放。还有其他方法可以做到吗?

_context =  [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Activity" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
// Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"level"
                                                               ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];


NSError *error = nil;
NSArray *fetchResults = [_context executeFetchRequest : fetchRequest error : &error];
if(fetchRequest == nil){
    NSLog(@"Nothing fetched");
}


for (Activity *objects in fetchResults){

    NSLog(@"%@",objects.video);
  prints->  External Data Reference: <self = 0x7bf48750 ; path = FF54B18E-10B3-4B04-81D4-55AC5E2141B9 ; length = 504426>


NSString *dataString = [[NSString alloc] initWithData:objects.video encoding:NSUTF8StringEncoding];
    NSLog(@"%@",dataString);
    NSURL *movieURL = [NSURL URLWithString:dataString];
    NSLog(@"%@",movieURL);
    _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    [_moviePlayer.view setFrame:CGRectMake (20, 20, 200 , self.view.bounds.size.height/2)];
    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer play];

}
NSLog(@"%i",fetchResults.count);

最佳答案

它只是托管对象上的一个二进制属性。您调用该属性,它返回NSData。从那里你可以在 NSData 还是内存的时候对它做任何你想做的事情。您的问题是您无法将 NSData 转换为 NSURLNSURL 是对数据的引用,其中 NSData 是实际数据。

您需要做的是将视频文件存储在磁盘上的 SQLite 文件之外。然后将对其的引用(也称为 url)存储在 Core Data 中。这将允许您通过电影播放器​​使用视频文件。

正如其他人所说,将视频存储在 SQLite 文件中是一个坏主意。它会破坏 Core Data 的性能。

更新 1

Thanks. I have not saved the video directly to core data instead just copied the video url and saved it as string in core data. Should I create a different folder for the app to store the videos whenever I create a copy of the videos the users use so even if they delete the original video that was uploaded to core data, the copy remains intact and thus maintaining integrity of the Objects in Core Data.

您的评论不清楚。根据您的问题,您将实际视频存储在核心数据中。根据您显示的输出,您将该文件存储在 Core Data 中。你改变了吗?如果是这样,您应该将视频文件存储在已知位置,并将该位置的相对 URL 作为字符串存储在核心数据中。然后,当您准备好使用它时,您可以从中构建完整的 URL。由于沙箱可以更改,因此您无法存储整个 URL,因为它会变得过时。

关于ios - 从存储在 Core Data 中的 NSData 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32976541/

相关文章:

ios - react-native 无法运行 ios 设备

android - 稍后如何设置视频文件的起点

ios - NSAttributedString 和 Button 的问题

ios - 我无法使用 Swift 4.0 使用 Spotify iOS SDK 播放音乐

iOS - 在横向模式下获取状态栏高度和宽度 0

ios - 对 UITableView 部分使用一个 NSManagedObject,它与另一个 NSManagedObject 的行有关系吗?

objective-c - 推送 View Controller 时如何隐藏导航栏?

c++ - 如何搭建Caffe框架XCode 6.2、iOS 8.3环境

ios - 关于objectWithID :的好处

ios - nsset,核心数据,正确的谓词