Upgrade to Pro — share decks privately, control downloads, hide ads and more …

音声アプリにおけるバックグラウンド再生Tips.pdf

entaku
February 22, 2022
1.1k

 音声アプリにおけるバックグラウンド再生Tips.pdf

entaku

February 22, 2022
Tweet

Transcript

  1. entaku • Name: entaku • Job: iOS/AndroidΤϯδχΞͳͲ • SIer໿6೥ •

    εϙʔπϚονϯάΞϓϦ1೥ • ෺ྲྀITαʔϏε ໿2೥ • Voicy ->New • Twitter • @entaku_0818
  2. AVAudioSession ͷαϯϓϧίʔυ /// Set session category. @available(iOS 3.0, *) open

    func setCategory(_ category: AVAudioSession.Category) throws /// Set session category with options. @available(iOS 6.0, *) open func setCategory(_ category: AVAudioSession.Category, options: AVAudioSession.CategoryOptions = []) throws /// Set session category and mode with options. @available(iOS 10.0, *) open func setCategory(_ category: AVAudioSession.Category, mode: AVAudioSession.Mode, options: AVAudioSession.CategoryOptions = []) throws
  3. AVAudioSession ͷαϯϓϧίʔυ /// Set session category. @available(iOS 3.0, *) open

    func setCategory(_ category: AVAudioSession.Category) throws /// Set session category with options. @available(iOS 6.0, *) open func setCategory(_ category: AVAudioSession.Category, options: AVAudioSession.CategoryOptions = []) throws /// Set session category and mode with options. @available(iOS 10.0, *) open func setCategory(_ category: AVAudioSession.Category, mode: AVAudioSession.Mode, options: AVAudioSession.CategoryOptions = []) throws • VoicyͰར༻
  4. AVAudioSession - category • soloAmbient • ΦʔσΟΦΛόοΫάϥϯυΦʔσΟΦͱϛοΫε͠ͳ͍ΞϓϦ༻ʢσϑΥϧτʣ ͷΧςΰϦͰ͢ɻ • ambient

    • ΦʔσΟΦΛόοΫάϥϯυΦʔσΟΦͱϛοΫε͢ΔΞϓϦ༻ʢϘλϯԻ΍ ΧʔφϏԻ੠Ҋ಺ͷΈͳͲʣͷΧςΰϦͰ͢ɻ • playback • ΦʔσΟΦ࠶ੜ༻ͷΧςΰϦͰ͢ɻ • record • ΦʔσΟΦ࿥Ի༻ͷΧςΰϦͰ͢ɻ https://developer.apple.com/documentation/avfaudio/avaudiosession/category
  5. AVAudioSession - category • playAndRecord • ΦʔσΟΦ࠶ੜͱ࿥ԻΛ྆ํߦ͏ΞϓϦ༻ʢVoIPͳͲʣͷΧςΰϦͰ͢ɻ • multiRoute •

    ΦʔσΟΦΛҟͳΔσόΠεʹಉ࣌ग़ྗ͢ΔΞϓϦ༻ͷΧςΰϦͰ͢ɻ https://developer.apple.com/documentation/avfaudio/avaudiosession/category
  6. AVAudioSession - Mode • default • σϑΥϧτͷϞʔυͰ͢ɻ • gameChat •

    ήʔϜνϟοτͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ • measurement • ΦʔσΟΦೖग़ྗͷଌఆͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ • moviePlayback • өը࠶ੜͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ https://developer.apple.com/documentation/avfaudio/avaudiosession/category
  7. AVAudioSession - Mode • spokenAudio • Ի੠ͷׂΓࠐΈ͕͋ΔΞϓϦͰͷར༻Λࣔ͢ϞʔυͰ͢ɻΧʔφϏԻ੠Ҋ಺ͳͲɺ୹ׂ͍ΓࠐΈͷࡍʹ࠶ੜதͷ΋ ͷΛҰ࣌ఀࢭ͠ɺऴΘΓ࣍ୈ࠶։͠·͢ɻ • videoChat

    • ΦϯϥΠϯϏσΦձٞͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ • videoRecording • ಈը࿥ըͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ • voiceChat • VoIPͳͲͷ૒ํ޲ͷԻ੠௨৴Ͱͷར༻Λࣔ͢ϞʔυͰ͢ɻ • voicePrompt • ςΩετಡΈ্͛Λ࢖༻ͨ͠Ի੠࠶ੜͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ https://developer.apple.com/documentation/avfaudio/avaudiosession/mode/
  8. AVAudioSession - Option • mixWithOthers • ΦʔσΟΦ͕όοΫάϥ΢ϯυΦʔσΟΦͱϛοΫε͢Δ͜ͱΛࣔ͢Φϓγϣϯɻ • duckOthers •

    ΦʔσΟΦ࠶ੜ࣌ʹɺόοΫάϥ΢ϯυΦʔσΟΦͷԻྔΛԼ͛ΔΦϓγϣϯɻ • InterruptSpokenAudioAndMixWithOthers • ΦʔσΟΦΛ࠶ੜ࣌ʹɺόοΫάϥ΢ϯυΦʔσΟΦΛҰ࣌ఀࢭ͢Δ͔Ͳ͏͔Λܾఆ͢ΔΦ ϓγϣϯɻ • allowBluetooth • BluetoothϋϯζϑϦʔσόΠεΛ࢖༻Մೳͳೖྗϧʔτͱͯ͠දࣔ͢Δ͔Ͳ͏͔Λܾఆ͢Δ Φϓγϣϯɻ https://developer.apple.com/documentation/avfaudio/avaudiosession/ categoryoptions
  9. AVAudioSession - Option • spokenAudio • Ի੠ͷׂΓࠐΈ͕͋ΔΞϓϦͰͷར༻Λࣔ͢ϞʔυͰ͢ɻΧʔφϏԻ੠Ҋ಺ͳͲɺ୹ׂ͍ΓࠐΈͷࡍʹ࠶ੜதͷ΋ ͷΛҰ࣌ఀࢭ͠ɺऴΘΓ࣍ୈ࠶։͠·͢ɻ • videoChat

    • ΦϯϥΠϯϏσΦձٞͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ • videoRecording • ಈը࿥ըͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ • voiceChat • VoIPͳͲͷ૒ํ޲ͷԻ੠௨৴Ͱͷར༻Λࣔ͢ϞʔυͰ͢ɻ • voicePrompt • ςΩετಡΈ্͛Λ࢖༻ͨ͠Ի੠࠶ੜͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ https://developer.apple.com/documentation/avfaudio/avaudiosession/categoryoptions
  10. AVAudioSession ͷαϯϓϧίʔυ ɹɹɹɹɹtry? audioSession.setCategory(AVAudioSession.Category.playback) OR ɹɹɹɹɹtry? audioSession.setCategory(AVAudioSession.Category.soloAmbient) OR ɹɹɹɹ// background

    try? audioSession.setCategory(AVAudioSession.Category.ambient, options: AVAudioSession.CategoryOptions.mixWithOthers) ※ίʔυ͸ΠϝʔδͰ͢
  11. backGround͔Ͳ͏͔ͷ൑ఆ ɹɹɹɹɹif (secondaryAudioShouldBeSilencedHint){ ɹ// background try? audioSession.setCategory(AVAudioSession.Category.ambient, options: AVAudioSession.CategoryOptions.mixWithOthers) }else{

    // forground // Ϣʔβʔͷঢ়ଶʹΑͬͯ࢖͍෼͚ try? audioSession.setCategory(AVAudioSession.Category.playback) OR ɹɹɹɹɹtry? audioSession.setCategory(AVAudioSession.Category.soloAmbient) } ɹɹɹ ※ίʔυ͸ΠϝʔδͰ͢
  12. backGround͔Ͳ͏͔ͷ൑ఆ https://developer.apple.com/documentation/avfaudio/avaudiosession/1616600-secondaryaudioshouldbesilencedhi • This property returns true if any other

    audio is playing, including audio from an app using the ambient category. Most apps should instead use the secondaryAudioShouldBeSilencedHint property, because it’s more restrictive when considering whether primary audio from another app is playing. https://developer.apple.com/documentation/avfaudio/avaudiosession/1616610-isotheraudioplaying • secondaryAudioShouldBeSilencedHintΛ ࢖͏