2010-12-08 66 views

回答

46

使用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 
} 
+0

更具体地说 NSError* error; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &error]; if(error) { } 2012-04-10 13:44:34

+0

还需要添加AVFoundation框架和#import“AVFoundation/AVAudioSession.h” – 2012-04-10 14:11:52

2
_player.useApplicationAudioSession = NO; 
1

在导入AVFoundation只是把这个在您的委托:

[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback错误:无];

1
斯威夫特

:做一次你在未来(在你的应用程序开始时为例)

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

播放声音/视频之前,任何人,我知道这已经被回答,但我有在我的应用程序播放视频引起应用程序,如Spotify的,YouTube的等停止播放它的音频,所以我结束了使用此问题:

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