MPMoviePlayerViewController won't play movie, start and closes immediately - ios

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];

Related

Open video file from Assets folder and play it

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"];

Download and play mp3 file from URL

I have downloaded a mp3 file but i'm not able to play it. The file is downloaded, i've checked but when i try to play the local file, nothing happens. If i directly play it without downloading it, it plays.
- (IBAction)downloadTrack:(id)sender {
NSData *soundData = [NSData dataWithContentsOfURL:self.song.ticket.url];
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documents stringByAppendingPathComponent:self.song.slug];
[soundData writeToFile:filePath atomically:YES];
NSURL *urlFile = [NSURL URLWithString:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:urlFile error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Download
NSString *urlString = #"https://oi.net/songs/5-dope-ski-128k.mp3";
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documents stringByAppendingPathComponent:#"5-dope-ski-128k.mp3"];
[soundData writeToFile:filePath atomically:YES];
// Play
NSString *filename = #"Ola.mp3";
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *yourSoundPath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *soundUrl;
if ([[NSFileManager defaultManager] fileExistsAtPath:yourSoundPath])
{
soundUrl = [NSURL fileURLWithPath:yourSoundPath isDirectory:NO];
}
// Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_audioPlayer play];
}

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.

Load audio in DOCUMENTS when UITableView "cell" is pressed

I have a UITableView displaying a llist f files in the Documents Directory... and I want it to play the audio file in the Documents directory when pressed...
It runs with no errores but doesn't play the audio when pressed...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d.caf",indexPath.row+1]];
NSURL *audioURL = [NSURL URLWithString:fileName];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
_audioPlayer.delegate = self;
[_audioPlayer play];
}
Change this line:
NSURL *audioURL = [NSURL URLWithString:fileName];
to this:
NSURL *audioURL = [NSURL fileURLWithPath:fileName isDirectory:NO];
And then just do:
if(_audioPlayer == nil)
{
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
}
else
{
_audioPlayer = [self.audioAlert initWithContentsOfURL:audioURL error:nil];
}
To start out, the best way to declare a files path is this
- (NSString *)docsdir {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
}
in your header, declareNSString *Path;
then, declare the path like this when the view loads
Path = [[self docsdir]stringByAppendingPathComponent:[NSString stringWithFormat:#"%d.caf",indexPath.row+1]];
if (![[NSFileManager defaultManager]fileExistsAtPath:Path]) {
[[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:#"%d",indexPath.row+1] ofType:#"caf"] toPath:Path error:nil];
}
now to play it, do this
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:&error];
player.delegate = self;
[player play];

Resources