Search code examples
iosobjective-cuiimagepickercontrolleruistatusbaravassetexportsession

UIStatusBar become red after using AVAssetExportSession for UIImagePickerController media conversion


I am converting the .MOV video i get from UIImagePickerController to the .mp4 format using AVAssetExportSession. Once the conversion is completed I send the data to a server. Everything works fine, except the status bar become red and pulsing after the transmission is completed. If I put the app in background and open it again, then the status bar returns to its normal status again.

This is what I think is the piece of code that causes this behavior:

 //I took a video

    __block NSString *messageType;
    __block NSData *messageData;
    __block NSString *messageText;

    [...]

    NSURL *url =  [info objectForKey:UIImagePickerControllerMediaURL];

    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    NSString *videoPath = nil;
    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
    {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];

        videoPath = [[NSTemporaryDirectory() stringByAppendingPathComponent:videoDirectory]stringByAppendingPathComponent:tempVideoFileName];
        exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
        NSLog(@"videopath of your mp4 file = %@",videoPath);  // PATH OF YOUR .mp4 FILE
        exportSession.outputFileType = AVFileTypeMPEG4;

        [exportSession exportAsynchronouslyWithCompletionHandler:^{

            switch ([exportSession status]) {

                case AVAssetExportSessionStatusFailed:{
                    NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
                    [picker dismissViewControllerAnimated:YES completion:^{
                    [SVProgressHUD showErrorWithStatus:[[exportSession error] localizedDescription]];
                    }];
                }
                    break;

                case AVAssetExportSessionStatusCancelled:

                    NSLog(@"Export canceled");

                    break;

                case AVAssetExportSessionStatusCompleted:{

                    messageData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoPath]];
                    messageText = videoPath;
                    messageType = kMessageTypeVideo;

                    // This method sends asynchronously the data to the server
                    [self sendMediaType:messageType MessageData:messageData MessageText:messageText];

                    [picker dismissViewControllerAnimated:YES completion:NULL];

                }

                    break;

                default:

                    break;

            }


        }];

    }

Is there a way to avoid the appearance of the red status bar or at least the way I should use to make it disappear?


Solution

  • It turns out that in some way the recording session of the UIImagePickerController went in conflict with the AVAssetExportSession.

    I resolved this issue by converting the video after having dismissed the UIImagePicker controller:

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeImage, 0)
        == kCFCompareEqualTo) {
    
        [....]
    
    }
    else{
        //Took a video
        NSURL *url =  [info objectForKey:UIImagePickerControllerMediaURL];
    
        [picker dismissViewControllerAnimated:YES completion:^{
    
            [self convertAndSendVideo:url];
        }];
    
    }
    
    }
    
    -(void)convertAndSendVideo:(NSURL *)url{
    
    __block NSString *messageType;
    __block NSData *messageData;
    __block NSString *messageText;
    
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    NSString *videoPath = nil;
    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
    {
        __block AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];
    
        videoPath = [[NSTemporaryDirectory() stringByAppendingPathComponent:videoDirectory]stringByAppendingPathComponent:tempVideoFileName];
        exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
        NSLog(@"videopath of your mp4 file = %@",videoPath);  // PATH OF YOUR .mp4 FILE
        exportSession.outputFileType = AVFileTypeMPEG4;
    
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
    
            switch ([exportSession status]) {
    
                case AVAssetExportSessionStatusFailed:{
                    NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
                    [SVProgressHUD showErrorWithStatus:[[exportSession error] localizedDescription]];
                }
                    break;
    
                case AVAssetExportSessionStatusCancelled:
    
                    NSLog(@"Export canceled");
    
                    break;
    
                case AVAssetExportSessionStatusCompleted:{
    
                    messageData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoPath]];
                    messageText = videoPath;
                    messageType = kMessageTypeVideo;
    
                    [self sendMediaType:messageType MessageData:messageData MessageText:messageText];
    
                }
    
                    break;
    
                default:
    
                    break;
    
            }
    
    
        }];
    
    }
    
    }