【问题标题】:Play system sound while recording video/audio录制视频/音频时播放系统声音
【发布时间】:2013-04-30 22:10:39
【问题描述】:

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

【问题讨论】:

    标签: ios xamarin.ios xamarin avcapturesession avaudiosession


    【解决方案1】:

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

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

    然后要关闭它,我这样做:

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

    这让我可以在视频拍摄期间使用AVAudioPlayer 播放“哈林摇晃”。它还覆盖了 iDevice 上的静音开关。我不知道这两个部分是否都需要AudioSession.Initialize(),你可以随便调用一次。

    【讨论】:

    • 感谢您的回复。我已经找到了解决方案,即将发布。
    • @jonathanpeppers 通话中断怎么样....捕获卡在通话中断中
    • 当应用程序处于后台或可能停用时,我会使用上面的代码在您的AppDelegate 中将其关闭。
    【解决方案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 音频内部的知识!

    【讨论】:

      【解决方案3】:

      各位开发者,这里是上述问题的 Objective 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];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 2023-04-03
        相关资源
        最近更新 更多