Slide 1

Slide 1 text

All the Audio You Need Beginners Guide to AV Foundation Audio

Slide 2

Slide 2 text

Remedial Core Audio 101

Slide 3

Slide 3 text

Foundations of Core Audio Audio Queue Audio Session Audio Unit

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

– 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.”

Slide 7

Slide 7 text

– 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.”

Slide 8

Slide 8 text

So what do we do??

Slide 9

Slide 9 text

System Sound Services and AVFoundation!!

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Demo

Slide 12

Slide 12 text

System Sound Services

Slide 13

Slide 13 text

System Sound Services

Slide 14

Slide 14 text

System Sound Services

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

AV Foundation

Slide 18

Slide 18 text

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.

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

AV Foundation Audio Classes AVAudioSession AVAudioPlayer AVAudioRecorder

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Audio Session Categories AVAudioSessionCategorySoloAmbient (default) AVAudioSessionCategoryAmbient AVAudioSessionCategoryPlayback AVAudioSessionCategoryRecord

Slide 26

Slide 26 text

Audio Session Categories AVAudioSessionCategoryPlayAnd Record AVAudioSessionCategoryAudio Processing AVAudioSessionCategoryMultiRoute

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Demo

Slide 32

Slide 32 text

Ambient vs Solo Ambient

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Demo

Slide 36

Slide 36 text

Ready Player One Demo

Slide 37

Slide 37 text

Ready Player One Demo

Slide 38

Slide 38 text

Ready Player One Demo

Slide 39

Slide 39 text

Ready Player One Demo

Slide 40

Slide 40 text

Ready Player One Demo

Slide 41

Slide 41 text

Ready Player One Demo

Slide 42

Slide 42 text

Ready Player One Demo

Slide 43

Slide 43 text

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.

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Demo

Slide 46

Slide 46 text

Recorder Demo

Slide 47

Slide 47 text

Recorder Demo

Slide 48

Slide 48 text

Recorder Demo

Slide 49

Slide 49 text

Recorder Demo

Slide 50

Slide 50 text

I [insert name here]…

Slide 51

Slide 51 text

Do solemnly swear…

Slide 52

Slide 52 text

Never to use my laptop microphone…

Slide 53

Slide 53 text

For anything I intend for human consumption…

Slide 54

Slide 54 text

So help me Jobs.

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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?

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Quiz Time!! Answer: B AVAudioSessionCategoryPlayback

Slide 59

Slide 59 text

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.

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Quiz Time! Answer: C 
 System Sound Services with AVAudioSessionCategorySoloAmbient

Slide 62

Slide 62 text

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?

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

Quiz Time! Answer: A AVAudioSessionCategoryAmbient

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

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?

Slide 67

Slide 67 text

For the More Curious:

Slide 68

Slide 68 text

In Conclusion

Slide 69

Slide 69 text

So Now You Know… @RedQueenCoder