如何使MPMoviePlayerController忽略静音开关

我想使用MPMoviePlayerController播放video,但是我希望它忽略静音开关,类似于Youtubevideo播放器的行为。

有任何想法吗?

使用AVAudioSession类别AVAudioSessionCategoryPlayback ,您的应用程序将忽略像Youtube应用程序的静音开关。

例如(由Ken Pletzer在评论中启发):

 #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 } 
 _player.useApplicationAudioSession = NO; 

在你导入AVFoundation后,把这个放在你的委托中:

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

在Swift中:在播放声音/video之前做一次(例如在你的应用程序开始时)

 do{ try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) } catch { //Didn't work } 

对于将来的任何人,我知道这已经被回答了,但是我在我的应用程序中播放了一个video,导致像spotify,youtube等应用程序停止播放它的audio,所以我最终使用了这个问题:

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