录制video/audio时播放系统声音

当我开始录制video时,我正尝试播放苹果所需的“哔哔”声。 我已经通过SO和其他来源发现,当audioinput没有进行某些configuration时,您将无法播放声音。

这是我的configuration方法的尝试:

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

这里是我设置logging会话的代码:

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

这就是我在哈林摇摆中使用的东西:

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

然后把它关掉,我这样做:

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

这使我可以在video捕捉过程中用AVAudioPlayer播放“哈林摇摆”。 它也覆盖了iDevice上的无声开关。 我不知道AudioSession.Initialize()是否需要在这两个部分,你可能会玩,只是调用一次。

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

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

之前,我试图使用sound.PlaySystemSound(),它不工作。 切换到AVAudioPlayer允许播放声音。 至less我学到了一堆关于iPhoneaudio内部的东西!

嗨,这里的开发人员是以上问题的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];