2011-12-15 4 views
0

AVfoudnationを使用してオーディオ/ビデオを記録しています。ビデオ/オーディオのキャプチャを開始する前に、システムサウンドを使用してサウンドを再生する必要があります。これは最初は正しく動作していますが、2回目にしようとすると、システムのaudiは再生されません。私の推測では、AVfoundationの何かが正しくリリースされていないということです。私のアプリケーションdeletageでオーディオ/ビデオのキャプチャ後にシステムサウンドを再生できない

、私はapplicationDidFinishLaunchingメソッドでこのコードを持って:あなたは、私は素晴らしい作品VKRSAppSoundPlayerを、使用している見ることができるように

VKRSAppSoundPlayer *aPlayer = [[VKRSAppSoundPlayer alloc] init]; 
[aPlayer addSoundWithFilename:@"sound1" andExtension:@"caf"]; 
self.appSoundPlayer = aPlayer; 
[aPlayer release]; 

もこの方法

- (void)playSound:(NSString *)sound 
{ 
    [appSoundPlayer playSound:sound]; 
} 

!ビューで

、私はこのコードを持っている:私は[videoInputリリース]を行う場合は

- (void) startSession 
{ 
    self.session = [[AVCaptureSession alloc] init]; 

    [session beginConfiguration]; 
    if([session canSetSessionPreset:AVCaptureSessionPreset640x480]) 
     session.sessionPreset = AVCaptureSessionPresetMedium; 

    [session commitConfiguration]; 


    CALayer *viewLayer = [videoPreviewView layer]; 

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 

    captureVideoPreviewLayer.frame = viewLayer.bounds; 

    [viewLayer addSublayer:captureVideoPreviewLayer]; 

    self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:[self frontFacingCameraIfAvailable] error:nil]; 
    self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:[self audioDevice] error:nil]; 

    if(videoInput){ 
     self.videoOutput = [[AVCaptureMovieFileOutput alloc] init]; 

     [session addOutput:videoOutput]; 
     //[videoOutput release]; 

     if([session canAddInput:videoInput]){ 
      //[session beginConfiguration]; 
      [session addInput:videoInput]; 

     } 
     //[videoInput release]; 

     [session removeInput:[self audioInput]]; 
     if([session canAddInput:audioInput]){ 
      [session addInput:audioInput]; 
     } 
     //[audioInput release]; 


     if([session canAddInput:audioInput]) 
      [session addInput:audioInput]; 


     NSLog(@"startRunning!"); 
     [session startRunning]; 

     [self startRecording]; 


     if(![self recordsVideo]) 
      [self showAlertWithTitle:@"Video Recording Unavailable" msg:@"This device can't record video."]; 

    } 
} 

- (void) stopSession 
{ 
    [session stopRunning]; 
    [session release]; 
} 


- (AVCaptureDevice *)frontFacingCameraIfAvailable 
{ 
    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 
    AVCaptureDevice *captureDevice = nil; 

    Boolean cameraFound = false; 

    for (AVCaptureDevice *device in videoDevices) 
    { 
     NSLog(@"1 frontFacingCameraIfAvailable %d", device.position); 
     if (device.position == AVCaptureDevicePositionBack){ 
      NSLog(@"1 frontFacingCameraIfAvailable FOUND"); 

      captureDevice = device; 
      cameraFound = true; 
      break; 
     } 
    } 

    if(cameraFound == false){ 
     for (AVCaptureDevice *device in videoDevices) 
     { 
      NSLog(@"2 frontFacingCameraIfAvailable %d", device.position); 
      if (device.position == AVCaptureDevicePositionFront){ 
       NSLog(@"2 frontFacingCameraIfAvailable FOUND"); 

       captureDevice = device; 
       break; 
      } 
     } 
    } 

    return captureDevice; 
} 

- (AVCaptureDevice *) audioDevice 
{ 
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio]; 
    if ([devices count] > 0) { 
     return [devices objectAtIndex:0]; 
    } 
    return nil; 
} 

- (void) startRecording 
{ 
#if _Multitasking_ 
    if ([[UIDevice currentDevice] isMultitaskingSupported]) { 
     [self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}]]; 
    } 
#endif 

    [videoOutput startRecordingToOutputFileURL:[self generatenewVideoPath] 
          recordingDelegate:self]; 
} 

- (void) stopRecording 
{ 
    [videoOutput stopRecording]; 

} 

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput 
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL 
     fromConnections:(NSArray *)connections error:(NSError *)error 
{ 
    NSFileManager *man = [[NSFileManager alloc] init]; 
    NSDictionary *attrs = [man attributesOfItemAtPath: [outputFileURL path] error: NULL]; 
    NSString *fileSize = [NSString stringWithFormat:@"%llu", [attrs fileSize]]; 


    // close this screen 
    [self exitScreen]; 
} 

-(BOOL)recordsVideo 
{ 
    AVCaptureConnection *videoConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo 
                    fromConnections:[videoOutput connections]]; 
    return [videoConnection isActive]; 
} 

-(BOOL)recordsAudio 
{ 
    AVCaptureConnection *audioConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeAudio 
                    fromConnections:[videoOutput connections]]; 
    return [audioConnection isActive]; 
} 

を。および[audioInput release];悪いアクセスエラーが発生しました。だから彼らはコメントされている。これは問題の一部である可能性があります。

システムサウンドをn回再生しようとするとうまくいきますが、最初に録音スクリプトに行くとそれ以降は動作しません。

アイデア?

+0

あなたはself.iVarが何を意味し、どのようなリリースを意味するのかをよく理解する必要があります。リリースでは保持カウントがデクリメントされます。0の場合、オブジェクトの割り当てが解除されます。 self.iVarを使用すると(それをプロパティとして宣言したと仮定して)、iVarは保持されます。その直後に解放することができます。しかし、私はそれがあなたのオーディオ問題だとは思わない。 – Rayfleck

答えて

0

AVCaptureSessionを解放する適切な方法は以下の通りです:

- (void) destroySession { 

    // Notify the view that the session will end 
    if ([delegate respondsToSelector:@selector(captureManagerSessionWillEnd:)]) { 
     [delegate captureManagerSessionWillEnd:self]; 
    } 

    // remove the device inputs 
    [session removeInput:[self videoInput]]; 
    [session removeInput:[self audioInput]]; 

    // release 
    [session release]; 

    // remove AVCamRecorder 
    [recorder release]; 

    // Notify the view that the session has ended 
    if ([delegate respondsToSelector:@selector(captureManagerSessionEnded:)]) { 
     [delegate captureManagerSessionEnded:self]; 
    } 
} 

あなたはリリースの問題のいくつかの並べ替え(悪いアクセス)を抱えている場合は、私はあなたの現在の「汚い」プロジェクトのうち、あなたのコードを取ってお勧めすることができます他の新しいプロジェクトに変更し、そこの問題をデバッグします。

私が似たような問題を抱えていた時、私はそれをしました。私はGithubでこれを共有しました。このプロジェクトは役に立ちました:AVCam-CameraReleaseTest

関連する問題