【问题标题】:Adjust Volume ios调整音量 ios
【发布时间】: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


【解决方案1】:

你不能这样说:

track2.preferredVolume(beatVolume.value)

如果你想设置track2.preferredVolume,那么说:

track2.preferredVolume = beatVolume.value

当然,这只有在 beatVolume.value 是浮点数时才有效。如果不是,您将不得不以某种方式制作一个 Float。

此外,您将无法设置 preferredVolumetrack2,因为它是 AVAssetTrack。 AVAssetTrack 的 preferredVolume 不可设置。您要做的是等到您设置 AVMutableComposition 并在 its 轨道上设置音量。例如:

comptrack3.preferredVolume = 0.5

这将编译,现在您可以弄清楚如何将其他浮点数替换为实际值。 (但是,它不会改变一个轨道相对于另一个轨道的音量。如果这是您的目标,请使用 AVMutableAudioMix。例如,请参阅此处的 Apple 示例代码:https://developer.apple.com/library/ios/qa/qa1716/_index.html.

【讨论】:

  • 我在 comptrack3.insertTimeRange 之后插入了它不起作用。然后我在 comptrack3.insertTimeRange 之前做了它,仍然没有工作。
  • 什么“没用”?那是没有意义的。发生了什么?
  • 我设置了preferredVolume = 0.0,然后我运行了我的应用程序,但我仍然在这里声音,所以音量没有改变
  • 对。但这不是你问的。您遇到了语法问题。你的代码甚至没有编译。我向你展示了如何解决它。现在您遇到了功能问题。那是另一回事。
  • 如何让音量改变?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
相关资源
最近更新 更多