iPad – iOS5: MPMOVIEPLAYERCONTROLLER Only video, no audio

This is not No video, the only problem with audio. On the contrary.
There is a problem when using iOS 5.0. iPad running version 4.3 or lower can play the same video file perfectly .

Because iOS 5 changed the way to initialize MPMoviePlayerControllers, I had to do some SDK-based programming to display the video. Before implementing the fragments I will display next, the video and its controls did not even Will appear on the screen. The controller only displays the black square with the size and origin of the given CGRect frame.

The way I handle it is as follows:

The video file is located in the document folder So NSURL must be initialized to fileURLWithPath. After it’s done, I continue to initialize the controller with the given framework. Since it doesn’t work, the view will only add the player after changing its loadState. This is achieved by subscribing to notifications. The subscriber selector executes adding the controller view to the parent view on the main thread, because notifications can be handled from other threads.

Initialize the video and add it to the view:

-(void)addVideo:(NSString*) videoName onRect:(CGRect)rect {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
< br /> 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 setUs eApplicationAudioSession:YES];

/*
BUGFIX: Video does not work on iOS 5.0
*/
if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(@"5.0")) {< br /> [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 != MPMovieLoadS tateUnknown) {


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

}

Thank you for reading this big question. I appreciate your answer.

Cheers.

< div class="answer"> Try this: Set the property "useApplicationAudioSession" of MPMoviePlayerController to "NO".

This is not No video, the only problem with audio. On the contrary.
There is a problem when using iOS 5.0. iPad running 4.3 or lower can play the same video file perfectly.

Since iOS 5 changed the way to initialize MPMoviePlayerControllers, I had to do something SDK-based programming can display the video. The video and its controls will not even appear on the screen until the fragments I display next are implemented. The controller only displays the black square with the size and origin of the given CGRect frame.

The way I handle it is as follows:

The video file is located in the document folder. So NSURL must be initialized to fileURLWithPath. After it’s done, I continue to initialize the controller with the given frame. Because it doesn’t Works, the view will only add after changing its loadState Add player. This is achieved by subscribing to notifications. The subscriber selector executes adding the controller view to the parent view on the main thread, because notifications can be processed from other threads.

Initialize the video and add Add it 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 object :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 ]];< br /> }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:MPMoviePlayerLoadStateDidChangeNotifica tion 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];< br />}

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

}

Thank you for reading this big question. I appreciate your answer.

p>

Cheers.

Try this: Set the property "useApplicationAudioSession" of MPMoviePlayerController to "NO".

< /p>

Leave a Comment

Your email address will not be published.