2. About...
Bob McCune
‣ MN Developer and Instructor
‣ Owner of TapHarmonic, LLC.
‣ Founded Minnesota CocoaHeads in 2008
3. Agenda
What will I learn?
‣ AV Foundation Overview
‣ Media Playback
‣ Media Editing
‣ Composing Media
‣ Mixing Audio
‣ Building Video Transitions
‣ Layering Visual Content
4. AV Foundation
Overview
‣ Apple’s advanced Objective-C framework for
working with timed-media
‣ High performance, asynchronous processing
‣ Hardware accelerated handling of AV media
‣ Available in its current form since iOS 4
‣ Significant additions and enhancements iOS 5 and 6
‣ Default media framework on Mac OS X since 10.7 Lion
‣ Apple’s focus for media apps on both iOS and Mac
5. Where does it fit?
iOS Media Options
Simple Awesome Complex
MediaPlayer CoreAudio
UIKit CoreMedia
AVFoundation
CoreVideo
CoreAnimation
7. Media Assets
Understanding Assets
‣ AVAsset is an abstract representation of media resource
modeling the static aspects of the media.
‣ Abstracts away the type and location
‣ AVAssetTrack models the static aspects of the
individual media streams within an asset
‣ Tracks are of a uniform type (video, audio, etc.)
AVAssetTrack (Video)
AVAssetTrack (Audio)
8. Using AVAssets
What can I do with an asset?
‣ Inspect
‣ Generate Images
‣ Transcode and Export
‣ Playback
10. AVPlayer
Playback Controller
‣ AVPlayer is a controller for managing playback
‣ play
‣ pause
‣ rate
‣ seekToTime:
‣ Use KVO to observe playback readiness and state
‣ status
‣ Timed Observations
‣ addPeriodicTimeObserverForInterval:queue:usingBlock
‣ addBoundaryTimeObserverForInterval:queue:usingBlock
11. Playing Media
Static vs Dynamic Models
‣ AV Foundation distinguishes between static
and dynamic aspects of media
AVAsset
AVAsset AVAsset
AVAssetTrack
St ati c
AVPlayerItemTrack
AVPlayerItem AVPlayerItemTrack
AVPlayerItemTrack
D y n amic
12. Understanding Time
Core Media Essentials
CMTime
‣ Rational number representing time
‣ 64-bit integer time value (numerator)
‣ 32-bit integer time scale (denominator)
CMTime fiveSeconds = CMTimeMake(5, 1);
CMTime oneSample = CMTimeMake(1, 44100);
CMTime zeroTime = kCMTimeZero;
‣ Large number of utility functions in Core Media:
‣ CMTimeAdd,CMTimeSubtract,CMTimeCompare, etc.
13. Understanding Time
Core Media Essentials
CMTimeRange
‣ Core Media struct containing start time and duration
CMTimeRange assetRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
CMTimeRange zeroRange = kCMTimeRangeZero;
‣ Like CMTime, there are many Core Media functions:
‣ CMTimeRangeEqual, CMTimeRangeContainsTime,
CMTimeRangeGetEnd, CMTIMERANGE_ISVALID, etc.
24. Audio Mixing
AVAudioMix
‣ Composition tracks play at their natural volume
‣ AVAudioMix applies track-level volume adjustments
‣ Composed of AVAudioMixInputParameters
‣ Parameters control individual track volume over time
CMTime CMTimeRange
28. Video Transitions
AVVideoComposition
AVVideoComposition
Defines how two or more video
tracks are composited together
AVVideoCompositionInstruction
Configured through collection of
AVVideoCompositionInstruction
AVVideoCompositionInstruction composition instructions describing
compositing behavior
29. Video Transitions
AVVideoCompositionInstruction
AVVideoComposition
AVVideoCompositionInstruction
AVVideoCompositionInstruction
AVVideoCompositionInstruction
Defines the time range of
compositing behavior
Composed of layer instructions
AVAsset
AVAsset
AVVideoCompositionLayerInstruction describing compositing behavior
30. Video Transitions
AVVideoCompositionLayerInstruction
AVVideoComposition
AVVideoCompositionInstruction
AVVideoCompositionInstruction
AVVideoCompositionInstruction
AVAsset
AVAsset Defines the transform and
AVVideoCompositionLayerInstruction
opacity ramps of input layers
Transform and opacity changes
modified over given time range
34. Building Transitions 3
Define Time Ranges
A
Passthrough Passthrough Passthrough
B
Transition Transition
* Time ranges must not have gaps or overlap
* Total duration must not be shorter than composition
36. Building Transitions 5
Set sizes and apply
#define FRAME_RATE CMTimeMake(1, 30)
#define RENDER_SIZE CGSizeMake(1280, 720)
AVMutableVideoComposition *videoComposition =
[AVMutableVideoComposition videoComposition];
// Set instructions on AVVideoComposition instance
videoComposition.instructions = instructions;
videoComposition.frameDuration = FRAME_RATE;
videoComposition.renderSize = RENDER_SIZE;
AVPlayerItem *playerItem =
[AVPlayerItem playerItemWithAsset:[composition copy]];
playerItem.videoComposition = videoComposition;
37. New in iOS 6
AVVideoComposition
videoCompositionWithPropertiesOfAsset:
Automagic Setup:
• Calculates all required passthrough and transition time ranges
• Builds appropriate composition and layer instructions for time ranges
• Sets the appropriate render size
• Sets the appropriate frame rate
40. Layering Content
Core Animation
Core Animation a natural choice
‣ High performance, inherently time-based
‣ CALayer subclasses used for all video rendering
CALayer: used to layer images and text
CAAnimation: used to animate layered content
CABasicAnimation
CAKeyframeAnimation
41. Animation Timing
AVSynchronizedLayer
‣ Core Animation operates on host time
‣ Starts at boot, marches towards infinity
‣ Timeline animations need to use movie time
‣ Starts at kCMTimeZero and runs to duration
‣ Can be started, stopped, rewound, etc.
‣ Use AVSynchronizedLayer to use movie time
‣ Confers player item timing on to its sublayer tree
AVPlayerItem AVSynchronizedLayer
CATextLayer CABasicAnimation
42. Core Animation
Timeline vs Realtime Animations
‣ Exactly the same, but different:
‣ Animations with zero beginTime won’t be seen
‣ Set beginTime = AVCoreAnimationBeginTimeZero
‣ Animations removed by default
‣ Set removedOnCompletion = NO
‣ Unable to use CAAnimationGroup?
43. Core Animation
Natural Choice, Awkward Implementation
‣ Different conceptual models for timeline editing
‣ CMTime and CMTimeRange for asset items
‣ Seconds and milliseconds for layers
‣ Build abstraction to help bridge the gap
‣ Usage differs in playback and export scenarios
‣ AVSynchronizedLayer for playback
‣ Attach to player’s view hierarchy/layer tree
‣ AVVideoCompositionCoreAnimationTool for export