gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:48:30 26 4
gpt4 key购买 nike

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

在下面的示例代码中,我要做的就是尝试将音频剪辑添加到AVMutableCompositionTrack。这是代码中发生的事情:

  • AVMutableComposition和AVMutableCompositionTrack对象是在init中创建的。
  • 调用类方法addPhrase:atTime:将音频剪辑添加到合成音轨。
  • 实际插入的函数是addAudioClip:atTime:。
  • 帮助程序函数audioClipWithName:ofType将请求的音频文件加载到AVAssetTrack中。

  • -> NSLOG输出:

    insertTimeRange atTime = 0.0范围=(0.0,2.1)compositionTrack =
    - - - - - 错误!错误域= AVFoundationErrorDomain代码= -11800“操作无法完成” UserInfo = {NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x16fc1c90 {Error Domain = NSOSStatusErrorDomain代码= -12780“(null)”},NSLocalizedFailureReason = An发生未知错误(-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

    最佳答案

    你的问题在于

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

    它使轨道拥有的 AVAsset超出范围并被释放,从而使轨道处于不良状态。

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

    关于ios - insertTimeRange:ofTrack:atTime:error:(类AVMutableCompositionTrack)在iPhone上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443484/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com