Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Ā
iOS Media APIs (MobiDevDay Detroit, May 2013)
1. iOS Media APIs
Chris Adamson ā¢ @invalidname
MobiDevDay ā¢ Detroit, MI ā¢ May 4, 2013
Slides will be posted to slideshare.net/invalidname
Thursday, May 2, 13
3. iOS Media APIs
ā¢ AV Foundation
ā¢ Core Media
ā¢ Core Animation
ā¢ Media Player
ā¢ Core Audio
ā¢ Audio Toolbox
ā¢ Audio Units
ā¢ Core MIDI
ā¢ Open AL
Thursday, May 2, 13
5. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
6. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
8. AV Foundation
ā¢ High-level Obj-C audio/video framework
ā¢ Debuted in iPhone OS 2.3, not fully baked
until iOS 4
ā¢ Consists of three distinct areas of
functionality with little overlap
Thursday, May 2, 13
10. AVF audio classes
ā¢ AVAudioPlayer ā plays ļ¬at ļ¬les or audio
from an NSData
ā¢ AVAudioRecorder ā records from most-
recently connected input device (built-in
mic, headset, etc.)
ā¢ AVAudioSession ā negotiates with system
for access to audio hardware
Thursday, May 2, 13
12. Playing a song
NSURL *songURL = [[NSBundle mainBundle]
URLForResource:@"Bossa Lounger Long"
! ! ! ! ! ! ! withExtension:@"caf"];
// set up av audio player
NSError *err = nil;
self.player = [[AVAudioPlayer alloc]
initWithContentsOfURL:songURL
error:&err];
if (err) {
! NSLog (@"Error creating player: %@", err);
} else {
! [self.player play];
}
Thursday, May 2, 13
13. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
14. Video Playback
ā¢ Two options:AVPlayer and
MPMoviePlayerController
ā¢ Since weāre already talking about AV
Foundation, letās do AVPlayer and friends
for now
Thursday, May 2, 13
15. AVF essentials
ā¢ AVAsset ā A time-based, playable item
(local or remote)
ā¢ AVPlayer ā Handles playback of one or
more AVPlayerItems
ā¢ Each AVPlayerItem is associated with an
AVAsset
ā¢ Player does play/pause, seekToTime:, etc.
Thursday, May 2, 13
16. AVPlayer video
ā¢ AVPlayerLayer ā A CALayer to present
video from an AVLayer
ā¢ CALayer is not a UIResponder, doesnāt
handle touches.You need to provide your
own playback UI.
ā¢ gravity property determines āstretchingā
of video to ļ¬t layerās bounds
Thursday, May 2, 13
18. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
19. AVF Capture
ā¢ AVCaptureSession ā Coordinates capture
activities
ā¢ Discover AVCaptureDevices (mics,
camera), create AVCaptureInputs from
them, connect to session
ā¢ Create AVCaptureOutputs (ļ¬le, data
callbacks), connect to session
Thursday, May 2, 13
20. AVF Capture// create capture session, attach default video input
self.captureSession = [[AVCaptureSession alloc] init];
NSError *setUpError = nil;
AVCaptureDevice *videoDevice = [AVCaptureDevice
defaultDeviceWithMediaType: AVMediaTypeVideo];
if (videoDevice) {
! AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput
deviceInputWithDevice:videoDevice
! error:&setUpError];
! if (videoInput) {
! ! [captureSession addInput: videoInput];
! }
}
// create a preview layer from the session and add it to UI
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer
layerWithSession:captureSession];
previewLayer.frame = captureView.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[captureView.layer addSublayer:previewLayer];
// start capture session and write to file
captureMovieOutput = [[AVCaptureMovieFileOutput alloc] init];
[captureSession addOutput:captureMovieOutput];
[captureSession startRunning];
[captureMovieOutput startRecordingToOutputFileURL:captureMovieURL
! ! ! ! ! ! recordingDelegate:self];
Thursday, May 2, 13
21. AVF Editing
ā¢ AVMutableComposition āĀ An AVAsset for a
multi-track movie you build from
references to other AVAssets
ā¢ AVMutableCompositionTrack ā Built up
with
insertTimeRange:ofTrack:atTime:error:
Thursday, May 2, 13
22. AVFVideo Effects
ā¢ Create AVMutableMutableVideoInstruction
made up of
AVMutableVideoCompositionLayerInstructi
ons
ā¢ Each instruction works with a video track
ā¢ Instruction ramps opacity or afļ¬ne
transform over a time range
Thursday, May 2, 13
23. AVF Text/Image effects
ā¢ Create an AVSynchronizedLayer to show
the composition
ā¢ Create CALayers, CATextLayers and set
CAAnimations on them, then add as sub-
layers to the AVSynchronizedLayer
Thursday, May 2, 13
24. AVF Export
ā¢ Create AVAssetExportSession with one of the
canned presets (audio only, or QuickTime .mov
at preset size or quality)
ā¢ If you used layer-based animations, add an
AVVideoComposition, with an
AVVideoCompositionCoreAnimationTool
(yes, itās as hard as it sounds)
ā¢ Call
exportAsynchronouslyWithCompletionHandler:
Thursday, May 2, 13
26. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
28. HTTP Live Streaming
ā¢ Apple-led semi-standard streaming format
ā¢ Only stream format allowed on App Store
ā¢ Streams video as series of small (10 sec.)
ļ¬les, via ordinary web server on port 80
ā¢ Create streams with command-line tools,
Pro apps (Compressor, Final Cut), server-
side transcoders (Wowza)
Thursday, May 2, 13
31. HLS Advantages
ā¢ Mobile-friendly: works over spotty cellular
connections, stream can provide multiple
bitrates (client switches on the ļ¬y)
ā¢ No special server software required: can
stream from Dropbox
ā¢ Wide adoption: Roku, Xbox,Android,
GoogleTV, etc.
Thursday, May 2, 13
32. Client-side HLS
ā¢ Create an AVPlayer or
MPMoviePlayerController with the
streamās .m3u8 URL just like any other URL
ā¢ There is no step 2
Thursday, May 2, 13
33. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
35. Media Player
ā¢ Allows access to the deviceās iTunes library
ā¢ Audio-only: songs, podcasts, audiobooks
ā¢ Discover contents with MPMediaQuery, or
show a MPMediaPickerController
ā¢ MPMediaItems have metadata (title, artist,
album, cover art, etc.)
ā¢ Play with MPMusicPlayerController
Thursday, May 2, 13
37. MPMoviePlayerController
ā¢ Simple video player, alternative to AVPlayer
ā¢ Provides its own view and controls
ā¢ Lighter-weight than AVPlayer, which helps
on really old devices (iPhone 3GS)
Thursday, May 2, 13
38. MP novelties
ā¢ MPNowPlayingInfoCenter ā Access to
metadata shown on lock screen and
external media displays (e.g., in-car
entertainment systems)
ā¢ MPVolumeView ā System-wide volume
slider, with AirPlay button if available
Thursday, May 2, 13
39. Remote Controls
ā¢ Receive play/pause, forward/back from
headset, dock keyboard, in-car systems,
other external devices
ā¢ -[UIApplication
beginReceivingRemoteControlEvents]
ā¢ Must be able to become ļ¬rst responder
Thursday, May 2, 13
40. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
42. AirPlay
ā¢ Wireless audio/video streaming from iOS
device to Apple TV,AirPort Express, some
speakers
ā¢ Unofļ¬cial third-party clients for Mac
(Reļ¬ector,Air Sharing), PC,Android, etc.
Thursday, May 2, 13
43. AirPlay API
ā¢ There basically isnāt one
ā¢ User either mirrors device, or uses
AirPlay menu on MPVolumeView or
MPMoviePlayerController
ā¢ You can deny (but please donāt) with
allowsAirPlay (MPMovieController),
mediaPlaybackAllowsAirPlay (UIWebView),
allowsExternalPlayback (AVPlayer)
Thursday, May 2, 13
44. Second screens
ā¢ If user connects toVGA/DVI via Dock/
Lightning adapter, or connects to AirPlay
without mirroring, your app will see a
second screen
ā¢ Discover with -[UIScreen screens], create
new UIWindow for it
ā¢ Chat on the device, video on the second
screen?
Thursday, May 2, 13
45. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
47. Core Audio
ā¢ C frameworks for real-time audio
processing
ā¢ Legendary performance, legendary
difļ¬culty
ā¢ Basis of OS X pro audio apps like Logic
and GarageBand
Thursday, May 2, 13
48. Core Audio
ā¢ Audio Queue
ā¢ Audio Units
ā¢ Open AL
ā¢ Audio File Services
ā¢ Audio Converter Svcs.
ā¢ Extended Audio File
Svcs.
ā¢ Audio File Stream Svcs.
ā¢ Audio Session Svcs.
Thursday, May 2, 13
49. Core Audio
ā¢ Small number of structures and functions
ā¢ Most behavior is speciļ¬ed by getting and
setting properties
ā¢ All functions return OSStatus, must check
for noErr before continuing
ā¢ āCreateā-style functions take pointer as a
parameter and populate it
Thursday, May 2, 13
50. Audio Queue
ā¢ Convenience API for play-out or capture
ā¢ Wrapped by AV Foundationās
AVAudioRecorder,AVAudioPlayer for ļ¬le-
only scenarios
ā¢ For play-out, app provides buffers to play
ā¢ For captuer, queue gives app with buffers of
capture data
Thursday, May 2, 13
56. Parsing Web Radio
NSData NSData
Packets Packets Packets Packets Packets
NSURLConnection delivers
NSData buffers, containing audio
and framing info.We pass it to
Audio File Services.
Thursday, May 2, 13
57. Parsing Web Radio
NSData NSData
Packets Packets Packets Packets Packets
Packets Packets
Packets Packets Packets
NSURLConnection delivers
NSData buffers, containing audio
and framing info.We pass it to
Audio File Services.
Audio File Services calls us back
with parsed packets of audio data.
Thursday, May 2, 13
58. Parsing Web Radio
NSData NSData
Packets Packets Packets Packets Packets
Packets Packets
Packets Packets Packets
012
Packets
Packets
Packets
Packets
Packets
Packets
NSURLConnection delivers
NSData buffers, containing audio
and framing info.We pass it to
Audio File Services.
Audio File Services calls us back
with parsed packets of audio data.
We create an AudioQueueBuffer
with those packets and enqueue it
for play-out.
Thursday, May 2, 13
59. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
75. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
77. Core MIDI
ā¢ Handles MIDI events from external devices
ā¢ Connect via Dock/Lightning connectors
or Camera Connection Kit (USB)
ā¢ Only provides callbacks on events (key up,
key down, pitch bend, etc.) ā up to you do
something with it
Thursday, May 2, 13
78. MIDI Packets
ā¢ High nybble of status is command, low is
channel number
ā¢ Data 1 & 2 depend on command
STATUS DATA 1 DATA 2
Thursday, May 2, 13
80. How Do I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
82. OpenAL
ā¢ Cross-platform C API to create positional
sound
ā¢ Designed to resemble OpenGL
ā¢ Low latency, ideal for games
ā¢ Calls set an error ļ¬ag, must check with
alGetError() after every AL call
Thursday, May 2, 13
83. AL example
// set up OpenAL source
alGenSources(1, player.sources);
alSourcei(player.sources[0], AL_LOOPING, AL_TRUE);
alSourcef(player.sources[0], AL_GAIN, AL_MAX_GAIN);
updateSourceLocation(player);
// connect buffer to source
alSourcei(player.sources[0], AL_BUFFER, buffers[0]);
// set up listener
alListener3f (AL_POSITION, 0.0, 0.0, 0.0);
// start playing
alSourcePlay(player.sources[0]);
Note: error checking removed for clarity
Thursday, May 2, 13
84. OpenAL Concepts
ā¢ ALListener ā The listener, who has an x,y,z
position, orientation, etc.
ā¢ ALSource ā A sound producing object in
the space. Has position, orientation,
motion, sound cone, much more
ā¢ ALBuffer ā Buffers of sound attached to a
source. Source can loop one buffer or
receive a stream of buffers
Thursday, May 2, 13
85. How Did I?
ā¢ Play music on my title
screen?
ā¢ Play a video in my app?
ā¢ Capture, edit, and
export video?
ā¢ Play streaming video
from my web site?
ā¢ Play userās iTunes music?
ā¢ Play a video on an
AppleTV?
ā¢ Play web radio?
ā¢ Mix and perform effects
on audio?
ā¢ Use MIDI devices?
ā¢ Create in-game sounds
for a 3D game?
Thursday, May 2, 13
86. iOS Media APIs
ā¢ There are a LOT of them!
ā¢ Each ļ¬lls a speciļ¬c role, not a lot of overlap
ā¢ Simple stuff is easy, complex stuff is possible
Thursday, May 2, 13
87. Questions?
ā¢ āAudio andVideo Starting Pointā in Apple
developer documentation
ā¢ http://devforums.apple.com
ā¢ coreaudio-api@lists.apple.com
Thursday, May 2, 13
88. iOS Media APIs
Chris Adamson ā¢ @invalidname
MobiDevDay ā¢ Detroit, MI ā¢ May 4, 2013
Slides will be posted to slideshare.net/invalidname
Thursday, May 2, 13