"using avcapturesession and avaudioplayer together" Code Answer

2

i fixed the problem by setting up the audio session. i called the following function before creating the audio player object to play the audio. that way, i was able to record video (with audio) and play audio at the 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];
    }
By Adriaan Nel on March 20 2022

Answers related to “using avcapturesession and avaudioplayer together”

Only authorized users can answer the Search term. Please sign in first, or register a free account.