iphone - Using AVCaptureSession and AVAudioPlayer together -
i able record video , output movie file correctly. however, have problem video recording (no video ouput) when trying use avaudioplayer play audio. mean cannot use avcapturesession , avaudioplayer @ same time? here code create capture session , play audio. video capture started first, during capturing, want play audio. help.
code create capture session record video (with audio) - output .mov file:
- (void)addvideoinput avcapturedevice *audiodevice = [avcapturedevice defaultdevicewithmediatype:avmediatypeaudio]; //... code setting videodevice/frontcamera nserror *error; avcapturedeviceinput *videoin = [avcapturedeviceinput deviceinputwithdevice:frontcamera error:&error]; avcapturedeviceinput *audioin = [avcapturedeviceinput deviceinputwithdevice:audiodevice error:&error]; if (!error) { if ([capturesession canaddinput:audioin]) [capturesession addinput:audioin]; else nslog(@"couldn't add audio input."); if ([capturesession canaddinput:videoin]) [capturesession addinput:videoin]; else nslog(@"couldn't add video input."); } - (void)addvideooutput { avcapturemoviefileoutput *m_capturefileoutput = [[avcapturemoviefileoutput alloc] init]; [capturesession addoutput:m_capturefileoutput]; [capturesession startrunning]; nsstring *docdir = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsmutablestring *filepath = [nsmutablestring stringwithstring:@"movie.mov"]; nsstring* fileroot = [docdir stringbyappendingpathcomponent:filepath]; nsurl *fileurl = [nsurl fileurlwithpath:fileroot]; avcaptureconnection *videoconnection = null; ( avcaptureconnection *connection in [m_capturefileoutput connections] ) { ( avcaptureinputport *port in [connection inputports] ) { if ( [[port mediatype] isequal:avmediatypevideo] ) { videoconnection = connection; } } } [videoconnection setvideoorientation:avcapturevideoorientationlandscaperight]; [m_capturefileoutput startrecordingtooutputfileurl:fileurl recordingdelegate:self]; [m_capturefileoutput release]; } code play audio, function call during video capture session. if don't call function, video recorded , able save .mov file. however, if call function, there's no output .mov file.
- (void)playaudio { nsstring *soundfilepath = [[nsbundle mainbundle] pathforresource:@"audiofile" oftype:@"mp3"]; nsurl *fileurl = [[nsurl alloc] initfileurlwithpath:soundfilepath]; avaudioplayer *newplayer = [[avaudioplayer alloc] initwithcontentsofurl:fileurl error:nil]; [fileurl release]; self.audioplayer = newplayer; [newplayer release]; [audioplayer setdelegate:self]; [audioplayer preparetoplay]; [audioplayer play]; }
i fixed problem setting audio session. called following function before creating audio player object play audio. way, able record video (with audio) , play audio @ same time.
- (void)setupaudiosession { static bool audiosessionsetup = no; if (audiosessionsetup) { return; } [[avaudiosession sharedinstance] setcategory: avaudiosessioncategoryplayback error: nil]; uint32 dosetproperty = 1; audiosessionsetproperty (kaudiosessionproperty_overridecategorymixwithothers, sizeof(dosetproperty), &dosetproperty); [[avaudiosession sharedinstance] setactive: yes error: nil]; audiosessionsetup = yes; } - (void)playaudio { [self setupaudiosession]; nsstring *soundfilepath = [[nsbundle mainbundle] pathforresource:@"audiofile" oftype:@"mp3"]; nsurl *fileurl = [[nsurl alloc] initfileurlwithpath:soundfilepath]; avaudioplayer *newplayer = [[avaudioplayer alloc] initwithcontentsofurl:fileurl error:nil]; [fileurl release]; self.audioplayer = newplayer; [newplayer release]; [audioplayer setdelegate:self]; [audioplayer preparetoplay]; [audioplayer play]; }
Comments
Post a Comment