I want to play audio file while recording with camera. I used AVAudioPlayer for playing audio and AVCamCaptureManager for recording.
我想在用相机录音的同时播放音频文件。我用AVAudioPlayer播放音频,用AVCamCaptureManager录音。
But when audio starts to play, preview screen is freezing. What should i do?
但是当音频开始播放时,预览屏幕就会冻结。我应该做什么?
Thanks for your helping. Here is the code. (I'm working on AVCam example.)
谢谢你的帮助。这是代码。(我正在研究AVCam示例。)
This is preview part.
这是预览部分。
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];
}
And this is audio play part.
这是音频播放部分。
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", sound] ofType:@"wav"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];
If I use AudioServicesPlaySystemSound normal without freezing but this time it only works on viewDidLoad.
如果我使用AudioServicesPlaySystemSound正常而没有冻结,但是这次它只在viewDidLoad上工作。
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileRef;
soundFileRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) CFBridgingRetain(@"sound"), CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileRef, &soundID);
AudioServicesPlaySystemSound(soundID);
2 个解决方案
#1
1
My problem wasn't for video but it was for playing a sound while in a call (voip type application) and the answer to this question worked for me:
我的问题不是视频,而是在电话中播放声音(voip类型应用程序),这个问题的答案为我工作:
Using vibrate and AVCaptureSession at the same time
同时使用振动和AVCaptureSession
#2
0
Hope setting AVAudioSession
category to AVAudioSessionCategoryPlayAndRecord
will solve the problem. And also set its category options to AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers
. Please check the below set of code
希望通过设置AVAudioSession category来解决这个问题。并将其类别选项设置为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];
#1
1
My problem wasn't for video but it was for playing a sound while in a call (voip type application) and the answer to this question worked for me:
我的问题不是视频,而是在电话中播放声音(voip类型应用程序),这个问题的答案为我工作:
Using vibrate and AVCaptureSession at the same time
同时使用振动和AVCaptureSession
#2
0
Hope setting AVAudioSession
category to AVAudioSessionCategoryPlayAndRecord
will solve the problem. And also set its category options to AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers
. Please check the below set of code
希望通过设置AVAudioSession category来解决这个问题。并将其类别选项设置为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];