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
}
Related
I am getting video data from server. i am using signalR Framework. first of all i am capturing video from iPhone photo library. Then next i am converting encoding format (base64EncodedStringwithOptions) method. next i am sending video encrypted data to signalR server. and next i am receiving video data and decrypted and play the video data using AVPlayer. but my problem video does not played. but i received video data from signalR. For decrypting i am using this method. (NSDataBase64DecodingIgnoreUnknownCharacters)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *videoFile=[documentDirectory stringByAppendingPathComponent:post.mediaName];
NSURL *url;
if ([[NSFileManager defaultManager] fileExistsAtPath:videoFile]) {
url=[NSURL URLWithString:videoFile];
}
NSLog(#"Video URL is:%#",url);
AVPlayer *player = [AVPlayer playerWithURL:url];
cell.videoPlayerController.player = player;
[player play];
i got Video URL like this:
Video URL is:/Users/Library/Developer/CoreSimulator/Devices/EEC96DE7-6D0F-4D80-82B8-3C24E4F6B3EF/data/Containers/Data/Applicatication video0006.mp4
You can use MPMoviePlayerController to play video from url' but make sure that you have to declare property for that like,
#property MPMoviePlayerController *videoController;
don't create local object of MPMoviePlayerController.
you can use it something like,
self.videoController = [[MPMoviePlayerController alloc] init];
[self.videoController setContentURL:tempView.videoURL]; //here your url
[self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.videoController.view];
// close or cancel button if you want to put
UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60, 20, 60, 30)];
[closeButton addTarget:self action:#selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
[closeButton setTitle:#"Cancel" forState:UIControlStateNormal];
[self.videoController.view addSubview:closeButton];
// add observer to notify when playing will be completed if you want to put
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoController];
[self.videoController play];
Update :
You must use file url like,
NSURL *url = [NSURL fileURLWithPath:videoFile];
instead of
[NSURL URLWithString:videoFile];
for my app I want the user to record a video which will be stored in the caches directory for repeated use throughout the application.
NSString *DestFilename = # "output.mov";
//Set the file save to URL
NSLog(#"Starting recording to file: %#", DestFilename);
NSString *DestPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
DestPath = [paths objectAtIndex:0];
DestPath = [DestPath stringByAppendingPathComponent:DestFilename];
NSURL* saveLocationURL = [[NSURL alloc] initFileURLWithPath:DestPath];
[MovieFileOutput startRecordingToOutputFileURL:saveLocationURL recordingDelegate:self];
The NSLog says:
url = file:///var/mobile/Containers/Data/Application/BD41ABB0-77BA-4872-B447-07906A3C6FA7/Library/Caches/output.mov
How can I load or possibly embed this video into one of the ViewControllers in my storyboard?
Basically, your video URL is there, so you can load the video into your view controller and play it in 2 possible approaches: using MPMoviePlayerController or AVPlayer
For example from your view controller
//The first approach
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:saveLocationURL];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, 1024, 768)];
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController play];
//The second approach
self.playerItem = [AVPlayerItem playerItemWithURL:saveLocationURL];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer:layer];
[self.player play];
Hope this will help you.
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!!!
I have a ViewController which plays a movie. If the movie was downloaded (with ASI-HTTP-Request) it takes the local version. Otherwise it streams it from the web. It's the exact same movie.
Streaming does work very well, but playing the local file does not work. Just a black screen and no controls. The file has been downloaded correctly I checked the md5 checksum.
It's a Apple QuickTime Video (.mov) at a size of 1024x5xx pixel.
player = [[MPMoviePlayerController alloc] init];
[player.view setFrame: self.view.bounds];
if (local) {
// DOES not work
// SHOULD be [NSURL fileURLWithString:...]
[player setContentURL: [NSURL URLWithString: #"/path/to/the/movie.mov"]];
} else {
// does work
[player setContentURL: [NSURL URLWithString: #"http://mydomain.tld/path/to/the/movie.mov"]];
}
[self.view addSubview: player.view];
[player play]
I tried around with the players MediaSourceType. But it didn't help.
Why does it not work?
I don't have any error messages because I dont receive one..., is there a way to get error messages from it?
Solution
I found my error.
I used [NSURL URLWithString: #"/the/path/..."] for the local path.
It works with [NSURL fileURLWithPath: #"/the/path/..."]
Check URL to video file. This line return path to Documents directory.
NSString* docPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Your path is not related to Document directory
[player setContentURL: [NSURL URLWithString: #"/path/to/the/movie.mov"]];
Try this
[player setContentURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#%#", docPath, #"/path/to/the/movie.mov"]]];