Building a Playlist with AV Foundation

Building a Playlist with AV Foundation

Slides from my talk at iOSDevCampDC 2013 about building playlists with AV Foundation.

Josh Johnson

August 24, 2013

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

36 slides
  1. or: A crash course to AV Foundation  with music.

    Josh Johnson @jnjosh | αjnjosh | jnjosh.com
  2. MPMusicPlayerController *player =  [MPMusicPlayerController applicationMusicPlayer]; ! // Access the

    music through the iPod Library Access API MPMediaQuery *query = [MPMediaQuery artistsQuery]; ! [query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"David Hasselhoff" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo]]; [query setGroupingType:MPMediaGroupingArtist]; ! // Let the player have the query [player setQueueWithQuery:query]; ! // Listen to the musical stylings of David Hasselhoff [player play];
  3. AVURLAsset *asset =  [AVURLAsset URLAssetWithURL:<# hasselhoff.mp3 #> options:nil]; !

    ! AVPlayerItem *playerItem =  [[AVPlayerItem alloc] initWithAsset:asset]; ! ! AVPlayer *audioPlayer =  [[AVPlayer alloc] initWithPlayerItem:playerItem]; ! ! [audioPlayer play];
  4. AVURLAsset *asset =  [AVURLAsset URLAssetWithURL:<# hasselhoff.mp3 #> options:nil]; !

    AVPlayerItem *item1 =  [[AVPlayerItem alloc] initWithAsset:asset]; ! // setup asset2 and item2 ! AVQueuePlayer *audioPlayer =  [AVQueuePlayer queuePlayerWithItems:@[ item1, item2 ]]; ! ! [audioPlayer play];
  5. AVURLAsset *asset =  [AVURLAsset URLAssetWithURL:song.url options:nil]; ! ! [asset

    loadValuesAsynchronouslyForKeys:@[ @"tracks" ] completionHandler:^{  ! NSArray *assetTracks = [asset tracksWithMediaType:AVMediaTypeAudio]; // ... ! }];
  6. CMTime fiveSeconds = CMTimeMake(5, 1); CMTime halfSecond = CMTimeMake(1, 2);

    CMTime thirtyFPS = CMTimeMake(1, 30); CMTimeAdd(<#CMTime addend1#>, <#CMTime addend2#>) ! CMTimeSubtract(<#CMTime minuend#>, <#CMTime subtrahend#>) ! CMTimeShow(<#CMTime time#>)
  7. CMTimeRange range = CMTimeRangeMake(CMTimeMake(5, 1), CMTimeMake(5, 1)); ! CMTimeRangeMake(<#CMTime start#>,

    <#CMTime duration#>) ! CMTimeRangeContainsTime(<#CMTimeRange range#>, <#CMTime time#>) ! CMTimeRangeShow(<#CMTimeRange range#>)
  8. AVURLAsset *asset =  [AVURLAsset URLAssetWithURL:song.url options:nil]; ! ! [asset

    loadValuesAsynchronouslyForKeys:@[ @"tracks" ] completionHandler:^{  ! NSArray *assetTracks = [asset tracksWithMediaType:AVMediaTypeAudio]; // ... ! }];
  9. AVMutableComposition *composition = [AVMutableComposition composition]; AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio

    preferredTrackID:kCMPersistentTrackID_Invalid]; AVAssetTrack *audioTrack = [assetTracks lastObject]; [track insertTimeRange:audioTrack.timeRange ofTrack:audioTrack atTime:<# CMTime #> error:&error];
  10. AVComposition *compositionAsset = [composition copy]; ! ! AVPlayerItem *playerItem =

    [[AVPlayerItem alloc] initWithAsset:compositionAsset]; ! ! AVPlayer *audioPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem]; ! ! // Observe `status` for AVPlayerStatusReadyToPlay ! ! [audioPlayer play];
  11. AVMutableAudioMixInputParameters *params =  [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track]; ! CMTime crossfadeDuration =

    ...; CMTimeRange crossfadeEndRange = CMTimeRangeMake(CMTimeSubtract(CMTimeRangeGetEnd(songRange), crossfadeDuration), crossfadeDuration); ! // set the ramp [params setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:crossfadeEndRange]; [audioMixParameters addObject:params]; ! AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; [audioMix setInputParameters:audioMixParameters]; [playerItem setAudioMix:fadeMix];

More Decks by Josh Johnson

Other Decks in Programming