-(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
Related
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]];
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.
would you please help me with this code, I'm trying to play video but I'm having error Exc_bad_Access code = 1 and some times code = 2:
-(IBAction)BtnPressed:(id)sender{
self.videoview.hidden = false;
NSString *btnTag = [NSString stringWithFormat:#"%d",[sender tag]];
NSString *videofilename = [NSString stringWithFormat:#"%#%#_%#", selectedGender, btnTag, selectedVowel];
//Playing video
NSString *filepath = [[NSBundle mainBundle] pathForResource:videofilename ofType:#"mp4"];
NSLog(#"file name is %#",filepath);
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
//NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:btnTag ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller];
//[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
//playercontroller.moviePlayer.scalingMode = MPMovieScalingModeNone;
playercontroller.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playercontroller.view setFrame:CGRectMake(30, 50, 150, 200)];
[self.videoview addSubview:playercontroller.view];
[playercontroller.moviePlayer prepareToPlay];
[playercontroller.moviePlayer play];
//playercontroller = nil;
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
Waiting for your advice
It looks like your MPMoviePlayerViewController isn't retained, so it is probably getting deleted.
I am using GPUImage library to record a video to a file on filesystem. I save it as m4v file. This is the code I'm using:
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:recordingDestination];
unlink([pathToMovie UTF8String]);
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(1280.0, 720.0)];
[videoCameraFilter addTarget:movieWriter];
movieWriter.shouldPassthroughAudio = YES;
movieFile.audioEncodingTarget = movieWriter;
[movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];
[movieWriter startRecording];
[movieFile startProcessing];
[movieWriter setCompletionBlock:^{
[videoCameraFilter removeTarget:movieWriter];
[movieWriter finishRecording];
}];
This records a m4v video. I then try to play it with MPMoviePlayerController:
NSString *videoPath = [NSString stringWithFormat:#"%#/%#", [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"], [libraryFiles objectAtIndex:selectedCell]];
NSLog(videoPath);
NSURL *url=[[NSURL alloc] initWithString:videoPath];
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDonePressed) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
However, the player just starts buffering and nothing happens. The video doesn't start playing. The player is just buffering forever. The app doesn't crash and there are no warnings in the console, so I don't know what is the problem.
I see at a couple of things that don't look right.
First off you're writing directly to NSHomeDirectory which returns the application directory that is however not writeable (see reference).
Either write to the Documents folder or the Library/Caches folder instead.
Secondly I don't see how videoCameraFilter is defined. You need to make sure to add it as a target of movieFile
GPUImageMovie *movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathToMovie = [documentsDirectory stringByAppendingPathComponent:#"filtered-movie.m4v"];
unlink([pathToMovie UTF8String]);
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
GPUImageMovieWriter *movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(1920.0, 1080.0)];
GPUImageBoxBlurFilter * videoCameraFilter = [[GPUImageBoxBlurFilter alloc] init]; // just a random filter...
videoCameraFilter.blurSize = 2.0;
[movieFile addTarget:videoCameraFilter];
[videoCameraFilter addTarget:movieWriter];
movieWriter.shouldPassthroughAudio = YES;
movieFile.audioEncodingTarget = movieWriter;
[movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];
[movieWriter startRecording];
[movieFile startProcessing];
__weak GPUImageMovieWriter *weakMoviewWriter = movieWriter;
[movieWriter setCompletionBlock:^{
[movieFile removeTarget:weakMoviewWriter];
[weakMoviewWriter finishRecording];
}];
* EDIT:
Did you try yo making moviePlayer a property/ivar as #SAMIR RATHOD suggested? The fact that the moviePlayer stays there buffering endlessly is most probably caused because it's not being retained.
add a property to your viewController's interface:
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
and instantiate your MPMoviewPlayerController:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
Try this :
First Check videoPath it display proper path or not?
.h File
MPMoviePlayerController* player
.m
#synthesize player;
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:videoPath]];
player.scalingMode = MPMovieScalingModeFill;
player.movieSourceType = MPMovieSourceTypeFile;
player.view.frame = CGRectMake(0, 45, 320, 400);
player.shouldAutoplay = YES;
[player prepareToPlay];
[self.view addSubview:player.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player play];
}
#pragma mark MoviPlayer Method
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player1 = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player1];
[player stop];
[player1.view removeFromSuperview];
player1 = nil;
[self.navigationController popViewControllerAnimated:YES];
}
-(void) dealloc {
[player release];
[super dealloc];
}
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.