How to make MPMoviePlayerController ignore the mute switch

泄露秘密 提交于 2019-12-03 16:16:34

问题


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?


回答1:


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
}



回答2:


_player.useApplicationAudioSession = NO;



回答3:


After you import AVFoundation just put this in your delegate:

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




回答4:


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
}



回答5:


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.
}


来源:https://stackoverflow.com/questions/4388801/how-to-make-mpmovieplayercontroller-ignore-the-mute-switch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!