2011-01-17 15 views
5

Napisałem aplikację do nagrywania wideo z iPhone'a. Działa dobrze, ale ma jeden duży problem. Gdy AVCaptureSession zacznie działać, a użytkownik spróbuje odtwarzać dźwięk z ich biblioteki (iPod). To działanie spowoduje zakończenie sesji AVCaptureSession. Jakiś pomysł może przeszkodzić użytkownikowi w próbie odtwarzania dźwięku lub rozwiązaniu tego problemu?Audio spowoduje zakończenie sesji AVCaptureSession


to mój kod:

videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];   
audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 

AVCaptureDeviceInput *videoDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoDevice error:nil]; 
AVCaptureDeviceInput *audioDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil]; 

movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 

captureSession = [[AVCaptureSession alloc] init]; 

[captureSession beginConfiguration]; 
[captureSession setSessionPreset:AVCaptureSessionPresetHigh]; 
[captureSession addInput:videoDeviceInput]; 
[captureSession addInput:audioDeviceInput]; 
[captureSession addOutput:movieFileOutput]; 
[captureSession commitConfiguration]; 

[captureSession startRunning]; 
+0

Czy kiedykolwiek znalazłeś rozwiązanie tego problemu? – Sandy

+0

Czy kiedykolwiek znalazłeś rozwiązanie @anistar? –

+0

Niestety natknąłem się na ten sam problem - jakieś pomysły? – nixau

Odpowiedz

0

Musisz użyć NSNotificationCenter. Użyj poniższego kodu (dodałem także inne przydatne metody) i napisz metodę dla AVCaptureSessionWasInterruptedNotification, która będzie obsługiwać przerwanie przechwytywania, w dowolny sposób. Mam nadzieję że to pomogło.

NSNotificationCenter *notify = [NSNotificationCenter defaultCenter]; 
[notify addObserver: self selector: @selector(onVideoError:) name: AVCaptureSessionRuntimeErrorNotification object: captureSession]; 
[notify addObserver: self selector: @selector(onVideoInterrupted:) name: AVCaptureSessionWasInterruptedNotification object: captureSession]; 
[notify addObserver: self selector: @selector(onVideoEnded:) name: AVCaptureSessionInterruptionEndedNotification object: captureSession]; 
[notify addObserver: self selector: @selector(onVideoDidStopRunning:) name: AVCaptureSessionDidStopRunningNotification object: captureSession]; 
[notify addObserver: self selector: @selector(onVideoStart:) name: AVCaptureSessionDidStartRunningNotification object: captureSession]; 
+1

Ale jakie rzeczy należy zatrzymać lub zaoszczędzić na przerwach, aby upewnić się, że nadal działa? – Alper

0

Spróbuj poradzić sobie z sesją audio!

Oto krótki przypuszczenie o tym, co można zrobić, ale nie próbowałem tego z iPoda szczególności:

OSStatus status = noErr; 
status |= AudioSessionInitialize(CFRunLoopGetMain(), kCFRunLoopCommonModes, PVAudioSessionInterruptionListener, NULL); 

    status |= AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &(UInt32){kAudioSessionCategory_PlayAndRecord}); 

    status |= AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, 
             sizeof(UInt32), 
             &(UInt32){true}); 

    status |= AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, 
             sizeof(UInt32), 
             &(UInt32){false}); 

status |= AudioSessionSetActive(YES); 

if (status != noErr) { 
    NSLog(@"ERROR: There was an error in setting the audio session"); 
} 

miałem też trochę szczęścia z otoczeniem brzmieć kategorię dźwięku (chociaż to daje błąd, wydaje się, aby umożliwić odtwarzanie dźwięku podczas nagrywania filmu):

status |= AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &(UInt32){kAudioSessionCategory_AmbientSound}); 
1

Ten pracował dla mnie:

- (void)setupAudio { 
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil]; 
    UInt32 doSetProperty = 1; 
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty); 
    [[AVAudioSession sharedInstance] setActive: YES error: nil]; 

}

+0

W jaki sposób dodałeś to do swojej avcapturesession, czy możesz podać więcej kodu? –

+0

Przepraszam, minęło wiele lat, odkąd musiałem spojrzeć na to i nie mogę podać żadnego prawdziwego kontekstu, a następnie tego, co jest tutaj. – micahp

Powiązane problemy