使用AVPlayer 播放音頻,ipad設置靜音後聲音 仍然播放,查資料後須要以下設置 :html
1 func setAudioCAtegory() { 2 let audioSession = AVAudioSession.sharedInstance() 3 audioSession.setCategory(AVAudioSessionCategorySoloAmbient); 4 // audioSession.setCategory(AVAudioSessionCategorySoloAmbient); 5 audioSession.setActive(true) 6 7 }
設置以後 報異常:ios
Call can throw, but it is not marked with 'try' and the error is not handled swift
修改以下:session
1 func setAudioCAtegory() { 2 let audioSession = AVAudioSession.sharedInstance() 3 try! audioSession.setCategory(AVAudioSessionCategorySoloAmbient); 4 try! audioSession.setActive(true) 5 6 }
修改完成以後,我發現本身設置的畫中畫模式不支持了,最後發下 因爲我在AppDelegate 中設置了ui
1 func setPiPAllow(){ 2 let audiosession = AVAudioSession.sharedInstance() 3 do { 4 try audiosession.setCategory(AVAudioSessionCategoryPlayback) 5 } 6 catch { 7 print("Audio session setCategory failed") 8 } 9 }
又在播放器中再一次設置了AVAudioSession.sharedInstance,而且是不一樣的模式。致使畫中畫不支持,結論:目前畫中畫和 靜音不能兼容,只能捨棄靜音模式。spa