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 trackWithMediaType: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すべてのトラックがすぐにロードされない可能性があることです。audio_Assetメソッドを ( を取得した後に) load メソッドでラップして、より確実に保証することができます。

NSString *tracksKey = @"tracks";

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