-1

My iOS base SDK is 8.1. dispatch_get_main_queue works fine when I'm running on 8.1 simulator. However when I run it on 7.1 simulator, it does not get called. I noticed that dispatch_get_main_queue has been reimplemented in iOS 8.0 and later.

How can I solve this problem? change base SDK or what?

my code

    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];
        });
    }];
}
  • Do you have some sample code to show us what you're doing? I've been using `dispatch_get_main_queue` in iOS 8 the same way I did with iOS 7 and haven't experienced any problems so far. – Kai Schaller Nov 28 '14 at 19:56
  • @Kai [exporter exportAsynchronouslyWithCompletionHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self exportDidFinish:exporter]; }); }]; – CancerKnight Nov 28 '14 at 19:58
  • Have you already set a breakpoint on `[self exportDidFinish:exporter];` to confirm that you are never executing that line in the iOS 7.1 simulator? You'll need to break your example into separate lines in order to test. If that doesn't yield anything useful, I would recommend adding some KVO code (as suggested by the documentation for [`exportAsynchronouslyWithCompletionHandler:`](http://goo.gl/hxbmMI)) for `status` and `error` to see if the export isn't completing for some reason. – Kai Schaller Nov 28 '14 at 20:10
  • @Kai It seems that the status is cancelled when using iOS 7.1 simulator. What can be the problem? since it works fine on iOS 8.1 simulator. – CancerKnight Nov 28 '14 at 20:52
  • Does anything show up in `error`? – Kai Schaller Nov 28 '14 at 20:59
  • no error or warnings @Kai – CancerKnight Nov 28 '14 at 21:01
  • @CancerKnight If you want us to help you, you need to be far more clear. Is the problem really that dispatching to the main queue is not working (which is highly unlikely) as suggested by this question's title? Or is it that the exporter status is cancelled? Or is it that completion block is not getting called? You have to be more specific as to what the precise point of failure is. You have to give us an [example](http://stackoverflow.com/help/mcve) we can run and reproduce the behavior you describe. – Rob Nov 29 '14 at 02:32

1 Answers1

0

Finally i figured it out. The presetName need to be AVAssetExportPresetPassthrough so that it works fine on iOS 7 simulator. I don't really know why, but thank you @Kai and @Rob for your replies.