insertTimeRange:ofTrack:atTime:error :(类AVMutableCompositionTrack)在iPhone上失败

时间:2016-09-12 04:44:41

标签: ios objective-c iphone avasset avmutablecomposition

insertTimeRange:ofTrack:atTime:error:类AVMutableCompositionTrack返回NO并且错误"操作无法完成"在iPhone上(但不是模拟器)。错误消息是无益的,我很难找到错误的原因,更不用说解决方法了。任何帮助将不胜感激。

在下面的示例代码中,我正在尝试将音频剪辑添加到AVMutableCompositionTrack。以下是代码中的内容:

  1. AVMutableComposition和AVMutableCompositionTrack对象在init中创建。

  2. 调用类方法addPhrase:atTime:将音频剪辑添加到合成轨道。

  3. 实际插入的函数是addAudioClip:atTime:。

  4. 辅助函数audioClipWithName:ofType将请求的音频文件加载到AVAssetTrack中。

  5. - > NSLOG输出:

    insertTimeRange atTime = 0.0 range =(0.0,2.1)compositionTrack = - - - - - 错误!错误域= AVFoundationErrorDomain代码= -11800"操作无法完成" UserInfo = {NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x16fc1c90 {错误域= NSOSStatusErrorDomain代码= -12780"(null)"},NSLocalizedFailureReason =发生未知错误(-12780)}

    - >样本代码:

    @interface MyAudioPlayer()
    
    @property (strong, nonatomic) AVMutableComposition *composition;
    @property (strong, nonatomic) AVMutableCompositionTrack *compositionTrack;
    
    @end
    
    
    @implementation MyAudioPlayer
    
    - (id) init
    {
        self = [super init];
        if (self != nil) {
            self.composition = [AVMutableComposition composition];
            self.compositionTrack = [self.composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        }
        return self;
    }
    
    // Add the phrase into compositionAudioTrack at the given time.
    - (void) addPhrase:(NSString *)phrase atTime:(CMTime)atTime
    {
        // Add the phrase to the audio.
        AVAssetTrack *audioClip = [self audioClipWithName:phrase ofType:@"mp3"];
        // Add the phrase to the audio track.
        [self addAudioClip:audioClip atTime];
    }
    
    // Add the given audio clip to the composed audio at the given time.
    - (void) addAudioClip:(AVAssetTrack *)audioClip atTime:(CMTime)atTime
    {
        NSError *error = [[NSError alloc] init];
        CMTimeRange audioClipRange = CMTimeRangeMake(kCMTimeZero, audioClip.timeRange.duration);
        BOOL success = [self.compositionTrack insertTimeRange:audioClipRange ofTrack:audioClip atTime:atTime error:&error];
        NSLog(@"insertTimeRange atTime=%.1f range=(%.1f, %.1f) compositionTrack=%@\n", CMTimeGetSeconds(atTime), CMTimeGetSeconds(audioClipRange.start), CMTimeGetSeconds(audioClipRange.duration), self.compositionTrack);
        if (!success) {
            NSLog(@"----------ERROR! %@", error);
        }
    }
    
    // Return an audio clip with the given name and type.
    - (AVAssetTrack *) audioClipWithName:(NSString *)name ofType:(NSString *)type
    {
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
        AVAsset *avAsset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
        AVAssetTrack *audioClip = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        return audioClip;
    }
    
    @end
    

1 个答案:

答案 0 :(得分:5)

你的问题在于

- (AVAssetTrack *) audioClipWithName:(NSString *)name ofType:(NSString *)type;

它允许轨道拥有AVAsset超出范围并被取消分配,使轨道处于不良状态。

确保AVAsset被引用(至少),直到您致电insertTimeRange。实际上,这意味着您应该将AVAsset及其AVAssetTrack一起传递。