2011-01-17 13 views
5

Ho scritto un'app per registrare video da iPhone. Funziona bene ma ha un grosso problema. All'avvio di AVCaptureSession, l'utente tenta di riprodurre l'audio dalla libreria (iPod). Questa azione farà terminare AVCaptureSession. Qualche idea può impedire all'utente di provare a riprodurre l'audio o risolvere questo problema?L'audio farà terminare AVCaptureSession


questo è il mio codice:

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

Avete mai trovato una soluzione per questo? – Sandy

+0

Hai mai trovato una soluzione @anistar? –

+0

Sfortunatamente mi sono imbattuto nello stesso problema - qualche idea? – nixau

risposta

0

È necessario utilizzare il NSNotificationCenter. Utilizzare il codice seguente (ho incluso anche altri metodi utili) e scrivere un metodo per AVCaptureSessionWasInterruptedNotification che gestirà l'interruzione della cattura, con qualsiasi mezzo. Spero che aiuti.

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

Ma che tipo di cose dovresti interrompere o salvare su un'interruzione per assicurarti che continui a funzionare? – Alper

0

Provare a fare scherzi con la sessione audio!

Ecco un rapido indovinare su cosa si potrebbe fare, ma non ho provato questo con l'iPod in particolare:

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"); 
} 

Ho anche avuto un po 'di fortuna con l'ambiente acustico categoria audio (anche se dà un errore, sembra consentire la riproduzione audio durante la registrazione video):

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

Questo ha funzionato per me:

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

}

+0

come hai aggiunto questo alla tua avcapturesession, puoi fornire più codice? –

+0

Mi scuso, sono passati diversi anni da quando ho dovuto guardare a questo e non posso fornire alcun reale ulteriore contesto di ciò che è qui. – micahp

Problemi correlati