【问题标题】:dispatch_get_main_queue not workingdispatch_get_main_queue 不工作
【发布时间】:2014-11-28 19:49:47
【问题描述】:

我的 iOS 基础 SDK 是 8.1。当我在 8.1 模拟器上运行时,dispatch_get_main_queue 工作正常。但是,当我在 7.1 模拟器上运行它时,它不会被调用。我注意到dispatch_get_main_queue 已在 iOS 8.0 及更高版本中重新实现。

我该如何解决这个问题?更改基础 SDK 还是什么?

我的代码

    AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

    // audio track
    AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    //
    NSError *error;

    AVAsset *videoAsset = [AVAsset assetWithURL:videoURL];
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                        ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] firstObject]
                         atTime:kCMTimeZero
                          error:&error];
    if (error) {
        NSLog(@"extract audio error!");
        return;
    }
    error = nil;

    // audio path
    NSString *path = [NSString stringWithFormat:@"%@newAudio.m4a", NSTemporaryDirectory()];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
            NSLog(@"audio cannot be saved!");
        }
    }
    // exporter
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                      presetName:AVAssetExportPresetAppleM4A];
    exporter.outputURL = [NSURL fileURLWithPath:path];
    exporter.outputFileType = AVFileTypeAppleM4A;
    exporter.shouldOptimizeForNetworkUse = YES;
    [exporter exportAsynchronouslyWithCompletionHandler:^{
        //NSLog(@"export status: %ld", exporter.status);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self exportDidFinish:exporter];
        });
    }];
}

【问题讨论】:

  • 您有一些示例代码可以向我们展示您在做什么吗?我一直在 iOS 8 中使用 dispatch_get_main_queue,就像在 iOS 7 中一样,到目前为止还没有遇到任何问题。
  • @Kai [exporter exportAsynchronouslyWithCompletionHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self exportDidFinish:exporter]; }); }];
  • 您是否已经在[self exportDidFinish:exporter]; 上设置断点以确认您从未在iOS 7.1 模拟器中执行该行?您需要将示例分成单独的行才能进行测试。如果这没有产生任何有用的东西,我建议为statuserror 添加一些KVO 代码(如exportAsynchronouslyWithCompletionHandler: 的文档所建议的那样),以查看导出是否由于某种原因没有完成。跨度>
  • @Kai 使用iOS 7.1模拟器的时候好像取消了状态。可能是什么问题?因为它在 iOS 8.1 模拟器上运行良好。
  • error 中是否显示任何内容?

标签: ios ios8.1 base-sdk


【解决方案1】:

我终于想通了。 presetName 需要是 AVAssetExportPresetPassthrough 以便它在 iOS 7 模拟器上正常工作。我真的不知道为什么,但感谢@Kai 和@Rob 的回复。

【讨论】:

    猜你喜欢
    • 2012-09-23
    • 2023-03-19
    • 2016-09-13
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多