Adjust Volume ios


I am combining two audio files into one. I set up two sliders to change volume of each audio file. When i try to do preferredVolume for an AVAssetTrack i get this (@lvalue Float) -> $T5 is not identical to float. Are there any other ways to accomplish this? Code is in swift but i dont mind if answer is in objective c.

EDIT: How can i change the volume of each audio file with a slider or with a float?


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


Your code is meaningless. You can't say this:


That's nonsense. What are you trying to do? If you want to set track2.preferredVolume, then set it:

track2.preferredVolume = beatVolume.value

Of course, that will only work if beatVolume.value is a Float. If it isn't, you will have to make a Float out of it somehow.

Also, you won't be able to set the preferredVolume of track2, because it's an AVAssetTrack. An AVAssetTrack's preferredVolume isn't settable. What you want to do is wait until you're setting up your AVMutableComposition and set the volumes on its tracks. For example:

comptrack3.preferredVolume = 0.5

That will compile, and now you can figure out how to substitute some other Float as the real value. (It will not, however, change the volume of one track relative to another. If that's your goal, use an AVMutableAudioMix. See, for example, Apple's sample code here:

By : matt

This video can help you solving your question :)
By: admin