我想在使用相機錄製時播放音頻文件。我使用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);
你能發佈你的代碼嗎? – Robert 2013-02-20 12:22:20
這方面的更新?我想知道在用AVAudioPlayer錄製音頻時是否可以打開相機(不一定是錄製視頻)。 – 2013-03-05 13:16:13
我也有同樣的問題。我現在該怎麼做.. – 2014-03-25 05:48:08