以下代码使用 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 框架从麦克风录音(似乎微软在将数据写入文件时在该框架中出现错误!)
看看这段代码Microphone Sample和这篇以 wav 格式保存数据的文章 Saving Microphone stream to wave format in Windows Phone来自诺基亚开发者门户网站!
关于c# - 使用 AudioVideoCaptureDevice 在 Windows Phone 8 上录制 PCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14661851/