gpt4 book ai didi

ios - 录制视频/音频时播放系统声音

转载 作者:可可西里 更新时间:2023-11-01 05:13:28 25 4
gpt4 key购买 nike

当我开始录制视频时,我正在尝试播放苹果要求的“哔”声。我通过 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);
}

这是我设置录制 session 的代码:

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

无论我尝试什么,在开始捕获 session 后我都无法播放声音。有人在 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(),您可以尝试只调用一次。

关于ios - 录制视频/音频时播放系统声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16309802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com