【发布时间】:2015-01-22 23:02:03
【问题描述】:
我正在将两个音频文件合并为一个。我设置了两个滑块来改变每个音频文件的音量。当我尝试为 AVAssetTrack 做 PreferredVolume 时,我得到这个(@lvalue Float)-> $T5 与浮动不同。有没有其他方法可以做到这一点?代码很快,但我不介意答案是否在目标 c 中。
编辑:如何使用滑块或浮动来更改每个音频文件的音量?
代码:
let type = AVMediaTypeAudio
let asset1 = AVURLAsset(URL: beatLocationURL, options: nil)
let arr2 = asset1.tracksWithMediaType(type)
let track2 = arr2.last as AVAssetTrack
track2.preferredVolume(beatVolume.value) <--where error occurs
let duration : CMTime = track2.timeRange.duration
let comp = AVMutableComposition()
let comptrack = comp.addMutableTrackWithMediaType(type,
preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
comptrack.insertTimeRange(CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)), ofTrack:track2, atTime:CMTimeMakeWithSeconds(0,600), error:nil)
comptrack.insertTimeRange(CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)), ofTrack:track2, atTime:CMTimeMakeWithSeconds(5,600), error:nil)
let type3 = AVMediaTypeAudio
let asset = AVURLAsset(URL: vocalURL, options:nil)
let arr3 = asset.tracksWithMediaType(type3)
let track3 = arr3.last as AVAssetTrack
let comptrack3 = comp.addMutableTrackWithMediaType(type3, preferredTrackID:Int32(kCMPersistentTrackID_Invalid))
comptrack3.insertTimeRange(CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(10,600)), ofTrack:track3, atTime:CMTimeMakeWithSeconds(0,600), error:nil)
let params = AVMutableAudioMixInputParameters(track:comptrack3)
params.setVolume(1, atTime:CMTimeMakeWithSeconds(0,600))
params.setVolumeRampFromStartVolume(1, toEndVolume:0, timeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(7,600), CMTimeMakeWithSeconds(3,600)))
let mix = AVMutableAudioMix()
mix.inputParameters = [params]
let item = AVPlayerItem(asset:comp)
item.audioMix = mix
【问题讨论】:
-
为什么我在我的问题中所说的反对票我使用滑块来改变音量。 beatvolume 是滑块, .value 以浮点格式获取值。对于你的回答,我已经尝试在preferredVolume 中设置一个浮点数,但我仍然得到同样的错误。
-
说真的,在我提出这个问题之前,我已经花了一整天的时间来解决这个问题。我阅读了苹果文档,它说 AVAssetTrack 和 AVMutableCompositionTrack 都有 preferredVolume 所以我尝试了 AVAssetTrack 并得到了我在问题中陈述的错误。然后我在我的问题中尝试了 AVMutableCompositionTrack ,但仍然遇到相同的错误。这也是可能的,因为我看到了一些针对目标 c 的解决方案,所以我不明白你想说什么
标签: ios objective-c audio swift avplayer