swift - iOS - change volume for each AVAssetTrack -
i have audio files need insert in avmutablecomposition
. each audio have different volume. accomplish created avmutabletrackcomposition
, avassettrack
each audio file. change volume every track using instance of avmutableaudiomix
.
let composition = avmutablecomposition() var trackmixarray = nsmutablearray() audio in layer{ let trackaudio:avmutablecompositiontrack = composition.addmutabletrackwithmediatype(avmediatypeaudio, preferredtrackid: cmpersistenttrackid()) let file = project.stringbyappendingpathcomponent(audio.name) let soundasset = avurlasset(url: nsurl(fileurlwithpath: file), options: option [nsobject : anyobject]) let sounds = soundasset.trackswithmediatype(avmediatypeaudio) var sound:avassettrack = sounds[0] as! avassettrack let duration:cmtime = sound.timerange.duration let audiotimerange:cmtimerange = cmtimerangefromtimetotime(kcmtimezero, duration) let start:cmtime = cmtimemakewithseconds(audio.start.todouble()!, 600) let stop:cmtime = cmtimemakewithseconds(audio.stop.todouble()!, 600) let trackmix = avmutableaudiomixinputparameters(track: trackaudio) trackmix.setvolume(audio.volume, attime: kcmtimezero) trackmixarray.addobject(trackmix) trackaudio.inserttimerange(audiotimerange, oftrack: sound, attime: start, error: nil) } let audiomix = avmutableaudiomix() audiomix.inputparameters = trackmixarray [anyobject]
using single avmutablecompositiontrack
more avassettrack
associated doesn't allow me change volume each track.
let trackmix = avmutableaudiomixinputparameters(track: sound) trackmix.setvolume(audio.volume, attime: kcmtimezero) trackmixarray.addobject(trackmix)
it's possible change volume directly avassettrack
?
avmutablecomposition *collagecomposition = [[avmutablecomposition alloc]init]; ... magic ... nsarray *trackstoduck = [collagecomposition trackswithmediatype:avmediatypeaudio]; audiomix = [avmutableaudiomix audiomix]; nsmutablearray *trackmixarray = [nsmutablearray array]; (int = 0; < [trackstoduck count]; i++) { avmutableaudiomixinputparameters *trackmix = [avmutableaudiomixinputparameters audiomixinputparameterswithtrack:[trackstoduck objectatindex:i]]; [trackmix setvolume:volume attime:kcmtimezero]; [trackmixarray addobject:trackmix]; } audiomix.inputparameters = trackmixarray; ... avassetexportsession magic ... generalexporter = [[avassetexportsession alloc] initwith... generalexporter.audiomix = audiomix;
this should change volume in each track, change swift.
edit: each avmutablecompositiontrack
have segments, each segment have starttime , duration, can use avmutableaudiomixinputparameters
change volume each cmtimerange
- (void)setvolumerampfromstartvolume:(float)startvolume toendvolume:(float)endvolume timerange:(cmtimerange)timerange
Comments
Post a Comment