2013-02-20 8 views
6

Chcę odtwarzać plik audio podczas nagrywania przy użyciu aparatu. Użyłem AVAudioPlayer do odtwarzania dźwięku i AVCamCaptureManager do nagrywania.Odtwarzanie pliku audio podczas nagrywania wideo w iOS

Jednak po rozpoczęciu odtwarzania dźwięku ekran podglądu jest zamrożony. Co powinienem zrobić?

Dzięki za pomoc. Oto kod. (Pracuję nad przykładem AVCam.)

To jest część podglądu.

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];   
     } 

I to jest część audio.

Jeśli używam AudioServicesPlaySystemSound normalne bez zamrażania, ale tym razem działa tylko na viewDidLoad.

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

Czy możesz napisać swój kod? – Robert

+0

Wszelkie aktualizacje w tej sprawie? Zastanawiam się, czy możesz włączyć kamerę (niekoniecznie nagrywając wideo) podczas nagrywania dźwięku w AVAudioPlayer. –

+0

Mam też ten sam problem. Co mam teraz zrobić? –

Odpowiedz

0

Nadzieja ustawienie AVAudioSession kategorii na AVAudioSessionCategoryPlayAndRecord rozwiąże problem. A także ustaw opcje kategorii na AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers. Sprawdź poniższy zestaw kodu:

@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];