iOS-更改每个AVAssetTrack的音量
我有一些音频文件需要插入到iOS-更改每个AVAssetTrack的音量,ios,swift,avmutablecomposition,Ios,Swift,Avmutablecomposition,我有一些音频文件需要插入到AVMutableComposition中。每个音频都有不同的音量。为此,我为每个音频文件创建了一个AVMutableTrackComposition和一个AVAssetTrack。因此,我使用AVMutableAudioMix的实例更改每个曲目的音量 let composition = AVMutableComposition() var trackMixArray = NSMutableArray() for audio in layer{ let trac
AVMutableComposition
中。每个音频都有不同的音量。为此,我为每个音频文件创建了一个AVMutableTrackComposition
和一个AVAssetTrack
。因此,我使用AVMutableAudioMix
的实例更改每个曲目的音量
let composition = AVMutableComposition()
var trackMixArray = NSMutableArray()
for 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 as [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 as [AnyObject]
使用单个AVMutableCompositionTrack
以及与其关联的更多AVAssetTrack
不允许我更改每个曲目的音量
let trackMix = AVMutableAudioMixInputParameters(track: sound)
trackMix.setVolume(audio.volume, atTime: kCMTimeZero)
trackMixArray.addObject(trackMix)
可以直接从
AVAssetTrack
更改音量?您所说的“不允许我更改每个曲目的音量”是什么意思?“直接改变音量”是什么意思?清楚地解释你想做什么。我可以更改每个AVMutableCompositionTrack的音量,但不能更改每个AVAssetTrack的音量。为此,我必须将每个AVAssetTrack分配给AVMutableCompositionTrack,并更改AVMutableCompositionTrack中的卷。我的目标是使用单个AVMutableCompositionTrack,因为使用它会降低AvPlayerItem的声明速度。我仍然没有看到问题。您已经很好地解释了如何设置资产跟踪的首选卷,方法是将其下拉到可变组合跟踪子类,在该子类中它是可变的。那就这么做吧。可变组合轨迹是资产轨迹;可以在任何可以使用后者的地方使用前者。子类。看到了吗?这会更改每个AVMutableCompositionTrack的卷,而不是每个AVAssetTrack的卷
AVMutableComposition *collageComposition = [[AVMutableComposition alloc]init];
...
Some Magic
...
NSArray *tracksToDuck = [collageComposition tracksWithMediaType:AVMediaTypeAudio];
audioMix = [AVMutableAudioMix audioMix];
NSMutableArray *trackMixArray = [NSMutableArray array];
for (int i = 0; i < [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;
- (void)setVolumeRampFromStartVolume:(float)startVolume
toEndVolume:(float)endVolume
timeRange:(CMTimeRange)timeRange