使用 AudioVideoCaptureDevice 在 Windows Phone 8 上录制 PCM
本文关键字:PCM Phone AudioVideoCaptureDevice Windows 使用 | 更新日期: 2023-09-27 17:57:14
以下代码使用 AMR 和 AAC 格式正确地将从我的 Windows Phone 8 设备录制的音频记录到独立存储中。但是,当我尝试记录PCM格式时,应用程序崩溃并显示错误:
Value does not fall within the expected range.
Windows.Phone.Media.Capture.AudioVideoCaptureDevice.StartRecordingToStreamAsync(IRandomAccessStream stream)
at WP8.Classes.AudioCaptureController.<StartRecordingAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at WP8.ViewModels.CameraPageViewModel.<capture>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at WP8.ViewModels.CameraPageViewModel.<TakePictureAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(Object state)
我工作的代码如下:
private AudioVideoCaptureDevice mic;
private IRandomAccessStream sst;
private string path;
public async Task MicStartAsync()
{
mic = await AudioVideoCaptureDevice.OpenForAudioOnlyAsync();
mic.AudioEncodingFormat = CameraCaptureAudioFormat.Pcm;
}
public async Task StartRecordingAsync()
{
IsolatedStorageAccess iso = new IsolatedStorageAccess();
sst = await iso.SetUpFileStreamForSavingAudioAsync("123.pcm");
await mic.StartRecordingToStreamAsync(sst);
path = iso.Path;
}
public async Task StopRecordingAsync()
{
await mic.StopRecordingAsync();
sst.AsStream().Dispose();
new MediaPlayerLauncher()
{
Media = new Uri(path, UriKind.Relative)
}.Show();
}
}
}
将格式更改为PCM以外的任何格式都可以工作!
更新:由于某种原因,它现在似乎避免了崩溃,但这显示在输出中:
A first chance exception of type 'System.ArgumentException' occurred in WP8.DLL
An exception of type 'System.ArgumentException' occurred in WP8.DLL and wasn't handled before a managed/native boundary
(WP8是我的dll的名字)在此之后,代码照常进行,并且仅在我停止录制时崩溃,它说录制从未开始并且AudioVideoCaptureDevice处于不正确的状态
我还尝试使用var AudioVideoCaptureDevice.SupportedAudioEncodingFormats;
来检查PCM是否可用
您可以使用XNA框架从麦克风录制(似乎微软在将数据写入文件时框架中存在错误!
看看这个代码麦克风示例和这篇文章,用于将数据保存为 wav 格式 将麦克风流保存为 Windows Phone 中的波形格式 从诺基亚开发人员门户!