1

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?

Code:

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
2
  • why the downvote i stated in my question i was using the slider to change the volume. beatvolume is the slider and .value is getting the value in float format. And to your answer i already tried setting a float in preferredVolume but i still get the same error. Nov 25, 2014 at 3:38
  • 1
    seriously i have spent the whole day working on this before i put up this question. I read the apple docs and it said AVAssetTrack and AVMutableCompositionTrack both had preferredVolume so i tried it for the AVAssetTrack and get the error i stated in my question. Then i tried it for AVMutableCompositionTrack like in my question and still got the same error. And also it is possible because i saw a couple solutions for objective c so i dont understand what your trying to say Nov 25, 2014 at 4:23

1 Answer 1

0

You can't say this:

track2.preferredVolume(beatVolume.value)

If you want to set track2.preferredVolume, then say:

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: https://developer.apple.com/library/ios/qa/qa1716/_index.html.)

7
  • i inserted that after comptrack3.insertTimeRange didn't work. Then i did it before comptrack3.insertTimeRange and still didnt work. Nov 25, 2014 at 5:19
  • What "didn't work"? That's meaningless. What happened?
    – matt
    Nov 25, 2014 at 5:28
  • i set the preferredVolume = 0.0 and i run my app but i still here the sound so the volume didnt change Nov 25, 2014 at 5:36
  • Right. But that isn't what you asked. You were having a syntax problem. Your code didn't even compile. I showed you how to fix it. Now you are having a functionality problem. That's a different matter.
    – matt
    Nov 25, 2014 at 5:55
  • how would i get the volume to change? Nov 25, 2014 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.