Open video file from Assets folder and play it - ios

I am trying to load a .mp4 file that I added inside assets/videos folder but I fail. This is my code so far:
- (IBAction)showVideo:(id)sender
{
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths1 objectAtIndex:0];
NSString *strPath = [NSString stringWithFormat:#"%#/%#",documentsDirectory,#"myVideoName.mp4"];
NSLog(#"strPath %#",strPath);
NSURL *videosURL = [NSURL fileURLWithPath:strPath];
AVPlayer *player = [AVPlayer playerWithURL:videosURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self presentViewController:controller animated:YES completion:nil];
controller.player = player;
[player play];
}
I think I am doing something wrong with the video file path.
Can you help me? Thanks!

I found the solution. I replaced this:
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths1 objectAtIndex:0];
NSString *strPath = [NSString stringWithFormat:#"%#/%#",documentsDirectory,videoName];
NSLog(#"strPath %#",strPath);
with this:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *myFile = [mainBundle pathForResource: #"videoName" ofType: #"mp4"];

Related

Read video file from documents folder

I have this code that works fine and shows me the video in my app:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;
What I need is to read the same file from documents folder, and it exist, so what I did is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *str = [NSString stringWithFormat:#"%#/MyCoolApp/Animations/%#", documentsPath,#"movie"];
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:str ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;
The file is there, but nothing playing, just loading.
You are getting the path for the documents folder then trying to find it in the main bundle.
Try the following:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"/MyCoolApp/Animations/movie.mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:path];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;

i play a video stored in a subfolder in ios6 documents using MPMoviePlayerController ,but it doesn't work

i play a video stored in a subfolder named test1 in ios6 documents using MPMoviePlayerController, but it doesn't work
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];//
NSString *movieFolderPath = [documentDirectory stringByAppendingPathComponent:#"/test1"];
NSString *newsrc = [[NSString alloc]initWithFormat:#"/%#",new.src ];
NSString *moviePath = [movieFolderPath stringByAppendingPathComponent:newsrc];
NSURL *url = [[NSURL alloc]initWithString: [moviePath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] ];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
_moviePlayer.movieSourceType = MPMovieSourceTypeFile;
_moviePlayer.view.frame = new.region;
[_moviePlayer prepareToPlay];
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
well :url is /Users/fengsu/Library/Application Support/iPhone Simulator/6.0/Applications/53C97346-C8FD-42DF-AD37-F2B14D5D3984/Documents/test1/v1.mp4
I think there is something wrong with the url,but i don't konw why,please help me ,thank you very much
The problem is how you construct the NSURL object.
MPMoviePlayerController expects URLs pointing to local files to have a file:// prefix.
The way to do this is as follows:
NSString *filename = #"your_video.mpeg";
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *yourVideoPath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *movieURL = [NSURL fileURLWithPath:yourVideoPath isDirectory:NO];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// Rest of the code...
Use [NSURL fileURLWithPath:moviePath]
That is used to open urls from files.

AVAudioPlayer Doesn't play the songs inside the folders?

I used this code to play songs it is not playing the songs inside the folders or folders of folders except the Documents directory.
NSString *songname=[NSString stringWithFormat:#"%#",songArray[songIndex]];
NSLog(#"songname:%#",songname);
NSString* saveFileName = songname;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName];
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
self.audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"github.mp4"];
NSURL *url1 = [[NSURL alloc] initFileURLWithPath: path];
avPlayer = [AVPlayer playerWithURL:url1] ;
[self.avplayer play];

MPMoviePlayerViewController won't play movie, start and closes immediately

I'm trying to play a video with MPMoviePlayerViewController, I present the MPMoviePlayerViewController but after 1 second he is dismissing itself.
This is my code:
.h:
#import <MediaPlayer/MediaPlayer.h>
#property (strong, nonatomic) MPMoviePlayerViewController *moviePlayerViewController;
.m:
- (void)playmovie
{
NSString *databaseName = #"NO.mp4";
NSArray *documentPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentDir stringByAppendingPathComponent:databaseName];
NSURL *movieURL = [NSURL fileURLWithPath:databasePath];
UIGraphicsBeginImageContext(CGSizeMake(1,1));
self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];
UIGraphicsEndImageContext();
[self.moviePlayerViewController.moviePlayer prepareToPlay];
[self.moviePlayerViewController.moviePlayer play];
}
Please tell me what I did wrong.
Thanks in advance!
check below answer
https://stackoverflow.com/a/15586468/1713478
change url to below code
NSString *path =[[NSBundle mainBundle] pathForResource:#"NO" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
try this you will be succeed.
Try this..
NSString *databaseName = #"NO.mp4";
NSArray *documentPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentDir stringByAppendingPathComponent:databaseName];
NSURL *url = [NSURL fileURLWithPath:databasePath];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayer.view.frame = CGRectMake(0, 0, 500, 500);
moviePlayer.moviePlayer.shouldAutoplay=YES;
moviePlayer.moviePlayer.controlStyle = MPMediaTypeMusicVideo;
[moviePlayer.moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer play];

Attaching a recorded .caf file locatted in documents directory to mail iOS Xcode

I have files recorded by the user in the documents directory... displayed in a uitableview...
I placed this code in my didSelectRowAtIndexPath... Why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",[directoryContents objectAtIndex:indexPath.row]]];
NSURL *audioURL = [NSURL URLWithString:fileName];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer addAttachmentData:audioData mimeType:#"audio/caf" fileName:fileName];
You are creating the URL incorrectly. You need to use fileURLWithPath, not URLWithString.
Also, this:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",[directoryContents objectAtIndex:indexPath.row]]];
Should be:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[directoryContents objectAtIndex:indexPath.row]];
There is no need for the string format.
Last thing. Th file name passed to the addAttachment method should be just a file name, not a full pathname.

Resources