0
float sliderValue = 0.5;
NSURL *audio_url = [[NSBundle mainBundle] pathForResource:@“video_fileName” ofType:@"mp4"]];
AVURLAsset* audio_Asset = [[AVURLAsset alloc]initWithURL:audio_url options:nil];
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];

NSString* formattedNumber = [NSString stringWithFormat:@"%.01f", sliderValue];
NSLog(@"formattedNumber %@",formattedNumber);
NSLog(@"formattedNumber %.01f",[formattedNumber floatValue]);

[audioInputParams setVolume:[formattedNumber floatValue] atTime:kCMTimeZero];
[audioInputParams setTrackID:[[[audio_Asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]  trackID]];
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = [NSArray arrayWithObject:audioInputParams];



AVAssetExportSession *exportSession=[AVAssetExportSession exportSessionWithAsset:audio_Asset presetName:AVAssetExportPresetAppleM4A];
exportSession.audioMix = audioMix;

exportSession.outputURL=[NSURL fileURLWithPath:audioPath];
exportSession.outputFileType=AVFileTypeAppleM4A;

[exportSession exportAsynchronouslyWithCompletionHandler:^{
    if (exportSession.status==AVAssetExportSessionStatusFailed) {
        NSLog(@"failed");
    }
    else {
        NSLog(@"AudioLocation : %@",audioPath);
    }
}];

问题:资产空白,因此应用程序崩溃。

[[audio_Asset trackingWithMediaType:AVMediaTypeAudio] objectAtIndex:0] [__NSArrayM objectAtIndex:]: 索引 0 超出空数组的范围';

4

1 回答 1

0

崩溃已经描述了这个问题,您正在尝试访问超出其边界的数组。改变:

[audioInputParams setTrackID:[[[audio_Asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]  trackID]];

至:

NSArray * tracks = [audio_Asset tracksWithMediaType:AVMediaTypeAudio];
if([tracks count]) {
    [audioInputParams setTrackID:[[tracks firstObject]  trackID]];
}

您的主要问题可能是加载AVURLAsset可能不会立即加载所有曲目。您可以将您的方法(在 get 之后audio_Asset)包装在 load 方法中以获得更好的保证:

NSString *tracksKey = @"tracks";

[audio_Asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:  ^{ 
// rest of the code here
于 2015-10-14T18:29:27.267 回答