Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS5: MPMoviePlayerController VIDEO ONLY, NO AUDIO

This is not the No video, audio only problem. It's just the opposite. The problem arises when using iOS 5.0. iPads running 4.3 or lower play the same video files flawlessly.

since iOS 5 changed the way stuff is initialized for MPMoviePlayerControllers, I had to do some SDK based programming in order to get the video to be displayed. Before implementing the snippet I'm showing next, the video and it's controls won't even show up on the screen. The controller would only show a black square with the size and origin of given CGRect frame.

The way I handle it is the following:

The video files are located on the documents folder. So the NSURL has to be initialized as fileURLWithPath. Once that's done, I proceed to initialized the controller with a given frame. Since it wouldn't work otherwise, the view will only add the player once it has changed its loadState. That's achieve by subscribing to a notification. the subscriber selector performs the addition of the controller's view to the parent view on the main thread since the notification could be handled from other threads.

Initializing and adding video 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];
        }
    }

}

Thank you for reading this huge question. I appreciate your answers.

cheers.

like image 484
Pacu Avatar asked Oct 30 '11 22:10

Pacu


2 Answers

Try this: Set MPMoviePlayerController's property "useApplicationAudioSession" to "NO".

like image 118
Tony Z Avatar answered Jan 04 '23 04:01

Tony Z


Apparently there's a bug, but it's not related to the MPMoviePlayerController, but to iOS 5 itself.

My iPad was muted from the switch but still played audio from iPod app anyway so I didn't realized that it was that way, so MPMoviePlayerController was fine, but part of the OS did not notice that the iPad was muted.

I've filed the corresponding bug on Apple's bug tracker. Bug ID# 10368531.

I Apologize if I've wasted your time.

UPDATE: Got feedback from apple for the bug. It's expected behavior. :\

like image 29
Pacu Avatar answered Jan 04 '23 04:01

Pacu