Play system sound while recording video/audio

孤街浪徒 提交于 2019-12-03 16:58:23

This is what I use in The Harlem Shake:

AudioSession.Initialize();
AudioSession.Category = AudioSessionCategory.MediaPlayback;
AudioSession.OverrideCategoryMixWithOthers = true;

Then to turn it off, I do this:

AudioSession.Initialize();
AudioSession.Category = AudioSessionCategory.AmbientSound;
AudioSession.OverrideCategoryMixWithOthers = false;

This allows me to play "the Harlem Shake" with AVAudioPlayer during video capture. It also overrides the silent switch on the iDevice. I don't know if AudioSession.Initialize() is required on both parts, you might play around with just calling it once.

After pulling my hair out all day yesterday, I got this working.

private void PlayBeepSound()
    {
        NSError err;
        var player = AVAudioPlayer.FromUrl(NSUrl
            .FromFilename("Sounds/video_record/video_beep.wav"), out err);
        player.Play();
    }

Before, I was attempting to use sound.PlaySystemSound(), which wasn't working. Switching to AVAudioPlayer allowed the sound to play. At least I learned a bunch about iPhone audio internals!

Hi fellow developers here is the Objective C code for above problem,

@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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!