iOS_newbie
iOS_newbie

Reputation: 31

AVAudioPlayer plays on silent mode when MPMoviePlayer plays

The issue is that AVAudioPlayer in itself behaves as it should. It plays background music when the device is not in silent mode (silent/ringer switch is on). When the silent switch is on, it also respects this state and does not play the background music.

However, one peculiar scenario that the AVAudioPlayer plays in silent mode is when I play a video in MPMoviePlayer.

In my app, the BG sound is played first before playing the movie.

Can anyone help? This only occurs when I play the movie.

For other details:
BG Loop used - m4a type
Movie type used - m4v

I also found out that this scenario repeatedly surfaces in iOS 5 but it sometimes happen even in the latest versions.

I've researched everywhere in Google, Apple's documents, and in SO but I couldn't find anything regarding this particular scenario.

I even tried explicitly setting the AVAudioSession's category to AVAudioSessionCategoryAmbient and then calling setActive. (Also tried the setCategory:withOptions:error method but the app crashes for some reason -- tested on device)

I even tried setting the delegate and implementing methods when I tried to use the AVAudioPlayerDelegate.

Help anyone? :(

Upvotes: 0

Views: 895

Answers (2)

ajithmn89
ajithmn89

Reputation: 249

try to set the audio session in this way

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        NSError *err = nil;
        [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
        if(err){
            NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        }
        [audioSession setActive:YES error:&err];
        err = nil;
        if(err){
            NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        }

Upvotes: 1

Arun
Arun

Reputation: 3404

NSURL *url = [NSURL URLWithString:self.videoURL];
            MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
            NSError *_error = nil;
            [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];
            [self presentMoviePlayerViewControllerAnimated:mpvc];

Add this framework

#import <AVFoundation/AVFoundation.h>

try like this...

Upvotes: 1

Related Questions