录制视频/音频时播放系统声音 [英] Play system sound while recording video/audio

查看:165
本文介绍了录制视频/音频时播放系统声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始录制视频时,我正试图播放苹果要求的嘟嘟声。我已经通过SO和其他来源发现,当你的音频输入没有一些配置时你就无法发出声音。

I'm trying to play a "beep" sound as required by apple when I start recording a video. I've found through SO and other sources that you can't play a sound while you have an audio input going without some configuration.

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

Here's my attempt at the configuration method:

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);
    }

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

And here's my code where I setup the recording session:

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中解决了这个问题吗?

No matter what I try, I can't get a sound to play after I've started the capture session. Anyone solved this in MonoTouch?

推荐答案

这是我在 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;

这允许我用 AVAudioPlayer播放哈林摇动在视频捕获期间。它还会覆盖iDevice上的静音开关。我不知道两个部分是否需要 AudioSession.Initialize(),你可能只需要调用一次。

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.

这篇关于录制视频/音频时播放系统声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆