How to make MPMoviePlayerController ignore the mute switch

后端 未结 5 1884
無奈伤痛
無奈伤痛 2021-02-07 02: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?

相关标签:
5条回答
  • 2021-02-07 02:29

    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
    }
    
    0 讨论(0)
  • 2021-02-07 02:43

    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.
    }
    
    0 讨论(0)
  • 2021-02-07 02:44

    After you import AVFoundation just put this in your delegate:

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

    0 讨论(0)
  • 2021-02-07 02:45
    _player.useApplicationAudioSession = NO;
    
    0 讨论(0)
  • 2021-02-07 02:47

    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
    }
    
    0 讨论(0)
提交回复
热议问题