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

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

entaku
February 22, 2022
950

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

entaku

February 22, 2022
Tweet

More Decks by entaku

Transcript

  1. Ի੠ΞϓϦʹ͓͚Δ
    όοΫάϥ΢ϯυ࠶ੜ
    Tips
    entaku

    View Slide

  2. entaku
    • Name: entaku


    • Job: iOS/AndroidΤϯδχΞͳͲ


    • SIer໿6೥


    • εϙʔπϚονϯάΞϓϦ1೥


    • ෺ྲྀITαʔϏε ໿2೥


    • Voicy ->New


    • Twitter


    • @entaku_0818

    View Slide

  3. Voicy
    https://corp.voicy.jp/
    https://voicy.jp/

    View Slide

  4. Voicy
    https://apps.apple.com/jp/app/voicy-recorder/id1115553195
    iOS Android

    View Slide

  5. ࠓճͷςʔϚ

    View Slide

  6. ໰୊
    • Voicyௌ͖ͳ͕ΒଞͷԻָྲྀΕΔܥίϯςϯπ
    ͷβοϐϯά͕Ͱ͖ͳ͍

    View Slide

  7. AVAudioSession
    https://developer.apple.com/documentation/avfaudio/avaudiose
    • ΞϓϦͰར༻͢ΔԻ੠ͷछྨͳͲΛઃఆ

    View Slide

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


    View Slide

  9. 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Ͱར༻

    View Slide

  10. AVAudioSession - category
    • soloAmbient


    • ΦʔσΟΦΛόοΫάϥϯυΦʔσΟΦͱϛοΫε͠ͳ͍ΞϓϦ༻ʢσϑΥϧτʣ
    ͷΧςΰϦͰ͢ɻ


    • ambient


    • ΦʔσΟΦΛόοΫάϥϯυΦʔσΟΦͱϛοΫε͢ΔΞϓϦ༻ʢϘλϯԻ΍
    ΧʔφϏԻ੠Ҋ಺ͷΈͳͲʣͷΧςΰϦͰ͢ɻ


    • playback


    • ΦʔσΟΦ࠶ੜ༻ͷΧςΰϦͰ͢ɻ


    • record


    • ΦʔσΟΦ࿥Ի༻ͷΧςΰϦͰ͢ɻ
    https://developer.apple.com/documentation/avfaudio/avaudiosession/category

    View Slide

  11. AVAudioSession - category
    • playAndRecord


    • ΦʔσΟΦ࠶ੜͱ࿥ԻΛ྆ํߦ͏ΞϓϦ༻ʢVoIPͳͲʣͷΧςΰϦͰ͢ɻ


    • multiRoute


    • ΦʔσΟΦΛҟͳΔσόΠεʹಉ࣌ग़ྗ͢ΔΞϓϦ༻ͷΧςΰϦͰ͢ɻ
    https://developer.apple.com/documentation/avfaudio/avaudiosession/category

    View Slide

  12. AVAudioSession - Mode
    • default


    • σϑΥϧτͷϞʔυͰ͢ɻ


    • gameChat


    • ήʔϜνϟοτͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ


    • measurement


    • ΦʔσΟΦೖग़ྗͷଌఆͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ


    • moviePlayback


    • өը࠶ੜͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ
    https://developer.apple.com/documentation/avfaudio/avaudiosession/category

    View Slide

  13. AVAudioSession - Mode
    • spokenAudio


    • Ի੠ͷׂΓࠐΈ͕͋ΔΞϓϦͰͷར༻Λࣔ͢ϞʔυͰ͢ɻΧʔφϏԻ੠Ҋ಺ͳͲɺ୹ׂ͍ΓࠐΈͷࡍʹ࠶ੜதͷ΋
    ͷΛҰ࣌ఀࢭ͠ɺऴΘΓ࣍ୈ࠶։͠·͢ɻ


    • videoChat


    • ΦϯϥΠϯϏσΦձٞͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ


    • videoRecording


    • ಈը࿥ըͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ


    • voiceChat


    • VoIPͳͲͷ૒ํ޲ͷԻ੠௨৴Ͱͷར༻Λࣔ͢ϞʔυͰ͢ɻ


    • voicePrompt


    • ςΩετಡΈ্͛Λ࢖༻ͨ͠Ի੠࠶ੜͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ
    https://developer.apple.com/documentation/avfaudio/avaudiosession/mode/

    View Slide

  14. AVAudioSession - Option
    • mixWithOthers


    • ΦʔσΟΦ͕όοΫάϥ΢ϯυΦʔσΟΦͱϛοΫε͢Δ͜ͱΛࣔ͢Φϓγϣϯɻ


    • duckOthers


    • ΦʔσΟΦ࠶ੜ࣌ʹɺόοΫάϥ΢ϯυΦʔσΟΦͷԻྔΛԼ͛ΔΦϓγϣϯɻ


    • InterruptSpokenAudioAndMixWithOthers


    • ΦʔσΟΦΛ࠶ੜ࣌ʹɺόοΫάϥ΢ϯυΦʔσΟΦΛҰ࣌ఀࢭ͢Δ͔Ͳ͏͔Λܾఆ͢ΔΦ
    ϓγϣϯɻ


    • allowBluetooth


    • BluetoothϋϯζϑϦʔσόΠεΛ࢖༻Մೳͳೖྗϧʔτͱͯ͠දࣔ͢Δ͔Ͳ͏͔Λܾఆ͢Δ
    Φϓγϣϯɻ
    https://developer.apple.com/documentation/avfaudio/avaudiosession/
    categoryoptions

    View Slide

  15. AVAudioSession - Option
    • spokenAudio


    • Ի੠ͷׂΓࠐΈ͕͋ΔΞϓϦͰͷར༻Λࣔ͢ϞʔυͰ͢ɻΧʔφϏԻ੠Ҋ಺ͳͲɺ୹ׂ͍ΓࠐΈͷࡍʹ࠶ੜதͷ΋
    ͷΛҰ࣌ఀࢭ͠ɺऴΘΓ࣍ୈ࠶։͠·͢ɻ


    • videoChat


    • ΦϯϥΠϯϏσΦձٞͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ


    • videoRecording


    • ಈը࿥ըͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ


    • voiceChat


    • VoIPͳͲͷ૒ํ޲ͷԻ੠௨৴Ͱͷར༻Λࣔ͢ϞʔυͰ͢ɻ


    • voicePrompt


    • ςΩετಡΈ্͛Λ࢖༻ͨ͠Ի੠࠶ੜͰͷར༻Λࣔ͢ϞʔυͰ͢ɻ
    https://developer.apple.com/documentation/avfaudio/avaudiosession/categoryoptions

    View Slide

  16. AVAudioSessionͬͯͨ͘͞Μ
    ઃఆ͕͋ΔΜͩͶʂ

    View Slide

  17. Ͱ݁ہͲ͏͢Μͷʁ

    View Slide

  18. 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)


    ※ίʔυ͸ΠϝʔδͰ͢

    View Slide

  19. 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)


    }


    ɹɹɹ


    ※ίʔυ͸ΠϝʔδͰ͢

    View Slide

  20. 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Λ
    ࢖͏

    View Slide

  21. BackGround࠶ੜͰؾΛ͚ͭΔ
    ͜ͱ
    • AVAudioSessionͷछྨ͸ͨ͘͞Μ͋ΔͷͰࣗ෼ͷΞϓϦͰͲͷ
    ઃఆ͕߹͍ͬͯΔͷ͔͸֬ೝ͢Δඞཁ͕͋Δ


    • Ի੠͕όοΫάϥ΢ϯυʹདྷ͍ͯΔ͔Ͳ͏͔͸
    secondaryAudioShouldBeSilencedHintΛ࢖͏


    • isOtherAudioPlayingࣄྫ͋ͬͨΒڭ͍͑ͯͩ͘͠

    View Slide

  22. ࠷ޙʹ

    View Slide

  23. https://voicy.jp/search/entaku
    ฉ͍ͯ͘ΕΑͳ😊

    View Slide

  24. https://www.wantedly.com/projects/619248
    Ԡԉ͚ͩͯ͘͠ΕΑͳ😊

    View Slide