2015-06-24 20 views
5

Tôi có một ứng dụng khi lượt xem được tải lên, bắt đầu quay video và âm thanh và sau khi hoàn thành, hãy ghi lại cả thư mục tài liệu của ứng dụng, cùng với Camera Roll của iPad nó đang chạy. Tôi đã chắc chắn và thêm vào đầu vào cho phiên cho cả âm thanh và video, nhưng khi tôi đi xem video đã lưu, không có âm thanh với nó. Bất cứ ai có thể phát hiện bất cứ điều gì trong mã của tôi mà sẽ chỉ ra nơi mà vấn đề là?Âm thanh không ghi trong AVCaptureSession

CẬP NHẬT: Không có thông báo lỗi nào hiển thị. Tuy nhiên, tôi tìm thấy một mẫu số chung. Âm thanh sẽ ghi lại, nhưng chỉ khi ghi âm là 10 giây hoặc ngắn hơn. Nếu nó chạm 11 giây thì âm thanh sẽ không ghi.

NSLog lãm

Finished with error: (null)

-(void)viewWillAppear:(BOOL)animated { 
    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]; 
    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]); 
    } 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

    session = [[AVCaptureSession alloc] init]; 
    [session beginConfiguration]; 
    session.sessionPreset = AVCaptureSessionPresetMedium; 
    self.navigationController.navigationBarHidden = YES; 

    NSError *error = nil; 

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


    AVCaptureDevice *device; 
    AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionBack; 
    // find the front facing camera 

    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 


    // get the input device 
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 




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

    NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"]; 
    NSString *editedfilename = [[@"ComeOnDown" 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:audioInput]; 
    [session addInput:deviceInput]; 
    [session addOutput:movieFileOutput]; 
    [session commitConfiguration]; 
    [session startRunning]; 




    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
    previewLayer.backgroundColor = [[UIColor blackColor] CGColor]; 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight; 

    CALayer *rootLayer = [vImagePreview layer]; 
    [rootLayer setMasksToBounds:YES]; 
    [previewLayer setFrame:[rootLayer bounds]]; 
    [rootLayer addSublayer:previewLayer]; 

    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 



    //session = nil; 
    if (error) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: 
            [NSString stringWithFormat:@"Failed with error %d", (int)[error code]] 
                  message:[error localizedDescription] 
                  delegate:nil 
                cancelButtonTitle:@"Dismiss" 
                otherButtonTitles:nil]; 
     [alertView show]; 

    } 
    [super viewWillAppear:YES]; 

} 

-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections { 

} 
-(void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ 
    NSLog(@"Finished with error: %@", error); 
} 
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error { 
    //finished 
    NSLog(@"Finished"); 
    NSString *proud = [[NSString alloc] initWithString:[outputFileURL path]]; 
    UISaveVideoAtPathToSavedPhotosAlbum(proud, self, @selector(video:didFinishSavingWithError: contextInfo:), (__bridge void *)(proud)); 
} 
+0

Tôi thực sự không thấy nhu cầu mỉa mai. Bạn gán 'audioInput' chưa bao giờ kiểm tra' error2'. Cho rằng 'audioInput' là nơi bạn đang gặp vấn đề không phải là một gợi ý hợp lý? – jaket

+0

Không có thông báo lỗi nào hiển thị. Tuy nhiên, tôi tìm thấy một mẫu số chung. Âm thanh sẽ ghi lại, nhưng chỉ khi ghi âm là 10 giây hoặc ngắn hơn. Nếu nó chạm 11 giây thì âm thanh sẽ không ghi. – user717452

Trả lời

7

Câu trả lời là movieFileOutput.movieFragmentInterval = kCMTimeInvalid; Rõ ràng nó được thiết lập đến 10 mặc định, và bất cứ điều gì sau đó, âm thanh sẽ không ghi lại. Tham chiếu từ AVCaptureSession audio doesn't work for long videos

Là dễ dàng để tìm thấy câu trả lời khi tôi đã tìm ra thời gian đã tham gia với vấn đề

+0

cảm ơn câu trả lời. nhưng điều này không có ý nghĩa gì cả. –

0
videoFileOutput.movieFragmentInterval = kCMTimeInvalid 

giải quyết này cho tôi.

Tuy nhiên, tôi vô tình đặt movieFragmentInterval sau khi gọi startRecordingToOutputFileURL. Một giờ đau đớn sau đó tôi nhận ra sai lầm của mình. Đối với những người mới như tôi - hãy lưu ý TUYỆT VỜI TRƯỚC NÀY.

videoFileOutput.movieFragmentInterval = kCMTimeInvalid 
videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate) 
Các vấn đề liên quan