I am trying to play a video stored in Core data. After fetch it shows, there is an object and objects.video returns a value but the dataString prints out to be null. I am not sure what I maybe doing wrong. Is it a right way to play video or is there something I could have done better?
I have single object in Core Data.
I have stored as video as NSData in Core data. I want to get that stored video and play. Is there any other way I can do it?
_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);