以下代码使用 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 处于不正确的状态
我也尝试过使用 varAudioVideoCaptureDevice.SupportedAudioEncodingFormats;
检查 Pcm 是否可用