Video is not playing in ios8 using AVPlayer - objective-c

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

Related

How play a .tmp file with MPMoviePlayerController in objective-c

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];
}

MPMoviePlayerController: sound but no picture in fullscreen mode

I'm using the following code to play a movie on an iPad 1 running iOS6:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myMovie.mov" ofType:#""]];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
// Use the new 3.2 style API
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
[self.moviePlayer prepareToPlay];
self.moviePlayer.view.frame = CGRectMake(0, 0, 1024, 768);
[self.uiView addSubview:self.moviePlayer.view];
//[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
This works.
If however I uncomment the line for setFullscreen, I get the movie audio but with a completely black screen, no picture.
I've tried changing the order of a few lines, in particular the play call and the setFullscreen call, with no effect.
Update
This question appears to be directly relevant:
MPMoviePlayerController in full screen issue (showing blank black screen)
first of all change this line : [[NSBundle mainBundle] pathForResource:#"myMovie.mov" ofType:#""] to: [[NSBundle mainBundle] pathForResource:#"myMovie" ofType:#"mov"] and try again
Use this as its working for me:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myMovie" ofType:#"mov"]];
MPMoviePlayerViewController *movieView = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
movieView.moviePlayer.scalingMode = MPMovieScalingModeFill;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
[movieView.moviePlayer.view setFrame:CGRectMake(0,0,480,320)];
else
[movieView.moviePlayer.view setFrame:CGRectMake(0,0,1024,768)];
movieView.moviePlayer.view.userInteractionEnabled = FALSE;
movieView.moviePlayer.controlStyle=0;
[movieView setFullscreen:YES];
self.view=movieView.view;

MPMoviePlayerController fullscreen not working

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];

MPMoviePlayerController does not work in ios 5

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];

How to set the thumbnail image got from MpMoviePlayerController as a thumbnail when loading the video url string into webview?

I got the thumbnail Image by passing the video url to the MPMoviePlayerController and I am playing the video by loading the Url string into the webview How can I set the Thumbnailimage as a thumnailwhen playing the video in webview at present I am getting white screen with play button.
My code is:
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:newURL];
if (moviePlayer)
{
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer setFullscreen:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleDurationAvailableNotification:)
name:MPMovieDurationAvailableNotification
object:moviePlayer];
moviePlayer.useApplicationAudioSession = NO;
UIImage *image = [moviePlayer thumbnailImageAtTime:(NSTimeInterval)1.0 timeOption: MPMovieTimeOptionNearestKeyFrame];
NSData *imgData = UIImagePNGRepresentation(image);
printf("\n length of ThumbnailImage Data...%d",[imgData length]);
// May help to reduce latency
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
///moviePlayer.view.frame = CGRectMake(184, 200, 400, 300);
//[self.view addSubview:moviePlayer.view];
//[moviePlayer play];
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
printf("\n Inside didSelect method....!!");
masterAisleItemListIndexArray = (NSMutableArray*)[ [masterAisleItemListDictionary allKeys] sortedArrayUsingSelector:#selector(compare:)];
NSString* sectionName = [masterAisleItemListIndexArray objectAtIndex:indexPath.section];
NSMutableArray* aisleItemsArray = [masterAisleItemListDictionary valueForKey:sectionName];
CustomerDetails *custObj = [aisleItemsArray objectAtIndex:indexPath.row];
//CustomerDetails *custObj = [videosList objectAtIndex:indexPath.row];
printf("\n audiofile = %s",[custObj.audioFile UTF8String]);
//NSURL *URL = [NSURL URLWithString:custObj.audioFile];
//printf("URL====%s",[URL UTF8String]);
webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 440)];
[myView addSubview:webView];
webView.backgroundColor = [UIColor blueColor];
[webView loadHTMLString:[self getVideo:custObj.audioFile] baseURL:[NSURL URLWithString:#"http:bluepal.com"]];
webView.delegate = self;
self.view = myView;
//[self downloadOutstandingFileSounds:custObj.audioFile];
}
-(NSString *)getVideo:(NSString *)aString
{
NSString *htmlString = #"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 94\"/></head><body style=\"background:#000;margin-top:0px;margin-left:0px\"><div><object width=\"90\" height=\"70\"><embed src=\"";
htmlString = [htmlString stringByAppendingString:aString];
htmlString = [htmlString stringByAppendingString:#"\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"400\"></embed></object></div></body></html>"];
printf("\n htmlString.......%s",[htmlString UTF8String]);
return htmlString;
}
Guys please give me suggestions to get out of this.
Hope I wil get quick response from your side.
Thanks all,
Lakshmi.