I know that we can create movie player using initWithContentURL:
and we can pass NSUrl argument. Here I don't have NSUrl, I have only NSData. By using it can I create movie player?
There is no method available for initializing the Movieplayer with data.
My suggestion : you need to write the data to document directory as a video file and then initialize the player using that url.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile.m4v"];
[data writeToFile:appFile atomically:YES];
Here data
is the NSData
of the video file.
You can now use the appFile
variable for initializing your movieplayer.
NSURL *movieUrl = [NSURL fileURLWithPath:appFile];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController] alloc] initWithContentURL:movieUrl];
Saving File:
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
if let docDir = paths.first
{
let appFile = docDir.appending("/MyFile.m4v")
let movieUrl = URL(fileURLWithPath: appFile)
do
{
try data.write(to: movieUrl, options: .atomic)
}
catch let error as NSError
{
print(error.localizedDescription)
}
}
Initialising movie player:
let moviePlayer = MPMoviePlayerController(contentURL: movieUrl)
Note: MPMoviePlayerController is deprecated in iOS 9, so you may need to use AVPlayerViewController