假设您的视频文件很小
let p = Bundle.main.path(forResource: "small", ofType: "mp4")
let url = NSURL.fileURL(withPath: p!)
您可以轻松地将其设为播放器项目并播放..
let pi = AVPlayerItem(url: url)
av.player = AVPlayer(playerItem: pi)
或者,您可以将其设置为AVURLAsset,然后获取其中一个曲目
asset = AVURLAsset(url: url)
let tracks:[AVAssetTrack] = asset!.tracks
print(tracks.count)
let track:AVAssetTrack = tracks[2]
那....您怎么播放其中一首曲目?
您如何播放AVAssetTrack?
答案 0 :(得分:1)
以下代码显示了如何创建仅包含所需曲目的AVMutableComposition
,然后像播放任何AVAsset
一样播放构图,因为也许是令人惊讶的考虑了名称,它是一个:
let asset = AVURLAsset(url: url)
let track = asset.tracks[1] // or whatever
let composition = AVMutableComposition()
let compositionTrack = composition.addMutableTrack(withMediaType: track.mediaType, preferredTrackID: kCMPersistentTrackID_Invalid)
try! compositionTrack?.insertTimeRange(CMTimeRange(start: .zero, duration: asset.duration), of: track, at: .zero)
let playerItem = AVPlayerItem(asset: composition) // play me in your AVPlayer
现实生活中的 N.B。应该tracks
和duration
异步加载。