This is not a "No video, audio" problem. It is just the opposite . The problem occurs when using iOS 5.0. iPads running 4.3 or lower play the same video files flawlessly.
since iOS 5 changed the way the file was initialized for MPMoviePlayerControllers, I had to do some SDK-based programming to display the video. Before implementing the fragment, which I will show below, the video and its controls will not be displayed on the screen. The controller will only show a black square with the size and start of the specified CGRect frame.
Processing Method:
Video files are in the document folder. Thus, the NSURL must be initialized as a file calledURLWithPath. After that, I proceed to initialize the controller with the given frame. Since this does not work otherwise, the view will add the player only after it changes its loadState. This is achieved by subscribing to the notification. the subscriber selector performs the addition of the controller view to the parent view in the main stream , since the notification can be processed from other streams.
Initializing and adding videos to the view:
-(void)addVideo:(NSString*) videoName onRect:(CGRect)rect { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; iPadMagazineAppDelegate *appDelegate = GET_APP_DELEGATE; NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *dirName = [dirArray objectAtIndex:0]; // get directory name for this issue NSURL *baseURL; /* BUGFIX: Video does not work on iOS 5.0 */ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")){ baseURL = [[NSURL fileURLWithPath:dirName]URLByAppendingPathComponent:[appDelegate.currentIssue getIssueDirectoryName ]]; }else { baseURL = [[NSURL URLWithString:dirName] URLByAppendingPathComponent:[appDelegate.currentIssue getIssueDirectoryName]]; } /* end BUGFIX: Video does not work on iOS 5.0 */ NSURL *videoURL = [baseURL URLByAppendingPathComponent:videoName]; MPMoviePlayerController * movieController= [[MPMoviePlayerController alloc]initWithContentURL:videoURL]; // set frame for player movieController.view.frame = rect; // set auto resizing masks [movieController.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; // don't auto play. [movieController setShouldAutoplay:NO]; [movieController setUseApplicationAudioSession:YES]; /* BUGFIX: Video does not work on iOS 5.0 */ if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"5.0")) { [movieController prepareToPlay]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadVideo:) name:MPMoviePlayerLoadStateDidChangeNotification object:movieController]; }else { [pdfView addSubview:movieController.view]; [pdfView bringSubviewToFront: movieController.view]; } /* end BUGFIX: Video does not work on iOS 5.0 */ [_moviePlayerViewControllerArray addObject:movieController]; [movieController release]; [pool release]; }
notification handler:
-(void)loadVideo:(NSNotification*)notification { for (MPMoviePlayerController *movieController in _moviePlayerViewControllerArray) { if (movieController.loadState != MPMovieLoadStateUnknown) { [pdfView performSelectorOnMainThread:@selector(addSubview:) withObject:movieController.view waitUntilDone:YES]; [pdfView performSelectorOnMainThread:@selector(bringSubviewToFront:) withObject:movieController.view waitUntilDone:YES]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:movieController]; } } }
Thanks for reading this huge question. I appreciate your answers.
greetings.
Pacu
source share