播放系统声音的同时录制视频/音频(Play system sound while recording

2019-09-01 14:02发布

我试图发挥所要求的苹果,当我开始录制视频“嘟”的声音。 我已经经历,当你有一个音频输入会没有一些配置,你不能播放声音SO和其他来源找到。

这里是我的配置方法的尝试:

private void SetupAudio()
    {
        beepSound = AssetBank.GetSystemSoundWav("video_record", "video_beep");
        AudioSession.Initialize();
        AudioSession.Interrupted += delegate
        {
            Console.WriteLine("Interrupted handler");
        };

        AudioSession.Category = AudioSessionCategory.PlayAndRecord;
        AudioSession.OverrideCategoryMixWithOthers = true;

        NSError err;
        AVAudioSession.SharedInstance().SetActive(true, out err);
    }

这是我的代码,我设置记录会话:

public void SetupVideoCaptureSession(AVCaptureDevicePosition position)
    {

        // Setup devices
        foreach (var device in AVCaptureDevice.Devices)
        {
            if (device.HasMediaType(AVMediaType.Video))
            {
                if (device.Position == AVCaptureDevicePosition.Front)
                {
                    frontCam = device;
                } else if (device.Position == AVCaptureDevicePosition.Back)
                {
                    backCam = device;
                }
            }
        }

        // Create capture session
        captureSession = new AVCaptureSession();
        captureSession.BeginConfiguration();
        captureSession.SessionPreset = AVCaptureSession.Preset640x480;
        // Create capture device

        switch (position)
        {
            case AVCaptureDevicePosition.Back:
                videoDevice = backCam;
                break;

            case AVCaptureDevicePosition.Front:
                videoDevice = frontCam;
                break;
        }

        if (null == videoDevice)
        {
            using (var alert = new UIAlertView { Message = "No camera detected!" })
            {
                alert.AddButton("Okay!");
                alert.Show();
            }
            return;
        }

        audioDevice = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio);

        // Create capture device input
        NSError videoError, audioError;
        videoDeviceInput = new AVCaptureDeviceInput(videoDevice, out videoError);
        audioDeviceInput = new AVCaptureDeviceInput(audioDevice, out audioError);

        captureSession.AddInput(videoDeviceInput);
        captureSession.AddInput(audioDeviceInput);

        // Create capture device output
        videoOutput = new AVCaptureMovieFileOutput();
        videoOutput.MaxRecordedDuration = new CMTime(10, 1);

        captureSession.AddOutput(videoOutput);

        if (null != previewLayer)
            previewLayer.RemoveFromSuperLayer();

        // Create preview layer
        previewLayer = AVCaptureVideoPreviewLayer.FromSession(captureSession);
        previewLayer.Orientation = AVCaptureVideoOrientation.Portrait;
        previewLayer.VideoGravity = "AVLayerVideoGravityResizeAspectFill";
        previewLayer.Frame = new RectangleF(new PointF(), ScreenSize);

        this.ParentScrollView.Layer.InsertSublayer(previewLayer, 0);

        // Start capture session

        SetupAudio();
        captureSession.CommitConfiguration();
        captureSession.StartRunning();
    }

不管我怎么努力,我不能让一个声音,我已经开始捕获会话后玩。 任何人在MonoTouch的解决了这个?

Answer 1:

这是我在使用哈林摇 :

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

然后将其关闭,我这样做:

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

这让我玩“哈莱姆摇”与AVAudioPlayer在视频捕捉。 它也覆盖上的iDevice的无声开关。 我不知道是否AudioSession.Initialize()需要在两个部分,你可能会玩弄只是调用它一次。



Answer 2:

拉我的头发昨天一整天后,我得到了这个工作。

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

之前,我尝试使用sound.PlaySystemSound(),这是行不通的。 切换到AVAudioPlayer允许播放声音。 至少我学到了一堆关于iPhone的音频内部!



Answer 3:

喜的开发伙伴们这里是上述问题的目标C代码,

@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];


文章来源: Play system sound while recording video/audio