2010-12-08 62 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. 
}