You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中VideoPlayer无法播放含.aac音频的视频问题求助

解决AVPlayer无法播放含多通道AAC音频的视频问题

从你提供的Xcode日志来看,错误码1718449215对应kAudioConverterErr_FormatNotSupported,本质是4通道的AAC音频格式不被当前iOS系统的AVPlayer支持,这才导致视频无法播放。下面提供两种解决思路:

一、提前检测音频轨道异常并提示用户

可以先解析视频资产的音频轨道信息,判断是否存在不支持的配置(比如4通道AAC),提前给用户友好提示:

import AVFoundation

func checkVideoAudioCompatibility(videoURL: URL) -> Bool {
    let asset = AVURLAsset(url: videoURL)
    guard let audioTrack = asset.tracks(withMediaType: .audio).first else {
        // 无音频轨道,可正常播放
        return true
    }
    
    let formatDesc = audioTrack.formatDescriptions.first as! CMFormatDescription
    guard let audioFormat = CMAudioFormatDescriptionGetStreamBasicDescription(formatDesc)?.pointee else {
        return false
    }
    
    // 检测通道数和编码格式
    let channelCount = Int(audioFormat.mChannelsPerFrame)
    let codecType = audioTrack.formatDescriptions.compactMap {
        CMFormatDescriptionGetMediaSubType($0)
    }.first
    
    // iOS对4通道AAC的支持有限,这里做判断
    if channelCount == 4 && codecType == kAudioFormatMPEG4AAC {
        return false
    }
    return true
}

// 使用示例
if !checkVideoAudioCompatibility(videoURL: videoURL) {
    // 提示用户:当前视频音频格式不支持,将仅播放画面或无法播放
    print("该视频的音频格式不支持,无法正常播放")
}

二、剥离音频轨道,仅播放视频画面

如果需要强制播放视频,可以通过AVComposition只保留视频轨道,创建不含音频的AVPlayerItem:

import AVFoundation
import SwiftUI

func createVideoOnlyPlayerItem(from url: URL) -> AVPlayerItem? {
    let asset = AVURLAsset(url: url)
    guard let videoTrack = asset.tracks(withMediaType: .video).first else {
        return nil
    }
    
    let composition = AVMutableComposition()
    guard let videoCompositionTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid) else {
        return nil
    }
    
    do {
        try videoCompositionTrack.insertTimeRange(CMTimeRangeMake(start: .zero, duration: asset.duration), of: videoTrack, at: .zero)
    } catch {
        print("插入视频轨道失败:\(error.localizedDescription)")
        return nil
    }
    
    return AVPlayerItem(asset: composition)
}

// 在SwiftUI中使用
if let videoOnlyItem = createVideoOnlyPlayerItem(from: videoURL) {
    let player = AVPlayer(playerItem: videoOnlyItem)
    VideoPlayer(player: player)
        .onAppear {
            player.play()
        }
} else {
    // 提示用户视频无法处理
    Text("无法播放该视频")
}

补充说明

如果需要导出无音频的视频文件(比如保存到相机胶卷),可以使用AVAssetExportSession:

func exportVideoWithoutAudio(from url: URL, completion: @escaping (URL?) -> Void) {
    let asset = AVURLAsset(url: url)
    let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)!
    exportSession.outputFileType = .mov
    exportSession.shouldOptimizeForNetworkUse = true
    // 禁用音频导出
    exportSession.audioMix = AVMutableAudioMix()
    
    let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent("video_only_\(UUID().uuidString).mov")
    exportSession.outputURL = tempURL
    
    exportSession.exportAsynchronously {
        switch exportSession.status {
        case .completed:
            completion(tempURL)
        default:
            print("导出失败:\(exportSession.error?.localizedDescription ?? "未知错误")")
            completion(nil)
        }
    }
}

内容的提问来源于stack exchange,提问作者jostell13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 10:54:18