如何使用 HLS (m3u8) 在 Swift AVPlayer 中播放不同视频质量的单个音频?

问题描述 投票:0回答:1

我正在 Swift 中开发一个视频流应用程序,该应用程序利用具有多种视频质量的 HLS(HTTP 实时流媒体)。我正在使用 AVPlayer 框架来播放视频。主 m3u8 文件指定不同的视频质量以及音频选项。然而,我在配置 AVPlayer 以针对每种视频质量播放适当的音频流方面面临挑战。

这是我的 m3u8 主文件的摘录:

#EXTM3U

#EXT-X-INDEPENDENT-SEGMENTS

#EXT-X-MEDIA:TYPE=AUDIO,URI="audio/audio.m3u8",GROUP-ID="audio",LANGUAGE="en",NAME="ENGLISH",AUTOSELECT=YES,CHANNELS="2"

#EXT-X-STREAM-INF:BANDWIDTH=745697,AVERAGE-BANDWIDTH=508245,CODECS="avc1.4d401e,mp4a.40.2",RESOLUTION=854x480,FRAME-RATE=25.000,VIDEO-RANGE=SDR,AUDIO="audio",CLOSED-CAPTIONS=NONE
480p/22534041-c4c5-4b5b-be4e-e7035e4ffdb6.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=961804,AVERAGE-BANDWIDTH=663000,CODECS="avc1.4d401f,mp4a.40.2",RESOLUTION=1280x720,FRAME-RATE=25.000,VIDEO-RANGE=SDR,AUDIO="audio",CLOSED-CAPTIONS=NONE
720p/04b4f73f-3f43-4c88-895f-931c22564e3d.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=534811,AVERAGE-BANDWIDTH=389186,CODECS="avc1.4d401e,mp4a.40.2",RESOLUTION=640x360,FRAME-RATE=25.000,VIDEO-RANGE=SDR,AUDIO="audio",CLOSED-CAPTIONS=NONE
360p/df767a2b-833a-4772-94cd-099e8ccf48e4.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2914202,AVERAGE-BANDWIDTH=2041333,CODECS="avc1.640028,mp4a.40.2",RESOLUTION=1920x1080,FRAME-RATE=25.000,VIDEO-RANGE=SDR,AUDIO="audio",CLOSED-CAPTIONS=NONE
1080p/f0c10df4-df65-4800-9e45-ff684357d4d7.m3u8

我需要有关如何在 Swift 中配置 AVPlayer 的指导,以确保播放适当的音频流以及所选的视频质量。我应该通过监视用户质量选择然后更新 AVPlayer 的音轨以编程方式处理此问题,还是有更自动化的方法来实现视频质量和音频选择之间的集成?

swift avplayer http-live-streaming
1个回答
0
投票

注意:请将 VideoQuality 枚举案例和 URL 替换为您的实际 URL 和质量定义。

import AVFoundation
    
    // This is just an example enum representing video quality levels
    enum VideoQuality: String {
        case sd480p
        case hd720p
        case hd1080p
        
        var playlistURL: String {
            return "\(rawValue)/playlist.m3u8"
        }
    }
    
    // This mapping relates video quality to its associated audio group ID
    var qualityToAudioMapping: [VideoQuality: String] = [
        .sd480p: "audio",
        .hd720p: "audio",
        .hd1080p: "audio"
    ]
    
    // User selects a video quality
    let selectedQuality: VideoQuality = .hd720p
    
    // Get the corresponding audio group ID
    guard let audioGroupID = qualityToAudioMapping[selectedQuality] else {
        fatalError("No audio group associated with the selected quality")
    }
    
    // Create AVPlayerItem for the selected video quality
    let videoURL = URL(string: selectedQuality.playlistURL)!
    let playerItem = AVPlayerItem(url: videoURL)
    
    // Select the appropriate audio track
    if let audioSelectionGroup = playerItem.asset.mediaSelectionGroup(forMediaCharacteristic: .audible) {
        for option in audioSelectionGroup.options {
            if option.identifier?.rawValue == audioGroupID {
                playerItem.select(option, in: audioSelectionGroup)
                break
            }
        }
    }
    
    // Create AVPlayer with the configured AVPlayerItem
    let player = AVPlayer(playerItem: playerItem)
    
    // Present or play the player as needed
    // For example, using AVPlayerViewController
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    present(playerViewController, animated: true) {
        player.play()
    }
© www.soinside.com 2019 - 2024. All rights reserved.