Video is not playing using MPMoviePlayerContrller - iphone

how to play a video in iPhone sdk using MPMoviePlayerViewController,here i capture video through device and save that particular video in documents directory .
please tell me any one....

Get Document Directory Path:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Then you can use any of the directory listing APIs of NSFileManager.
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
Now You have all contents at Directory Path (all videos stored) in array
NSURL *videoURL = [NSURL UrlWithString:[NSString stringWithFormat:#"%#/%#",documentsDirectory,[directoryContents objectAtIndex:0]]]; //check which url u want play as video name is in nsarray i have selected one random video name.
Use MovieControllerPlayer instance to play url:
MPMoviePlayerViewController *mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:mpViewController];
[mpViewController.view setCenter:yourView.center];
//[mpViewController release];
Hope useful.

Related

Playing a video file saved in the app's documents directory

I have a video file saved in my app's local directory in the documents folder. I want to playback the file when the user clicks on the item in an embedded table view I created. My code for playing back the video is as follows:
NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* path = [documentPath stringByAppendingPathComponent:#"DemoRecording.mp4"];
NSURL* movieURL = [NSURL fileURLWithPath: path];
[player.view setFrame: self.view.bounds];
[self.view addSubView: player.view];
[player play];
MPMoviePlayerViewController* moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL: movieURL];
[self presentMoviePlayerViewControllerAnimated: moviePlayer];
The video doesn't play back. The path is correct and the file exists there.
I know this is a repetitive question. I have referred to these links:
Playing a Downloaded Video from the Documents Directory with Cocoa-Touch
MPMoviePlayer load and play movie saved in app documents
But couldn't find a definitive answer.
Please help me out on this one. I am using iOS 5.1.1, if that changes anything.
EDIT:
It does work. I had forgotten to pass the URL to the movie player.
you can play Video from Document Directory like this way:-
-(IBAction)playVideo
{
NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(#"files array %#", filePathsArray);
NSString *fullpath;
for ( NSString *apath in filePathsArray )
{
fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
vedioURL =[NSURL fileURLWithPath:fullpath];
}
NSLog(#"vurl %#",vedioURL);
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
NOTE
Please note that don't Forget to include requested Framework or connect Delegate
The mistake in you was you missed / before DemoRecording.mp4
It should be
NSString* path = [documentPath stringByAppendingPathComponent:#"/DemoRecording.mp4"];
Also you are writing player statements before declaring it.

How can i store streaming video in resource folder in iOS?

If i go to the point then my problem is, i want to make a web view where it will load a video and when stream starts then i want to download/store that video data in the resource folder of the application.How can i do that?
Please somebody help me by any kinds of help.
BR
Emon
In the run time you can't save a video to resource folder but you can save files in to
NSDocuments and retrive them using NSFileManagers.Go through this link
http://www.techotopia.com/index.php/Working_with_Files_on_the_iPhone
Try with this Code to save a file
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/filename.MP4",documentsDirectory];
NSData* videoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"your Url"]];
[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
[filemanager createFileAtPath:filePath contents:videoData attributes:nil];
To retrive:
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/filename.MP4",documentsDirectory];
BOOL exists = [fm fileExistsAtPath:newDirectory isDirectory:&isDir];
if (exists) {
//Play Video with contents of file path.
}

how to play binarydata of song in AVAudioPlayer

I'm receiving songs url from xml and saving them in sqlite as binary data.
Now I want to play those songs in AVAudioPlayer.
How would I achieve this?
Is this method is right for playing binarydata of song in AVAudioPlayer.?
Please have a look at this post:
AVAudioPlayer with external URL to *.m4p
AVAudioPlayer only works with local URL's. It must be a File URL (file://)
See Apple's Technical Q&A QA1634
You could get your binary data and write it to a file and then play it:
NSURL *url = [NSURL URLWithString:#"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p"];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:#"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:filePath] error:NULL];
NSLog(#"error %#", error);

Saving/retrieving video files in iOS

I've been browsing around but couldn't find a solution to my problem. I'm trying to save some video files to my application's directory I do this as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"myFile"];
NSLog(#"%#",appFile);
[[NSUserDefaults standardUserDefaults] setValue:appFile forKey:#"videoURL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[webData writeToFile:appFile atomically:YES];
My question is, is this the proper way of saving the movie file and if yes how do I convert the NSData to a file that can be played back via MPMoviePlayer? Thanks for your help.
PS. I really don't want to use the photo library as the app is likely to hold a fair amount of videos.
If you really want to save it in the documents of your app, you'll have to use NSFileManager
it would probably look like something like that
NSString* completeFileName = [NSString stringWithFormat:#"%#",movieName];
NSString* filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:completeFileName];
[[NSFileManager defaultManager] createFileAtPath:filename contents:yourMovieData attributes:nil];
You also might need to use NSFileHandle
To read the video in your MPMoviePlayer, if think you can make an NSURL like this
[NSURL fileURLWithPath:path isDirectory:NO];
then you give that url to your MPMoviePlayer.
I hope this will help you.

save mp3 data in file on iphone

I'm developing an iPhone application and i need to download and Save mp3 media on the iPhone and play it,
I fished the downloading work and i put the data in NSMutableData variable But How can i save these data as mp3 file
any advice :)
If you want to save it to your applications Documents directory with a .mp3 file extension you can use the following:
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject]
stringByAppendingPathComponent:#"MyFileName.mp3"];
[[NSFileManager defaultManager] createFileAtPath:mp3FileName
contents:mp3Data
attributes:nil];
Where mp3Data is your NSMutableData. I use code similar to the above for saving PDF documents, custom file formats, jpgs and all sorts. I've not had cause to try mp3 yet but there's no reason why it shouldn't.
To read the file back into NSData to send to AVAudioPlayer:
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject]
stringByAppendingPathComponent:#"MyFileName.mp3"];
NSData *mp3Data = [NSData dataWithContentsOfFile:mp3FileName];
avAudioPlayer.data = mp3Data;
Above para of code untested, never had cause to use sounds in my apps.
here is the correct soulution appending up on #Diziet comment
'//prepare data to save in file
NSData *fileData=[[NSData alloc]initWithData:(NSData *)receivedData];
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]stringByAppendingPathComponent:#"MyFileName.mp3"];
//create the file
[[NSFileManager defaultManager] createFileAtPath:mp3FileName contents:receivedData attributes:nil];
//load the file for playing
NSString *FileNamePath = [[NSString alloc] initWithString:#"MyFileName.mp3"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:FileNamePath];
NSURL *url=[[NSURL alloc]initWithString:appSettingsPath ];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
player.delegate=self;
[player play];'