21

I want to play a video using MPMoviePlayerController but I want it to ignore the mute switch, similar to the behavior of Youtube's video player.

Any ideas?

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
zohar
  • 2,298
  • 13
  • 45
  • 75

5 Answers5

49

Use the AVAudioSession category AVAudioSessionCategoryPlayback and your app will ignore the mute switch like the Youtube app.

For example (inspired by Ken Pletzer in the comments):

#import <AVFoundation/AVFoundation.h>

// note: you also need to add AVfoundation.framework to your project's 
// list of linked frameworks
NSError *error = nil;
BOOL success = [[AVAudioSession sharedInstance] 
                setCategory:AVAudioSessionCategoryPlayback 
                error:&error];
if (!success) {
    // Handle error here, as appropriate
}
mgrandi
  • 3,389
  • 1
  • 18
  • 17
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • More specifically NSError* error; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &error]; if( error ) { } – Ken Pletzer Apr 10 '12 at 13:44
  • Also need to add AVFoundation framework and #import "AVFoundation/AVAudioSession.h" – Ken Pletzer Apr 10 '12 at 14:11
  • @KenPletzer Thanks Ken, I appreciate your comment. I made a small tweak to your code (check success value rather than error's existence) and put it in the answer. – Carl Veazey Mar 01 '13 at 22:44
2
_player.useApplicationAudioSession = NO;
Andrey
  • 85
  • 4
2

in Swift: Do this once before you play sound/video (for example at the beginning of your application)

do{
  try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
  //Didn't work
}
drpawelo
  • 2,348
  • 23
  • 17
2

For anyone in the future, I know this has been answered already, but I had an issue with playing a video in my app which caused apps like spotify, youtube etc. to stop playing it's audio, so I ending up using this:

NSError *silentSwitcherror = nil;
BOOL silentSwitchSuccess = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&silentSwitcherror];
if (silentSwitchSuccess)
{
//put whatever video code you are trying to play
}
else
{
//put how to handle failed instances.
}
C-H-E-F
  • 165
  • 11
1

After you import AVFoundation just put this in your delegate:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

Catalin
  • 1,821
  • 4
  • 26
  • 32