Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch among alternate audio streams using AVPlayer

I have a test application that uses AVPlayer to play video specified by an m3u8 HLS playlist. The playlist specifies several alternate audio streams, similar to the "Listing 10" sample playlist provided by Apple found here: http://developer.apple.com/library/ios/#technotes/tn2288/_index.html#//apple_ref/doc/uid/DTS40012238-CH1-ALTERNATE_MEDIA The app needs to be able to switch among the alternate audio streams while the video is playing. For example, the app should be able to switch among the English, French, and Spanish audio streams by the user tapping buttons in the app while the video is playing.

Which AVFoundation classes and methods would be used by the AVPlayer and its related objects to switch among the audio streams that are specified in the m3u8 playlist? I have looked at the AVFoundation class documentation but do not see how to do this.

A link to some sample code that shows how to do this would be great. I have been searching the web for this information without success. Thanks for any help with this.

like image 765
Marc Zehngut Avatar asked Jul 20 '12 20:07

Marc Zehngut


1 Answers

For m3u8 playback with AVPlayer, it looks like you cannot use an AVAsset to construct an AVPlayerItem. You need to construct an AVPlayerItem from the URI directly. Upon instantiating an AVPlayer with this AVPlayerItem, and then KVO listening on the property @"status", you will have an asset within the [[avPlayerInstance currentItem] asset] if the status is AVPlayerStatusReadyToPlay. This is described on page 20 of the AV Foundation Programming Guide.

To change audio to various alternates use:

AVMediaSelectionGroup *audioSelectionGroup = [[[avPlayerInstance currentItem] asset] mediaSelectionGroupForMediaCharacteristic: AVMediaCharacteristicAudible];

NSLog(@"audioSelectionGroup: %@", audioSelectionGroup);

// [audioSelectionGroup options] // Array of the options in the group above.

And the select AVMediaSelectionOption (the audio channel you want) with:

[[avPlayerInstance currentItem] selectMediaOption:avMediaSelectionOptionInstance] inMediaSelectionGroup: audioSelectionGroup];

The same will work for video.

This is described in the "Selection of audio and subtitle media according to language and other criteria" section of the AV Foundation Release Notes for IOS 5 (3rd section).

like image 188
subclassed Avatar answered Nov 15 '22 20:11

subclassed