i have a problem while working with MPMoviePlayerController. It doesnot work in ios 5 just a black screen to show. Not even throws any exception or any error.
Here is my code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"cavity" ofType:#"mov"];
NSLog(#"%#",path);
_movieController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
[_movieController prepareToPlay];
[_movieController setFullscreen:NO];
[_movieController.view setFrame:CGRectMake(10, 25, 287, 213)];
[_movieController setControlStyle:MPMovieControlStyleEmbedded];
[_movieView addSubview:_movieController.view];
[_movieController play];
The _movieController object has been declared in .h file but still not in working condition.
try this
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[self presentMoviePlayerViewControllerAnimated:mp];
Related
NSURL *url=[NSURL URLWithString:#"https://m.youtube.com/watch?v=E9wVJJhDOYQ&t=0s"];
NSLog(#"url is %#", url);
MPMoviePlayerController * _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[_moviePlayer view]setFrame:CGRectMake(50, 100, 200, 200)];
[_moviePlayer setFullscreen:NO animated:YES];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayer.shouldAutoplay= YES;
[[self view] addSubview: [_moviePlayer view]];
I download a video file .mp4 from server with NSURLSessionDownloadTask but in the delegate didFinishDownloadingToURL:(NSURL *)location, location is a .tmp file like this
file:///private/var/mobile/Applications/BEA4F9F8-D685-468F-B96C-AE7890ACFC5E/tmp/CFNetworkDownload_LQuGoo.tmp
When I try to initialize MPMoviePlayerController console show this error
_itemFailedToPlayToEnd: { kind = 1;new = 2; old = 0;} and screen show black
I researched and I can save the .tmp file and play .mp4 from my directory but I don't want to do that, I want to know if there is a way to play directly with MPMoviePlayerController. My code
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURLSessionDownloadTask * downloadTask =[downloadSession downloadTaskWithURL:self.userRecord.URL completionHandler:nil];
[downloadTask resume];
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
self.userRecord.locationData = location;
[(NSObject *)self.delegate performSelectorOnMainThread:#selector(fileDownloaderDidFinish:) withObject:self waitUntilDone:NO];
return;
}
-(void)fileDownloaderDidFinish:(FileDownloader *)fileDownloader {
[self setVideoPlayer:[[MPMoviePlayerController alloc] initWithContentURL:fileDownloader.userRecord.locationData]]
;
[self.videoPlayer setControlStyle:MPMovieControlStyleNone];
[self.videoPlayer.view setFrame:CGRectMake(0, 0, 320, 320)];
[self.videoPlayer prepareToPlay];
[self.vwContainerVideo addSubview:self.videoPlayer.view];
[self.vwContainerVideo bringSubviewToFront:self.videoPlayer.view];
[self.vwContainerVideo bringSubviewToFront:self.btPlay];
[self.vwContainerVideo bringSubviewToFront:self.imPlay];
[self.vwContainerThumbnail setHidden:YES];
[AnimationHelper fadeIn:self.vwContainerVideo withDuration:0.5 andWait:0.5];
[self.vwContainerVideo setHidden:NO];
[self.videoPlayer play];
}
My decision was to save file .tmp in a directory and init MPMoviePlayerController with the url from directory.
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *downloadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURLSessionDownloadTask * downloadTask =[downloadSession downloadTaskWithURL:self.userRecord.URL completionHandler:nil];
[downloadTask resume];
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
self.userRecord.data = [NSData dataWithContentsOfURL:location];
[self.userRecord.data writeToFile:[rootDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/%#.%#",self.userRecord.directory,self.userRecord.nameFile, self.userRecord.ext]] options:NSDataWritingWithoutOverwriting error:&error];
self.userRecord.locationData = [NSURL fileURLWithPath:[rootDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/%#.%#",self.userRecord.directory,self.userRecord.nameFile, self.userRecord.ext]]];
[(NSObject *)self.delegate performSelectorOnMainThread:#selector(fileDownloaderDidFinish:) withObject:self waitUntilDone:NO];
return;
}
-(void)fileDownloaderDidFinish:(FileDownloader *)fileDownloader {
[self setVideoPlayer:[[MPMoviePlayerController alloc] initWithContentURL:fileDownloader.userRecord.locationData]]
;
[self.videoPlayer setControlStyle:MPMovieControlStyleNone];
[self.videoPlayer.view setFrame:CGRectMake(0, 0, 320, 320)];
[self.videoPlayer prepareToPlay];
[self.vwContainerVideo addSubview:self.videoPlayer.view];
[self.vwContainerVideo bringSubviewToFront:self.videoPlayer.view];
[self.vwContainerVideo bringSubviewToFront:self.btPlay];
[self.vwContainerVideo bringSubviewToFront:self.imPlay];
[self.vwContainerThumbnail setHidden:YES];
[AnimationHelper fadeIn:self.vwContainerVideo withDuration:0.5 andWait:0.5];
[self.vwContainerVideo setHidden:NO];
[self.videoPlayer play];
}
Video stored in documents directory is not playing if i run app in ios8 but works perfect if i run in ios7
I am downloading video in documents directory using AFNetworking.Here is the code.
NSURLRequest *request = [NSURLRequest requestWithURL:urlVideo];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *path = [dataPath stringByAppendingPathComponent:[urlVideo lastPathComponent]];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.isDownloadingComplete = YES;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Video Downloading Failed");
}];
[operation start];
And i am playing that downloaded video using AVPlayer. Here is the code.
NSURL* playbackURL = [[NSURL alloc] initFileURLWithPath:strPath isDirectory:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[_pl currentItem]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_pl currentItem]];
AVAsset *asset = [AVURLAsset URLAssetWithURL:playbackURL options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
_pl = [AVPlayer playerWithPlayerItem:anItem];
_pl.volume = 1.0;
_pl.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_pl];
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
layer.frame = CGRectMake(0, 0, 305, 306);
[self.contentView.layer addSublayer: layer];
[_pl play];
I think issue is with Documents Directory as i run the app, the document path is changing every time, but i am sending correct path every time. Here is reference: http://pinkstone.co.uk/where-is-the-documents-directory-for-the-ios-8-simulator/
I finally solved it by following code
I just replaced
[self.contentView.layer addSublayer: layer];
with
[self.layer addSublayer:layer];
And it is working fine, but still i am not getting what is the problem with [self.contentView.layer addSublayer: layer];
Let me know if anyone have strong proof.
Thanks
My code plays the video properly but not in full screen.
-----------
|_______ |
|XXXXXXX| |
|XXXXXXX| |
-----------
Its size is about the size of the X filed area.
I am running this on an ipad2.
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"movie.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayer];
[self.view addSubview:videoPlayer.view];
[videoPlayer setFullscreen:YES];
videoPlayer.view.frame = [[UIScreen mainScreen] bounds];
[videoPlayer play];
[super viewDidLoad];
Set the frame of your MPMoviePlayerController to 1024x768.
videoPlayer.view.frame = CGRectMake(0,0,1024,768); //748 if you have status bar.
Hope this is what you were looking for.
*edit: Try this way, it's the one I use.
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[self.view addSubview:tempView];
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"www.orangebob.com/files/movie.mp4"]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
playerViewController.view.frame = CGRectMake(0, 0, 1024, 768);
[tempView addSubview:playerViewController.view]; //This *fixes* it, but it's kinda dirty.
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
I am working on MPMoviePlayerController, I am playing a video which is in URL #"http://www... xyz... ITs not playing in simulator.
I am
NSString *url = #"hhttp://www... xyz... ";
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"video.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(50, 50, 200, 200);
[moviePlayer play];