2013-04-30 180 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];