AVPlayer:淡入视频音频

时间:2018-08-10 14:54:50

标签: ios swift avfoundation avplayer

我正在尝试使用AVPlayer淡入视频开头的音轨。播放视频时我没有任何错误,似乎音量已经达到100%,并且我的volumeRamp音频淡入没有执行。

这是我的代码:

    guard let url = (self.videoView.player?.currentItem?.asset as? AVURLAsset)?.url else { return }

    let asset = AVAsset(url: url) as AVAsset
    let duration = asset.duration
    let durationInSeconds = CMTimeGetSeconds(duration)
    let playerItem = AVPlayerItem(asset: asset)

    let firstSecond = CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(1, 1))

    // Configure audio mix to fade in

    let audioParams = AVMutableAudioMixInputParameters(track: asset.tracks.first!)
    audioParams.setVolumeRamp(fromStartVolume: 0, toEndVolume: 1, timeRange: firstSecond)

    let mix = AVMutableAudioMix()
    mix.inputParameters = [audioParams]
    playerItem.audioMix = mix

    self.videoView.player?.replaceCurrentItem(with: playerItem)
    self.videoView.player?.play()

我做错什么了吗? (我正在使用模拟器进行测试)

1 个答案:

答案 0 :(得分:0)

如此处所述:Apple Library

AVAudioMix仅支持基于文件的资产。