[英]Play system sound while recording video/audio
當我開始錄制視頻時,我正試圖播放蘋果要求的“嘟嘟”聲。 我發現通過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中解決了這個問題嗎?
這就是我在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()
,你可能只需要調用一次。
昨天整天把頭發拉了出來后,我開始工作了。
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音頻內部的知識!
嗨其他開發人員在這里是針對上述問題的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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.