Question

I am starting to get convinced that iOS is just pure magic. I looked through numerous posts on AVAudioPlayer for solutions, but none worked.

When the application starts I have a movie starting off from MPMoviePlayerController and I have AVAudioPlayer kicking off with a sound as follows:

    -(void)createAndPlayMovieFromURL:(NSURL *)movieURL sourceType:(MPMovieSourceType)sourceType{

        /* Create a new movie player object. */
       player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
       player.controlStyle = MPMovieControlModeDefault;

        if (player) 
        {
            /* Save the movie object. */
            [self setMoviePlayerController:player];

            /* Specify the URL that points to the movie file. */
            [player setContentURL:movieURL];        

            /* If you specify the movie type before playing the movie it can result 
             in faster load times. */
            [player setMovieSourceType:sourceType];

            CGRect frame = CGRectMake(0, 0, 480, 320);

            /* Inset the movie frame in the parent view frame. */
            [[player view] setFrame:frame];

            [player view].backgroundColor = [UIColor blackColor];

            /* To present a movie in your application, incorporate the view contained 
             in a movie player’s view property into your application’s view hierarchy. 
             Be sure to size the frame correctly. */
            [self.view addSubview: [player view]];        
        }
    [[self moviePlayerController] play];
} 

and for sound:

- (void)setupAudioPlayBack:(NSString *) path : (NSString *) extension{

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:path
                                         ofType:extension]];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:url
                   error:&error];
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@",
              [error localizedDescription]);
    } else {
        audioPlayer.delegate = self;
        [audioPlayer prepareToPlay];
    }
}

And now lets talk magic. When the first movie plays and the sound, the sound is not responding to either the volume controls on the phone nor to Silent Mode On switch.

But then immediately a second movie plays with its own sound and everything works fine - there is no sound with Silent Mode On, if Silent Mode Off it responds to Volume Controls. 3rd movie - plays perfectly too.

I tried using the player.useApplicationAudioSession = NO for MPMoviePlayerController - fixed the problem with the first time sound not responding to Silent Mode or Volume Controls, but now when the second movie starts to play it freezes instantly and the sound played is cut from the original size to something around 2 seconds.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil]; didn't help either wherever I set it.

So my question is - how to make the MPMoviePlayerController interact with AVAudioPlayer without any problems...

Was it helpful?

Solution

Ok I actually found a solution (once again, magic).

When you start your ViewController you set it up as follows:

 - (void)viewDidLoad
{
    [self setupAudioPlayBack:@"sound1" :@"caf"];
    [self playMovieFile:[self localMovieURL:@"mov1" :@"mov"]];
    audioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playAudio) userInfo:nil repeats:NO];

    [super viewDidLoad];
}

Where the playAudio method is simply:

 - (void)playAudio{

    [audioPlayer play];
}

Just 0.1 millisecond timer is enough to make everything work (0.0 doesn't work) and make sure a new video doesn't start before the audio finishes or it will destroy the settings so you will have to invalidate the timer and redo the timer again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top