2013-02-20 193 views
6

我想在使用相機錄製時播放音頻文件。我使用AVAudioPlayer播放音頻和AVCamCaptureManager進行錄製。在iOS中錄製視頻時播放音頻文件

但是當音頻開始播放時,預覽屏幕凍結。我該怎麼辦?

感謝您的幫助。這是代碼。 (我正在研究AVCam示例。)

這是預覽部分。

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

這是音頻播放部分。

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

如果我使用AudioServicesPlaySystemSound無嚴寒正常的,但這次它只能在viewDidLoad中。

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

你能發佈你的代碼嗎? – Robert 2013-02-20 12:22:20

+0

這方面的更新?我想知道在用AVAudioPlayer錄製音頻時是否可以打開相機(不一定是錄製視頻)。 – 2013-03-05 13:16:13

+0

我也有同樣的問題。我現在該怎麼做.. – 2014-03-25 05:48:08

回答

0

希望將AVAudioSession類別設置爲AVAudioSessionCategoryPlayAndRecord將解決該問題。並且將其類別選項設置爲AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers。請檢查下面的代碼集

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