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]];
Related
I am capturing a five seconds long mp4 file. I would for the User to be able to play and review the video before uploading it to Parse. I don't want to save the video to the local photo library, I just want to replay. Should I be saving the file to the temporary directory like so:
NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"output.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
{
NSError *error;
if ([fileManager removeItemAtPath:outputPath error:&error] == NO){
NSLog(#"File removed from outputPath");
};
}
[MovieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath]
recordingDelegate:self];
Can I play the video using this method?
- (IBAction)playVideo:(id)sender {
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"Output.mp4"];
NSURL *fileURL = [NSURL URLWithString:filePath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[_avPlayer play];
}
-(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
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.
My app is like Vine or Instagram, which records small clips and joins them together. I have problem with memory management when creating avasset.
If I created 100 AVAsset objects from the beginning, it is working fine. But if I keep deleting some of them, and even all of them, and create new ones again, I receive memory warning and also reponds to memory pressure. I check my running app with Instrument and there is no leaked memory.
Below is how I create and remove AVAsset objects in array assets. Does anyone know what could be the problem?
- (void) copyFileToDocuments:(NSURL *)fileURL
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd_HH-mm-ss"];
NSString *destinationPath = [documentsDirectory stringByAppendingFormat:#"/output_%#.mov", [dateFormatter stringFromDate:[NSDate date]]];
NSError *error;
if (![[NSFileManager defaultManager] copyItemAtURL:fileURL toURL:[NSURL fileURLWithPath:destinationPath] error:&error]) {
if ([[self delegate] respondsToSelector:#selector(captureManager:didFailWithError:)]) {
[[self delegate] captureManager:self didFailWithError:error];
}
}
//add asset into the array or pieces
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:destinationPath]];
[self.assets addObject:asset];
}
-(void) deleteLastAsset
{
AVAsset *asset = [self.assets lastObject];
[self.delegate removeTimeFromDuration:CMTimeGetSeconds(asset.duration)];
NSURL *fileURL = nil;
if ([asset isKindOfClass:AVURLAsset.class])
{
AVURLAsset *urlAsset = (AVURLAsset*)asset;
fileURL = urlAsset.URL;
}
if (fileURL)
[self removeFile:fileURL];
[self.assets removeLastObject];
}
- (void) startRecording
{
if ([[UIDevice currentDevice] isMultitaskingSupported]) {
[self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}]];
}
[self removeFile:[[self recorder] outputFileURL]];
[[self recorder] startRecordingWithOrientation:self.orientation];
}
- (void) removeFile:(NSURL *)fileURL
{
NSString *filePath = [fileURL path];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
NSError *error;
if ([fileManager removeItemAtPath:filePath error:&error] == NO) {
if ([[self delegate] respondsToSelector:#selector(captureManager:didFailWithError:)]) {
[[self delegate] captureManager:self didFailWithError:error];
}
}
}
}
-(void)startRecordingWithOrientation:(AVCaptureVideoOrientation)videoOrientation;
{
AVCaptureConnection *videoConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]];
if ([videoConnection isVideoOrientationSupported])
[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
[[self movieFileOutput] startRecordingToOutputFileURL:[self outputFileURL] recordingDelegate:self];
}
-(void)stopRecording
{
[[self movieFileOutput] stopRecording];
}
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.