【问题标题】:Separate Audio from Video将音频与视频分离
【发布时间】:2015-10-14 18:10:53
【问题描述】:
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 曲目WithMediaType:AVMediaTypeAudio] objectAtIndex:0] [__NSArrayM objectAtIndex:]: 索引 0 超出范围为空数组';

【问题讨论】:

    标签: ios avassetexportsession avurlasset


    【解决方案1】:

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

    [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 之后)包装在加载方法中以获得更好的保证:

    NSString *tracksKey = @"tracks";
    
    [audio_Asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:  ^{ 
    // rest of the code here
    

    【讨论】:

    • 我想将音频与视频分开,并根据sliderValue设置音频级别。但它在 iOS 7.0 中工作,但在 iOS 8 中出现问题。所以这不是正确的解决方案。可以试试吗?
    • 这有帮助吗:stackoverflow.com/questions/19326728/… 您必须先加载曲目才能使用它们
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2016-06-13
    • 2010-10-10
    • 2015-03-25
    • 1970-01-01
    相关资源
    最近更新 更多