首页 文章

如何使用AVPlayer在备用音频流之间切换

提问于
浏览
8

我有一个测试应用程序,它使用AVPlayer播放由m3u8 HLS播放列表指定的视频 . 播放列表指定了几个备用音频流,类似于Apple提供的"Listing 10"示例播放列表:http://developer.apple.com/library/ios/#technotes/tn2288/_index.html#//apple_ref/doc/uid/DTS40012238-CH1-ALTERNATE_MEDIA应用程序需要能够在视频播放时在备用音频流之间切换 . 例如,应用程序应该能够在视频播放时通过用户点击应用程序中的按钮在英语,法语和西班牙语音频流之间切换 .

AVPlayer及其相关对象将使用哪些AVFoundation类和方法来切换m3u8播放列表中指定的音频流?我查看了AVFoundation类文档但看不到如何执行此操作 .

指向如何执行此操作的示例代码的链接将非常棒 . 我一直在网上搜索这些信息但没有成功 . 感谢您的帮助 .

1 回答

  • 19

    对于使用AVPlayer进行m3u8播放,看起来您无法使用AVAsset构建AVPlayerItem . 您需要直接从URI构造AVPlayerItem . 在使用此AVPlayerItem实例化AVPlayer,然后KVO侦听属性@ "status"时,如果状态为AVPlayerStatusReadyToPlay,则您将在 [[avPlayerInstance currentItem] asset] 内拥有资产 . 这将在AV Foundation Programming Guide的第20页中进行描述 .

    要将音频更改为各种替代项,请使用:

    AVMediaSelectionGroup *audioSelectionGroup = [[[avPlayerInstance currentItem] asset] mediaSelectionGroupForMediaCharacteristic: AVMediaCharacteristicAudible];
    
    NSLog(@"audioSelectionGroup: %@", audioSelectionGroup);
    
    // [audioSelectionGroup options] // Array of the options in the group above.
    

    并选择AVMediaSelectionOption(您想要的音频通道):

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

    这同样适用于视频 .

    这在AV Foundation Release Notes for IOS 5(第3部分)的"Selection of audio and subtitle media according to language and other criteria"部分中有所描述 .

相关问题