简体   繁体   English

iOS上的AVCapture会话中无音频

[英]No Audio In AVCapture Session on iOS

My app simulates a FaceTime call, while simultaneously recording the people using it through the front facing camera, and showing a preview of it on the screen, as though it were a real FaceTime call. 我的应用程序模拟FaceTime通话,同时通过前置摄像头记录使用它的人,并在屏幕上显示其预览,就好像它是真实的FaceTime通话一样。 It then plays a pre-recorded video as the "other person" on the FaceTime call. 然后,它在FaceTime通话中以“其他人”的身份播放预先录制的视频。 It is supposed to save the video off the front camera either when the End button is pressed, or when the main movie ends. 当按下“结束”按钮或主电影结束时,应该将视频保存在前置摄像头之外。 In the former, it works perfect, but if the main movie runs to completion, the saved output of the movie has no audio. 在前者中,它可以完美运行,但是如果主电影运行完毕,则电影的保存输出将没有音频。 Any ideas what is going on? 有什么想法吗?

#import "MovieView.h"
#import <AudioToolbox/AudioToolbox.h>

@implementation MovieView
@synthesize selectedCountry, vImagePreview, playit;

- (void)viewDidLoad { 
    [super viewDidLoad];
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM d hh:mm:ss a"];
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
    NSString *currentTime = [dateFormatter stringFromDate:today];

    self.navigationController.navigationBarHidden = YES;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

    NSError* error4 = nil;
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryAmbient error:&error4];
    OSStatus propertySetError = 0; 
    UInt32 allowMixing = true; 
    propertySetError |= AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(allowMixing), &allowMixing);

    // Activate the audio session
    error4 = nil;
    if (![audioSession setActive:YES error:&error4]) {
        NSLog(@"AVAudioSession setActive:YES failed: %@", [error4 localizedDescription]);
    }






   //tests


    // Set audio session category to "play and record"
       //endtests

    //this is the end of recording the video and audio
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];

   NSString *proud = [[documentsDirectoryPath stringByAppendingPathComponent:@"proud"] stringByAppendingPathComponent:selectedCountry];

    NSURL  *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"BlueHarvest" ofType:@"mp4"]];
    player =

    [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
    player.useApplicationAudioSession=YES;

    [player prepareToPlay];
    player.controlStyle = MPMovieControlStyleNone;
    player.allowsAirPlay = NO;
    player.scalingMode = MPMovieScalingModeFill;   

    player.view.frame = self.view.frame;

    [self.view insertSubview:player.view belowSubview:vImagePreview];

    [player setFullscreen:YES animated:YES];

    // ...

    [[NSNotificationCenter defaultCenter] 
     addObserver:self
     selector:@selector(movieFinishedCallback:)
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:player];


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerWillExitFullscreen:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:player];


    [player play];


    session = [[AVCaptureSession alloc] init];
    [session beginConfiguration];
    session.sessionPreset = AVCaptureSessionPresetMedium;

    CALayer *viewLayer = self.vImagePreview.layer;
    NSLog(@"viewLayer = %@", viewLayer);

    self.captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
    [self.captureVideoPreviewLayer setCornerRadius:14];
    [self.captureVideoPreviewLayer setBorderWidth:3.0];
    [self.captureVideoPreviewLayer setBorderColor:[[UIColor whiteColor] CGColor]];
    self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
    [[vImagePreview layer] setCornerRadius:14];


    [[vImagePreview layer] setBorderWidth:3.0];


    [[vImagePreview layer] setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.vImagePreview.layer addSublayer:self.captureVideoPreviewLayer];
    AVCaptureDevice *device = [self frontFacingCameraIfAvailable];
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }

    AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    NSError *error2 = nil;
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error2];





    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

    NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"];
    NSString *editedfilename = [[selectedCountry lastPathComponent] stringByDeletingPathExtension];
    NSString *datestring = [[editedfilename stringByAppendingString:@" "] stringByAppendingString:currentTime];
    NSLog(@"%@", datestring);
    NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:datestring] stringByAppendingString:@".mp4"];
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie];
    [session addInput:input];
    [session addInput:audioInput];
    [session addOutput:movieFileOutput];
    AVCaptureConnection *videoConnection = nil;

    for ( AVCaptureConnection *connection in [movieFileOutput connections] )
    {
        NSLog(@"%@", connection);
        for ( AVCaptureInputPort *port in [connection inputPorts] )
        {
            NSLog(@"%@", port);
            if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
            }
        }
    }

    if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false**
    {
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
    }

    [session commitConfiguration];
    [session startRunning];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];




    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];

    NSLog(@"OutputURL%@", outputURL);

}
-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (orientation) {
        case UIInterfaceOrientationPortrait:
            [self.captureVideoPreviewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            [self.captureVideoPreviewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
            break;
        case UIInterfaceOrientationLandscapeLeft:
            [self.captureVideoPreviewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
            break;
        case UIInterfaceOrientationLandscapeRight:
            [self.captureVideoPreviewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
            break;
    }
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {
    //finished
    NSLog(@"ErrorMessage%@", error);
}
-(IBAction)endcall {

    [player stop];
    [session stopRunning];
}
-(AVCaptureDevice *)frontFacingCameraIfAvailable
{
    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *captureDevice = nil;
    for (AVCaptureDevice *device in videoDevices)
    {
        if (device.position == AVCaptureDevicePositionFront)
        {
            captureDevice = device;

            break;
        }
    }

    //  couldn't find one on the front, so just get the default video device.
    if ( ! captureDevice)
    {
        captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    }

    return captureDevice;
}

- (void) movieFinishedCallback:(NSNotification*) aNotification {    
    NSLog(@"MovieDone");
     [player stop];
    [player.view removeFromSuperview];   
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];
    [session stopRunning];
    [self.navigationController popToRootViewControllerAnimated:NO];

}

- (void) exitedFullscreen:(NSNotification*) aNotification {   
    NSLog(@"MovieDone");
    [player.view removeFromSuperview];   
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerDidExitFullscreenNotification
     object:player];
}
- (void)moviePlayerWillExitFullscreen:(NSNotification*) aNotification {
    [player stop];
    [session stopRunning];
    [self dismissMoviePlayerViewControllerAnimated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerWillExitFullscreenNotification object:player];
}



@end

找到了。

movieFileOutput.movieFragmentInterval = kCMTimeInvalid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM