2015-01-28 5 views
3

아이폰으로 음악을 듣고 있습니다. 내 애플리케이션을 실행할 때 배경 음악을 멈추지 말고 배경 음악을 멈춰야합니다. 나는 캔디 쇄파 앱과 동일하게하고 싶다. 이 문제를 해결하려면 어떻게해야합니까? 기본값은 AVAudioSessionCategorySoloAmbient이기 때문에 나에게배경 음악이 앱이 시작되면서 음악이 멈 춥니 다.

var isOtherAudioPlaying = AVAudioSession.sharedInstance().otherAudioPlaying 
     println("isOtherAudioPlaying>>> \(isOtherAudioPlaying)") 
     if(isOtherAudioPlaying == false) 
      { 
      audioPlayer1!.play() 
      } 
+0

앱의 오디오 세션 카테고리 및 옵션을 적절하게 설정해야합니다. https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html – Palpatim

+0

다음 코드에 의해 해결되었습니다 : - ** AVAudioSession.sharedInstance(). setCategory (AVAudioSessionCategoryAmbient, error : nil) == true ** 어디에서 movieplayercontroller를 배치 할 것인가? –

답변

0

도와주세요, 당신은 당신의 앱 적절한 범주를 설정해야하고 활성화. 오디오 믹싱에 AVAudioSessionCategoryAmbient를 사용할 수 있습니다.

당신의 AppDelegate.swift이 추가 :

func applicationDidBecomeActive(application: UIApplication) { 
    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil) 
    AVAudioSession.sharedInstance().setActive(true, error: nil) 
    // ... 
} 

을 즐기십시오!

1

나는 체크 아웃을 권합니다. Sameer's answer to this for Swift 2.0, just a few months back. Swift 2.0에서 완벽하게 작동합니다.

let sess = AVAudioSession.sharedInstance() 
if sess.otherAudioPlaying { 
    _ = try? sess.setCategory(AVAudioSessionCategoryAmbient, withOptions: []) // 
    _ = try? sess.setActive(true, withOptions: []) 
}