MPMoviePlayerController video not playing from local document in real phone - ios

When I use the MPMoviePlayerController in my app, it does not play videos and just shows a black rectangle.
my code is as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = nil;
//filePath =[NSString stringWithFormat:#"%#/%#.mp4", documentsDirectory, fileName];
filePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp4",fileName]];
NSLog(#"%#",filePath);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists){
NSLog(#"exist!");
}else{
NSLog(#"no exist!");
}
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
[moviePlayer.view setFrame:self.view.frame];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.movieSourceType= MPMovieSourceTypeStreaming;
moviePlayer.fullscreen = YES;
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
-(void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
The output is:
iBeaconCollection[10320:60b] /var/mobile/Applications/107F6DE4-AE31-40E0-BAA0- 76A956B72116/Documents/101A ~ Hallway to 101-103.mp4
2014-06-11 14:13:27.302 iBeaconCollection[10320:60b] exist!
Hope someone could help me! Thanks.

Use MPMoviePlayerViewController, and then try and make sure that the file is in fact an MP4, as you are appending that extension.

Related

itemFailedToPlayToEnd Error in iOS while playing video

-(void)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);
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer prepareToPlay];
[moviePlayer.view setFrame:self.view.bounds]; // player's frame must match parent's
//moviePlayer.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;
moviePlayer.moviePlayer.fullscreen = YES;
[moviePlayer.moviePlayer play];
[self.view bringSubviewToFront:btnSkip];
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:#selector(movieFinishedCallback:)
// name:MPMoviePlayerPlaybackDidFinishNotification
// object:moviePlayer];
}
Getting Following ERROR
_itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; } error in iOS while playing video
in .h file create a new instance as
MPMoviePlayerController *player
in .m file
- (void) playMovieOffline:(UIView *)view
{
// Register to receive a notification when the movie has finished playing.
NSString *path = [[NSBundle mainBundle]pathForResource:
#"videofile_name" ofType:#"mp4"];
player = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player setControlStyle:MPMovieControlStyleNone];
player.movieSourceType = MPMovieSourceTypeFile;
[player play];
[player.view setFrame:CGRectMake(0, 0, 320, 235)];
[view addSubview:player.view];
}
- (void)moviePlayerDidFinish:(NSNotification *)note
{
if (note.object == player) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[player play];
}
}
}
Try this, i hope it helps

Video File Not Playing file Objective C

hello I have a function to download file save to Docs Dir and then play it . The File is download and saved .. the path are the same but the message is that can not player.
Here is
My Download Function code.
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSLog(#"Temporary File :%#\n", location);
NSError *err = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:#"welbi.mp4"]];
if ([fileManager moveItemAtURL:location toURL:docsDirURL error: &err])
{
//NSLog(#"File is saved to =%#",docsDir);
NSLog(#"File path is ----> %#",[docsDirURL absoluteString]);
[self playVideo:[docsDirURL absoluteString]];
}
else
{
NSLog(#"failed to move: %#",[err userInfo]);
}
}
and this is my Video Player Function.
- (void) playVideo:(NSString *)fileName
{
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSString *appDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *fileDirURL = [NSURL fileURLWithPath:[appDir stringByAppendingPathComponent:fileName]];
NSLog(#" URL Video ===> %#", [fileDirURL absoluteString]);
//MPMoviePlayerViewController *playerViewController;
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[fileDirURL absoluteString]]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//play movie
MPMoviePlayerController *player = [playerViewController moviePlayer];
player.shouldAutoplay = YES;
player.movieSourceType = MPMovieSourceTypeFile;
[player play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[player.view removeFromSuperview];
}
I checked and the file paths are exactly the same... but it gives me this error message
2014-08-01 12:13:30.972 training[16793:90b] File path is ----> file:///Users/Russelius/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/D92411C3-EBEA-4CEA-B1EB-DAC8AB986599/Documents/welbi.mp4
2014-08-01 12:13:30.973 training[16793:90b] URL Video ===> file:///Users/Russelius/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/D92411C3-EBEA-4CEA-B1EB-DAC8AB986599/Documents/file:/Users/Russelius/Library/Application%2520Support/iPhone%2520Simulator/7.1/Applications/D92411C3-EBEA-4CEA-B1EB-DAC8AB986599/Documents/welbi.mp4
2014-08-01 12:13:31.539 training[16793:90b] _itemFailedToPlayToEnd:
{
kind = 1;
new = 2;
old = 0;
}
any help is welcome.
Problem
Looking at the log you will see your path start with file// your player expect the path to your file not the absolute path.
Solution
Solution is to replace absoluteString with path
NSLog(#"absolute path is ----> %#",[docsDirURL absoluteString]);
NSLog(#"File path is ----> %#",[docsDirURL path]);
[self playVideo:[docsDirURL path]];

iOS - MPMoviePlayerController failing to play video from remote URL

So I have a video being accessed by filepicker.io that is attempting to be played with an MPMoviePlayerController. So far I've tried everything from trying to load the video directly from the FilePicker URL to grabbing the NSData and loading it into the local filesystem. This is my current code, but all that happens is I get a blank screen and the [movieplayer load state] returns 0 (error code). I've verified that the file is there and that it is an MOV filetype.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
movieString = [documentsDirectory stringByAppendingPathComponent:#"vid.mov"];
[responseData writeToFile:movieString atomically:YES];
NSURL *url = [NSURL fileURLWithPath:movieString];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];
[moviePlayer prepareToPlay];
[moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer play];
Declaring the movie player as a property fixed the problem. Apparently there's a bug associated with it.

Audio playback for movie despite in silent mode?

sorry for my question but i´ve implemented a intro- video and despite of the hardware silent-switch of my iPad, the audio is playing.
I´m also using the AVAudioplayer within my app just for playing short sound samples. Within this class, its the only region where i´ve set up the "AVAudioSessionCategory".
But for all audio playback only, there´s nothing hearable. Its just for my intro-video.
Any help how to fix that "audio-bug" so the movie player is silent?
Thanks you
Here´s my Audio-class:
- (id)initWithSoundfileName:(NSString*) file
{
if ((self = [super init]))
{
NSString* filename = [file stringByDeletingPathExtension];
NSString* fileextension = [file pathExtension];
// get file path from bundle
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: filename ofType: fileextension];
NSLog(#"AudioPlayer init: %#", soundFilePath);
NSURL* fileurl = [[NSURL alloc] initFileURLWithPath:soundFilePath];
NSError* error = nil;
AVAudioPlayer* audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileurl error:&error ];
if (error) { NSLog(#"Error creating AVAudioPlayer %#", [error description]);}
// set audio policy
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:NULL];
self.player = audioplayer;
[self.player prepareToPlay];
[self.player setDelegate:self];
}
return self;
}
-(void) play{
[self.player play];
}
And here´s my video-playback method:
- (void)playIntroVideo
{
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"mymovie" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:movpath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
self.moviePlayerController.fullscreen = YES;
self.moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayerController.useApplicationAudioSession = NO;
[self.moviePlayerController.view setFrame: self.view.bounds];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayerController];
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController play];
}
Like #Till mentioned:
When I change the MoviePlayerController property to useApplicationAudioSession=TRUE, it fixes my problem. Audio Playback is silent.

Play downloaded video MPMovieController

So I'm using ASIHTTPRequest to download a video file from my FTP. It downloads and I get this as the directory path to the file:
/Users/Matt_Sich/Library/Application Support/iPhone Simulator/5.0/Applications/B7366FF2-7592-4ED0-ABC2-46BA111D0FF4/Documents/INWD.mp4
Ok now I want to play that video.... I would image that doing this would be easy but for some reason I can't manage to get it to work. All I get is a black view and that's it.
//NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Park_iPod" ofType:#"mp4"]];
NSURL *url = [NSURL URLWithString:self.PathString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
(NOTE: self.PathString is the path to the downloaded file that I mentioned above)
It plays the video from the main bundle just fine so I'm sure that there's something wrong with the file path.
Once you've downloaded your file you need to use fileURLWithPath to access the file, not URLWithString
NSURL *url = [NSURL fileURLWithPath.PathString];
This would get you the documents directory...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:([NSURL fileURLWithPath : self.PathString])];
[moviePlayer setShouldAutoplay:NO];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(movieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: moviePlayer];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object: moviePlayer];
[self.view addSubview: moviePlayer.view];
[moviePlayer.view setFrame: self.view.bounds];
[moviePlayer prepareToPlay];

Resources