录制视频/音频时播放系统声音

时间:2013-04-30 22:10:40

标签: ios xamarin.ios xamarin avcapturesession avaudiosession

当我开始录制视频时,我正试图播放苹果要求的“嘟嘟”声。我发现通过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中解决了这个问题吗?

3 个答案:

答案 0 :(得分:5)

这是我在The Harlem Shake中使用的内容:

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

然后关掉它,我这样做:

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

这允许我在视频捕捉期间使用AVAudioPlayer播放“Harlem Shake”。它还会覆盖iDevice上的静音开关。我不知道两个部分是否都需要AudioSession.Initialize(),你可能只需要调用一次就可以了。

答案 1 :(得分: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音频内部的知识!

答案 2 :(得分:1)

这里的开发人员是上述问题的目标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];