Can we create movie player using nsdata in ios? - ios

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.
Objective C
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];
Swift
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

Related

Playing a local video using AVPlayer

I just wasted like 3 hours on this and I can't see where I'm going wrong.
I'm trying to play a video I have stored locally using AVPlayer. This is how I launch the player:
- (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];
}
And this is the NSURL I'm passing:
+ (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;
}
When I run openVideo, the output I get is:
Playing video with the url:
/var/mobile/Containers/Data/Application/72C35DC4-9EF1-4924-91F4-EDA4BDB6AAD3/Documents/sample.vid
But I keep getting a disabled video player..
What am I doing wrong?
Finally figured the issue out. I was downloading my files and storing them as NSData, and had to use writeToFile and specify the correct extension to read them correctly.
NSString *saveFilePath; = [NSTemporaryDirectory()stringByAppendingPathComponent:#"temp.mp4"];
[fileData writeToFile:saveFilePath atomically:YES];
NSURL *filepath = [NSURL fileURLWithPath:saveFilePath];
AVPlayer *player = [AVPlayer playerWithURL:filepath];
Hopefully this helps someone out there.

Play video from coredata as nsdata in avplayer

How to play video in AVPlayer from coredata as nsdata formate?
MPMoviePlayerController deprecated so I am going with AVPlayer. I am search some thing said about url path for saved nsdata location I am not clear about that. Anyone explain this? How can I play?
I am new for this I appreciate if share that code.
If any one share good tutorial for custom AVPlayer I didn't find good one so learn from github sample project.
Advance Thanks :)
You can try this!
NSString *filePath = [self documentsPathForFileName:#"video.mp4"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if(!fileExists) {
NSData *videoAsData; // your data here
[videoAsData writeToFile:filePath atomically:YES];
}
// access video as URL
NSURL *videoFileURL = [NSURL fileURLWithPath:filePath];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoFileURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[self presentViewController:controller animated:YES completion:^{
[controller.player play];
}];
- (NSString *)documentsPathForFileName:(NSString *)name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
return [documentsPath stringByAppendingPathComponent:name];
}
// if you want to remove file after playing video
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
Here's a Swift version on how to do this
let data = //... your video Data/NSData
let cacheURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("yourvidename.mp4")
do {
try data.write(to: cacheURL, options: .atomicWrite)
} catch let err {
print("Failed with error:", err.localizedString)
}
// instantiate the player
let player = AVPlayer(url: cacheURL)
let vcPlayer = AVPlayerViewController()
vcPlayer.player = player
vcPlayer.player?.play()
// present it or add it to whatever view you need
present(vcPlayer, animated: true, completion: nil)
Then on viewWillDisappear delete the content of the cachesDirectory if you want

iOS: How to save a video in your directory and play it afterwards?

I am working in an iOS project. I want may application to download a video from the internet programmatically. Then I want to play it. I know how can I play a local video from the Resources, but my question is how could I download it , and the find it to be played.
I am using MPMoviePlayerController to run the video.
Thanking in advance
I found the answer
here I saved the video
NSString *stringURL = #"http://videoURL";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *documentsDirectory ;
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"videoName.mp4"];
[urlData writeToFile:filePath atomically:YES];
}
and this code is for playing the video form its directory
NSString *filepath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"videoName.mp4"];
//video URL
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController play];
If you have a valid url of the video, Apple provides an API to directly buffer videos with NSURL.
You should hold a reference to the MPMoviePlayerController object from the controller so that ARC doesn't release the object.
#property (nonatomic,strong) MPMoviePlayerController* mc;
Make the URL
NSURL *url = [NSURL URLWithString:#"http://www.example.com/video.mp4"];
Init MPMoviePlayerController with that URL
MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] initWithContentURL:url];
Resize the controller, add it to your view, play it and enjoy the video.
self.mc = controller; // so that ARC doesn't release the controller
controller.view.frame = self.view.bounds;
[self.view addSubview:controller.view];
[controller play]; //Start playing
For more detail you can visit this playing video from a url in ios7

How to get a `NSURL` of the document directory?

I have the following code:
+(NSURL*) getRecordingDirectory {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSURL* url = [[NSURL alloc] initWithString:documentsDirectory]; //<-- nil
return url;
}
The line NSURL* url = [[NSURL alloc] initWithString:documentsDirectory]; Doesn't seem to work. url remains nil after the line.
You need to use a file URL to get the location of a resource on the file system.
[NSURL fileURLWithPath: documentsDirectory]
You can also use NSFileManager to get the same URL.
NSArray *arr = [[NSFileManager defaultManager] URLsForDirectory: NSDocumentDirectory inDomains: NSUserDomainMask];
NSURL *documentsUrl = [arr firstObject];

Find out the extension part of URL saved in NSString

How to find out and save the extension of fields that i am going to save in photo album. i want know it which format.
example:
http://www.example.com/myvideo.mp4,
http://www.example.com/mypicture.png
I need is like .mp4 and .png.If possible give me example code.
Have you looked at the documentation for NSString? You just need to send -pathExtension to your string.
If you're dealing with a string containing a URL, you should first convert it to an NSURL, then extract the path:
NSString *stringURL = #"http://...";
NSURL *url = [NSURL URLWithString:stringURL];
NSString *path = [url path];
NSString *extension = [path pathExtension];
NSURL also have pathExtension (Available in iOS 4.0 and later.)
NSString *extension = [[NSURL URLWithString: #"http://sample.example.com/path/hellowwrod.ext"] pathExtension];
refer a following code.
NSString *path = #"http://www.mysite.com/myvideo.mp4";
NSString *lastPath = [path lastPathComponent];
NSString *fileExtension = [lastPath pathExtension]; // [path pathExtension];
NSLog(#"%#", lastPath); //myvideo.mp4
NSLog(#"%#", fileExtension); // mp4
For Swift:
let stringUrl = " http://www.example.com/mypicture.png"
let url = URL(string: stringUrl)
let path = url?.path
let fileExtension = url?.pathExtension

Resources