2013-02-20 18 views
6

Desidero riprodurre il file audio durante la registrazione con la fotocamera. Ho usato AVAudioPlayer per riprodurre audio e AVCamCaptureManager per la registrazione.Riproduzione di file audio durante la registrazione di video su iOS

Ma quando l'audio inizia a suonare, la schermata di anteprima si sta congelando. Cosa dovrei fare?

Grazie per il vostro aiuto. Ecco il codice. (Sto lavorando all'esempio di AVCam.)

Questa è la parte di anteprima.

if ([self captureManager] == nil) { 
     AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init]; 
     [self setCaptureManager:manager]; 

     [[self captureManager] setDelegate:self]; 

     if ([[self captureManager] setupSession]) {    
      AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]]; 
      UIView *view = [self videoPreviewView]; 
      CALayer *viewLayer = [view layer]; 
      [viewLayer setMasksToBounds:YES]; 

      CGRect bounds = CGRectMake(0, 0, 480, 320); 
      [newCaptureVideoPreviewLayer setFrame:bounds]; 

      if ([newCaptureVideoPreviewLayer isOrientationSupported]) { 
       [newCaptureVideoPreviewLayer setOrientation:[DeviceProperties setPreviewOrientation]]; 
      } 

      [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 

      [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]]; 

      [self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer]; 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       [[[self captureManager] session] startRunning]; 
      }); 

      [self updateButtonStates];   
     } 

E questo fa parte del gioco audio.

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", sound] ofType:@"wav"]]; 
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    [audioPlayer play]; 

Se uso AudioServicesPlaySystemSound normale, senza gelo ma questa volta funziona solo su viewDidLoad.

CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFURLRef soundFileRef; 
    soundFileRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) CFBridgingRetain(@"sound"), CFSTR ("wav"), NULL); 
    UInt32 soundID; 
    AudioServicesCreateSystemSoundID(soundFileRef, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
+0

Potresti pubblicare il tuo codice? – Robert

+0

Eventuali aggiornamenti su questo? Mi stavo chiedendo se è possibile attivare la fotocamera (non necessariamente registrare video) durante la registrazione audio con AVAudioPlayer. –

+0

ho anche lo stesso problema. Cosa faccio ora .. –

risposta

0

Speranza l'impostazione della categoria AVAudioSession a AVAudioSessionCategoryPlayAndRecord risolverà il problema. E imposta anche le sue opzioni di categoria su AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers. Si prega di controllare il seguente set di codice

@property (strong, nonatomic) AVAudioPlayer *audioPlayer; 

NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"VoicemailSound" ofType:@"mp3"]]; //Download and add a system sound to your project and mention its name and type here 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; 

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error: nil]; 

[self.audioPlayer prepareToPlay]; 
[self.audioPlayer play]; 
Problemi correlati