I have this function to write NSData to document dictionary -
- (void)writeData
{
NSString *str = #"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:str];
NSLog(#"%#",path);
NSLog(#"About to Write Data");
NSData* data0 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];
[data0 writeToFile:path atomically:YES];
NSLog(#"Writting Data Completed");
}
Later i read this data and try to play the video using MPMoviePlayerController, but unfortunately this
does not play-
- (void)readData
{
NSString *str = #"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:str];
NSURL* url = [[NSURL alloc] initFileURLWithPath:path];
NSLog(#"url:%#",url);
NSError *error = nil;
BOOL isReacahble = [url checkResourceIsReachableAndReturnError:&error];
NSLog(#"isReachable :%i,%#",isReacahble,error);
BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:path];
if(fileExist)
{
NSLog(#"File Exist in path");
}
else
{
NSLog(#"NO File Exist");
}
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer prepareToPlay];
NSLog(#"movie player:%#",moviePlayer);
[moviePlayer setControlStyle: MPMovieControlStyleFullscreen];
[moviePlayer.view setFrame:CGRectMake(0, 0,320, 480)];
[self.view addSubview:moviePlayer.view];
moviePlayer.shouldAutoplay = YES;
[moviePlayer play];
}
When i try to play this it shows following error-
2013-01-17 11:21:07.067 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay for pause
2013-01-17 11:21:07.068 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay
2013-01-17 11:21:07.090 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-17 11:21:07.091 VideoCheck[338:907] movie player:<MPMoviePlayerController: 0x21078250>
2013-01-17 11:21:07.093 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-17 11:21:07.102 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
2013-01-17 11:21:07.113 VideoCheck[338:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-17 11:21:07.114 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2013-01-17 11:21:07.115 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
2013-01-17 11:21:07.121 VideoCheck[338:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2013-01-17 11:21:07.149 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
Can someone please run the code and provide some information about what's happening here??Please someone help me. I am using iOS 6.
In your wrieData method, instead of giving URL name as file, give only the name of the video. Like
NSString *path = [documentsDirectory stringByAppendingPathComponent:[str lastPathComponent]];
and do the same in readData method too.
Related
In our iOS app, we will receive flv**(Container)** file having video and audio streaming something like this
Input #0, flv, from 'test.flv':
Metadata:
streamName : flvLiveStream
encoder : Lavf55.12.100
Duration: 00:00:48.00, start: 66064.401000, bitrate: 632 kb/s
Stream #0:0, 41, 1/1000: **Video: h264 (Baseline)**, 1 reference frame, yuv420p(progressive, left), 1280x720, 0/1, 15 fps, 1k tbr, 1k tbn
Stream #0:1, 14, 1/1000: **Audio: pcm_alaw,** 8000 Hz, mono, s16, 64 kb/s
and this needs to be converted to mp4 container and format as well, I am trying using ffmpeg, I believe thats only the way , using transcoding.c file but failed at this stage
Impossible to convert between the formats supported by the filter 'in' and the filter 'auto_scaler_0'
I am trying to learn from OSX command like ffmpeg -I test.flv test.mp4 ,
is that feasible to port to iOS
will it work in all different scenarios,
In Summary
--- What is the best possible way to convert flv to mp4 on the iOS device
where video will be in h264 and audio will be in swf codec ?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"MyVideo.mp4"];
NSURL *outputURL = [NSURL fileURLWithPath:filePath];
[self convertVideoToLowQuailtyWithInputURL:localUrl outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
{
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
// Video conversation completed
}
}];
- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler {
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
handler(exportSession);
}];
}
I create one application that show file in document folder.
I have one file .mov format that I want show it from document folder but when run app and click play button don't show this movie and I see this image
:
this is my code : (please guide me and tell me my mistake)
- (IBAction)Play:(id)sender
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *file = [documentPath stringByAppendingPathComponent:#"xcode4-outlet.mov"];
NSURL *url = [NSURL fileURLWithPath:file];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[self.view addSubview:_moviePlayer.view];
_moviePlayer.fullscreen = YES;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.allowsAirPlay = YES;
[_moviePlayer play];
}
also compiler get me this massage :
movie player[9489:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
movie player[9489:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
movie player[9489:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
You need to check whether a file is there or not before adding movie player also you can add default
-(IBAction)Play:(id)sender
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *file = [documentPath stringByAppendingPathComponent:#"xcode4-outlet.mov"];
NSURL *url = [NSURL fileURLWithPath:file];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
if(fileExists)
{
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayer];
_moviePlayer.shouldAutoplay=NO;
[_moviePlayer prepareToPlay];
}
}
Add your movie after movie is completely loaded
-(void)moviePreloadDidFinish:(NSNotification*)notification
{
_moviePlayer.controlStyle=MPMovieControlStyleDefault;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer play];
[_moviePlayer setFullscreen:YES animated:YES];
}
You may not be supplying a valid path to the URL. It looks like your trying to append your resource to the documents path, but that resource may not actually be there. You have to write the file to that path first. You should always double check your path with:
if ([[NSFileManager defaultManager] fileExistsAtPath:yourPath])
{
//path exists
}
Otherwise, just init the movie player with the straight up resource if you have it inside of your app.
Your code looks good. Check the file name is correct.. Also give a try like
-(void)playMovie
{
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:#"xcode4-outlet.mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
MPMoviePlayerController *moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:fileURL]retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
// playback completed
}
I used MPMoviePlayerController in my application.
I got the right URL but MPMoviePlayer does not play video it shows only black screen.
My code is as below:
[video setImage:[UIImage imageNamed:#"video_active.png"] forState:UIControlStateNormal];
NSString *filename=[NSString stringWithFormat:#"%#.mp4",[appdel.TempVideoUrl valueForKey:tag]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *movieURL = [NSURL fileURLWithPath:filePath] ;
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(106, 18, 216, 235);
[uploadview addSubview:theMovie.view];
// Play the movie.
[theMovie pause];
I got the URL like this:
file://localhost/var/mobile/Applications/6E1D4CB7-A08D-438B-9FE7-E2FD9B7B5EEC/Documents/136_1355227629.mp4
Add [theMovie prepareToPlay]; before calling [theMovie play];.
prepareToPlay
Prepares a movie player for playback. (required)
- (void)prepareToPlay
Discussion
If a movie player is not already prepared to play when you call the
play method, that method automatically calls this method. However, to
minimize playback delay, call this method before you call play.
Calling this method may interrupt the movie player’s audio session.
For information on interruptions and how to resond to them, see Audio
Session Programming Guide. Availability
Available in iOS 3.2 and later.
Declared In MPMediaPlayback.h
Also you can use isPreparedToPlay for checking whether a movie player is ready to play.
Please refer this link for more.
Desperated.
Hello everybody!
I am having some issues with MPMoviePlayerController.
I've made it working with videos from NSBundle. But that's not what I need. I need to play it from Documents directory, because that is the place I store the recorded videos, wich URLs are stored in CoreData. but lets leave this aside, and simplify the code to its minimum needed. This code actually WORKS if using the contentURL, wich leads to NSBundle. After it, what I do to get to the docs place.
What I do:
NSURL *contentURL = [[NSBundle mainBundle] URLForResource:#"Oct_08_2012_10_00_51" withExtension:#"mp4"]; // this works
NSString* docPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * docaPathFull = [NSString stringWithFormat:#"%#%#", docPath, #"/Oct_08_2012_10_00_51.mp4"];
NSURL * docUrl= [NSURL URLWithString: docaPathFull];
BOOL ex = [[NSFileManager defaultManager] fileExistsAtPath:docaPathFull];
NSLog(#"file exists: %d, path using docPath: %#",ex, docaPathFull);
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:docUrl];
player.shouldAutoplay = YES;
player.controlStyle = MPMovieControlStyleEmbedded;
[player.view setFrame: self.thumbButton.bounds];
[player prepareToPlay];
[self.view addSubview: player.view];
[player play];
What we have:
2012-10-08 13:14:43.532 Voto[11968:907] file exists: 1, path using docPath: /var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Documents/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:43.907 Voto[11968:907] content URL: file://localhost/var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Voto.app/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:44.265 Voto[11968:907] doc URL: /var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Documents/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:45.343 Voto[11968:907] [MPAVController] Autoplay: Disabling autoplay for pause
2012-10-08 13:14:45.344 Voto[11968:907] [MPAVController] Autoplay: Disabling autoplay
2012-10-08 13:14:46.518 Voto[11968:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2012-10-08 13:14:46.540 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
2012-10-08 13:14:46.554 Voto[11968:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2012-10-08 13:14:46.555 Voto[11968:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2012-10-08 13:14:46.557 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
2012-10-08 13:14:46.567 Voto[11968:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2012-10-08 13:14:46.871 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
So, the file exists...
Questions i've looked through:
MPMoviePlayer load and play movie saved in app documents
MPMoviePlayerController does not work with movie in documents folder
MPMoviePlayerViewController play movie from Documents directory - objective-c
I also checked ut with class reference, nothing specific about playing from Documents.
My projects settings:
using latest iOS 6,
Deployment target 5.0
Testing on both iOS6 iPhone simulator and an iPad with iOS 6.
If i forgot to add something, please remind me, I will do it immediately.
Please, help! :)
Well you'r not building the file URL the correct way, you should do it like this:
NSString *docPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docaPathFull = [docPath stringByAppendingPathComponent:#"/Oct_08_2012_10_00_51.mp4"];
NSURL *docUrl= [NSURL fileURLWithPath:docaPathFull];
You should add directories and file to a path with the stringByAppendingPathComponent method of NSString;
Also when creating the file URL use fileURLWithPath: on NSURL, this willl create a correct NSURL for the giving path.
The most common mistake that everyone do is all use
NSURL *fileURL = [NSURL URLWithString:mVidPath];
^^^^^^^^^^^^^
instead of
NSURL *fileURL = [NSURL fileURLWithPath:mVidPath];
^^^^^^^^^^^^^^^
-(IBAction)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);
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[player.view setFrame: self.view.bounds];
[player.moviePlayer prepareToPlay];
[self.view addSubview:player.view];
player.moviePlayer.controlStyle = MPMovieControlStyleDefault;
player.moviePlayer.shouldAutoplay = YES;
[player.moviePlayer setFullscreen:YES animated:YES];
[player.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated: player];
}
Don't forget to add MediaPlayer.framework and
#import < MediaPlayer/MediaPlayer.h>
in your respective code.
Good Luck!!!
NSString *p = [app getFilePathFor:name];
// Printing description of p:
// /Users/Jackson/Library/Application Support/iPhone Simulator/5.0/Applications/7FE97EDD-D080-4E5E-8EDD-3D9FFA8F6CF5/Documents/6681a4d7-8630-4a12-90c6-f95d72650a42
NSData *data = [NSData dataWithContentsOfFile:p];
// Printing description of data:
// <01401dc2 a15ebc27 e473ee79 316568ff 19dc7000 df3f7c5c dc206870 01a1c00e 00010000 00000000 00e10288 00000003 01060020 08c80038 00080000 88edffbf 02000119 0000050e 00000000 00000000 02000007 0000050f 00000000 00000000>
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
// Printing description of str:
// #¡^¼'äsîy1ehÿÜp
path = [NSURL fileURLWithPath:str];
// Printing description of path:
// %01#%1D%C3%82%C2%A1%5E%C2%BC'%C3%A4s%C3%AEy1eh%C3%BF%19%C3%9Cp%00%C3%9F%3F%7C%5C%C3%9C%20hp%01%C2%A1%C3%80%0E%00%01%00%00%00%00%00%00%00%C3%A1%02%C2%88%00%00%00%03%01%06%00%20%08%C3%88%008%00%08%00%00%C2%88%C3%AD%C3%BF%C2%BF%02%00%01%19%00%00%05%0E%00%00%00%00%00%00%00%00%02%00%00%07%00%00%05%0F%00%00%00%00%00%00%00%00 -- file://localhost/
Then I send the path to the MPMoviePlayerViewController like so:
MPMoviePlayerViewController * playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:path];
[self presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController *)playerController];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playerController.moviePlayer play];
playerController=nil;
At this point, the Movie Player shows, then immediate hides once the "show" animation is complete. In the log I get this:
An instance 0x87ca7b0 of class AVPlayerItem was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x87dfc30> (
<NSKeyValueObservance 0x933c280: Observer: 0x93367d0, Key path: nonForcedSubtitleDisplayEnabled, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x933c630>
<NSKeyValueObservance 0x933c360: Observer: 0x93356b0, Key path: presentationSize, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x87b53b0>
I have no idea where to go next.. help?
** EDIT **
the MP4 I'm trying to play is coming from this buckbunny test clip. When I load the clip straight in form the URL it works. But if I download it and save it via NSData and try to reopen the NSData this no longer works.
** EDIT 2 **
#Till: "You are initializing a URL with a string that is garbledegock; // str=#"#¡^¼'äsîy1ehÿÜp" path = [NSURL fileURLWithPath:str]; What are you expecting here?"
So now that I've tried:
path = [NSURL fileURLWithPath:[app getFilePathFor:name]];
// Printing description of path:
// file://localhost/Users/Jackson/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/CB1CA8D0-B5EC-4B5F-8C7F-0EF449508BA7/Documents/9d425856-14b0-4ad3-835a-970affacb05f
I'm still getting the EXACT same error.
Why are you converting to nsdata from the saved clip file?
You should just load the path for the clip in your app documents directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:name];
NSURL *fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
yourMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];
I think it's due to the fact that you are setting it to nil while it's playing. Actually, it appears that it happens so quickly that the MPMoviePlayerViewController is still loading the material when you set it to nil.