如何使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. }
- 在UILocalNotification声音比AVAudioPlayer在最大音量
- 与AVAudioSessionCategoryPlayAndRecord组合在一起的低录音量
- AVAudioPlayer在locking屏幕上
- iOS – AVAudioPlayer在后台不会继续播放下一首歌曲
- 使用AVCaptureAudioDataOutputSampleDelegate无法播放通过语音录制的audio
- iOS AudioSessionSetActive()阻塞主线程?
- AVAudioSession类别不起作用的文件指示
- AVAudioRecorder支持什么格式的声音录制?
- 相机冻结时打开的应用程序,而电话正在运行