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

AV Foundation Cocoa Conf Chicago 2014

AV Foundation Cocoa Conf Chicago 2014

These are the slides from my talk about AV Foundation from Cocoa Conf Chicago 2014

Janie Clayton-Hasz

March 07, 2014
Tweet

More Decks by Janie Clayton-Hasz

Other Decks in Programming

Transcript

  1. All the Audio You
    Need
    Beginners Guide to AV Foundation
    Audio

    View Slide

  2. Remedial Core
    Audio 101

    View Slide

  3. Foundations of
    Core Audio
    Audio Queue
    Audio Session
    Audio Unit

    View Slide

  4. View Slide

  5. View Slide

  6. – Jens Alfke, quoted from the Core Audio Mailing List
    “"Easy" and "CoreAudio" can't be
    used in the same sentence. :P
    CoreAudio is very powerful, very
    complex, and under-documented. Be
    prepared for a steep learning curve,
    APIs with millions of tiny little
    pieces, and puzzling things out from
    sample code rather than reading
    high-level documentation.”

    View Slide

  7. – Q
    “If you can't take a little bloody
    nose, maybe you ought to go back
    home and crawl under your bed. It's
    not safe out here. It's wondrous,
    with treasures to satiate desires both
    subtle and gross. But it's not for the
    timid.”

    View Slide

  8. So what do we do??

    View Slide

  9. System Sound Services
    and AVFoundation!!

    View Slide

  10. Here is how to add
    Sound to your app in
    less than two minutes
    (which I found out
    by accident at my
    last talk…)

    View Slide

  11. Demo

    View Slide

  12. System Sound
    Services

    View Slide

  13. System Sound
    Services

    View Slide

  14. System Sound
    Services

    View Slide

  15. Wait a second, this is
    supposed to just be
    about AVFoundation!!
    What gives??

    View Slide

  16. So, that’s cool, but it’s
    kind of limited, isn’t
    it???
    !
    Also, didn’t you promise
    me there would be no
    C??

    View Slide

  17. AV
    Foundation

    View Slide

  18. What is AV
    Foundation?
    AV Foundation is one of several
    frameworks that you can use to play
    and create time-based audiovisual
    media. It provides an Objective-C
    interface you use to work on a
    detailed level with time-based
    audiovisual data.

    View Slide

  19. Advantages of
    AV Foundation
    Built-in interruption handling
    Written in Objective-C
    Optimized for the iOS ecosystem
    Now powerful enough to do much of
    what Core Audio does

    View Slide

  20. AV Foundation
    Audio Classes
    AVAudioSession
    AVAudioPlayer
    AVAudioRecorder

    View Slide

  21. AVAudioSession
    AVAudioSession is a singleton object
    that comes included into your projects
    regardless of whether you create one
    or not.
    AVAudioSession manages all of the
    other AV Audio objects.

    View Slide

  22. AVAudioSession
    Activate/Deactivate the audio session
    Set the audio session category
    Configure audio settings
    Handle audio route changes and
    react to audio events

    View Slide

  23. Audio Session
    Default Behavior
    Playback Enabled, Recording Disabled
    “Silent” switch mutes your sound
    When the phone sleeps your sound
    stops
    When your audio starts other audio,
    like iTunes, stops playing.

    View Slide

  24. When Can I Ignore
    the Audio Session?
    If you are using System Sound
    Services or UIKit playInputClick
    If you do not use sound at all

    View Slide

  25. Audio Session
    Categories
    AVAudioSessionCategorySoloAmbient
    (default)
    AVAudioSessionCategoryAmbient
    AVAudioSessionCategoryPlayback
    AVAudioSessionCategoryRecord

    View Slide

  26. Audio Session
    Categories
    AVAudioSessionCategoryPlayAnd
    Record
    AVAudioSessionCategoryAudio
    Processing
    AVAudioSessionCategoryMultiRoute

    View Slide

  27. AV Audio Session
    Category Audio Processing
    No input or output
    Processes audio offline
    Only really going to be used in a
    specialized audio app

    View Slide

  28. AV Audio Session
    Category MultiRoute
    Used to send sound to more than
    one output (disk, headphones,
    speaker, etc…)
    FYI: The default audio route is to
    the phone receiver, you can override
    this to go to the speaker on the
    bottom of the phone

    View Slide

  29. AV Audio Session
    Category Ambient
    Silenced by the mute switch
    Allows audio mixing from other
    apps, like iTunes
    Output only

    View Slide

  30. AV Audio Session
    Category Solo Ambient
    Silenced by the mute switch
    Does NOT allow audio mixing from
    other apps
    Output Only

    View Slide

  31. Demo

    View Slide

  32. Ambient vs
    Solo Ambient

    View Slide

  33. AV Audio Session
    Category Playback
    Is NOT silenced by the mute switch
    Does not allow other audio by
    default, but you can override this
    behavior
    Output only

    View Slide

  34. AVAudioPlayer
    Delegate
    Has built-in methods to handle
    interruptions from phone calls and
    alerts.

    View Slide

  35. Demo

    View Slide

  36. Ready Player One
    Demo

    View Slide

  37. Ready Player One
    Demo

    View Slide

  38. Ready Player One
    Demo

    View Slide

  39. Ready Player One
    Demo

    View Slide

  40. Ready Player One
    Demo

    View Slide

  41. Ready Player One
    Demo

    View Slide

  42. Ready Player One
    Demo

    View Slide

  43. AV Audio Session
    Category Record
    Input only, so it is not affected by
    the mute switch and it does not
    allow audio from other apps
    I find this to be limiting and would
    recommend the next category
    instead.

    View Slide

  44. AV Audio Session
    Category Play and Record
    Is NOT silenced by the mute switch
    Does not allow other audio by
    default, but you can override this
    behavior
    Both Input and Output

    View Slide

  45. Demo

    View Slide

  46. Recorder Demo

    View Slide

  47. Recorder Demo

    View Slide

  48. Recorder Demo

    View Slide

  49. Recorder Demo

    View Slide

  50. I [insert name
    here]…

    View Slide

  51. Do solemnly
    swear…

    View Slide

  52. Never to use my
    laptop microphone…

    View Slide

  53. For anything I
    intend for human
    consumption…

    View Slide

  54. So help me Jobs.

    View Slide

  55. AVAudioRecorder
    Delegate
    Does NOT allow phone calls to go
    through, so there is no error
    handling for phone calls.

    View Slide

  56. Quiz Time!!
    Scenario 1: You are writing an audio
    book app. You need to make sure
    the audio keeps playing when the
    device falls asleep. What category
    should you use?

    View Slide

  57. Choices:
    A: AVAudioSessionCategoryAmbient
    B: AVAudioSessionCategoryPlayback
    C: System Sound Services with
    AVAudioSessionCategorySoloAmbient

    View Slide

  58. Quiz Time!!
    Answer: B
    AVAudioSessionCategoryPlayback

    View Slide

  59. Quiz Time!
    Scenario 2: You are building an
    app.Net client and you want to add
    a sound effect when the user
    publishes a post.

    View Slide

  60. Choices:
    A: AVAudioSessionCategoryAmbient
    B: AVAudioSessionCategoryPlayback
    C: System Sound Services with
    AVAudioSessionCategorySoloAmbient

    View Slide

  61. Quiz Time!
    Answer: C 

    System Sound Services with
    AVAudioSessionCategorySoloAmbient

    View Slide

  62. Quiz Time!
    Scenario 3: You write a first-person
    shooter game that uses OpenAL-
    based sound effects. You also
    provide a background soundtrack
    but include an option for the user
    to turn it off. What session type do
    you use?

    View Slide

  63. Choices:
    A: AVAudioSessionCategoryAmbient
    B: AVAudioSessionCategoryPlayback
    C: System Sound Services with
    AVAudioSessionCategorySoloAmbient

    View Slide

  64. Quiz Time!
    Answer: A
    AVAudioSessionCategoryAmbient

    View Slide

  65. View Slide

  66. How to Choose Your
    Audio Category
    Think about the primary purpose for
    audio in your app
    Is your audio essential or peripheral?
    How likely is it that other audio will
    be playing when your app is
    launched?

    View Slide

  67. For the More
    Curious:

    View Slide

  68. In Conclusion

    View Slide

  69. So Now You Know…
    @RedQueenCoder

    View Slide