c# - 使用 AudioVideoCaptureDevice 在 Windows Phone 8 上录制 PCM

标签 c# windows-phone-8 windows-phone

以下代码使用 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/

相关文章:

c# - .csproj 文件中未通知时,Visual Studio 如何确定输出路径?

.net - 通用应用的主题资源

c# - 在 Windows Phone 8.1 RT 中将联系人添加到我的应用程序联系人商店后如何获取所有联系人列表?

jquery - Windows Phone 设备上 phonegap/cordova 上的选择标签问题

c# - Windows Phone 保持手势

c# - 为什么英语(加勒比海)的文化名称是 "en-029"?

c# - 分配给托管图像的非托管缓冲区的解决方案

c# - 将枚举作为字典集合中的键出现问题

c# - 在 Converter 中显示 StorageItem 缩略图

c# - 消息框导致 Windows Phone 8 的响应速度较差