2013-04-30 179 views
4

当我开始录制视频时,我正尝试播放苹果所需的“嘟嘟”声。我已经通过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中解决了这个问题?

回答

5

这是我在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(),您可以只打一次就玩。

+0

感谢您的答复。我已经找到了一个解决方案,即将发布。 – 2013-05-01 14:07:01

+0

@jonathanpeppers关于通话中断怎么样....通话中断时捕获卡住 – 2016-02-18 04:58:35

+0

我应该使用上面的代码在AppDelegate中关闭应用程序后台或关闭应用程序。 – jonathanpeppers 2016-05-19 02:36:34

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音频内部的东西!

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