c# - AVAudioRecorder 始终为空

标签 c# ios xamarin xamarin.forms

我一直在尝试以 xamarin 形式录制音频,但没有直接的方法,所以我试图通过自定义渲染来实现它,我遵循了 here 中的示例.

//AVAudioRecorder创建

        //set up the NSObject Array of values that will be combined with the keys to make the NSDictionary
        NSObject[] values = new NSObject[]
        {
            NSNumber.FromFloat (16000.0f), //Sample Rate
            NSNumber.FromInt32 ((int)AudioToolbox.AudioFormatType.MPEG4AAC), //AVFormat
            NSNumber.FromInt32 (1), //Channels
            NSNumber.FromInt32 (16), //PCMBitDepth  
            NSNumber.FromBoolean (false), //IsBigEndianKey
            NSNumber.FromBoolean (false) //IsFloatKey
        };

        //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary
        NSObject[] keys = new NSObject[]
        {
            AVAudioSettings.AVSampleRateKey,
            AVAudioSettings.AVFormatIDKey,
            AVAudioSettings.AVNumberOfChannelsKey,
            AVAudioSettings.AVLinearPCMBitDepthKey,
            AVAudioSettings.AVLinearPCMIsBigEndianKey,
            AVAudioSettings.AVLinearPCMIsFloatKey
        };

        //Set Settings with the Values and Keys to create the NSDictionary
        settings = NSDictionary.FromObjectsAndKeys (values, keys);

        //Set recorder parameters
        recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);

但是在代码中"recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);"总是空的

如有任何帮助,我们将不胜感激。

最佳答案

我自己修复了它,通过如下更改音频设置使其工作

    `var settings = new AudioSettings
        {
            SampleRate = 44100.0f,
            Format = AudioFormatType.LinearPCM,
            NumberChannels = 1,
            LinearPcmBitDepth = 16,
            AudioQuality = AVAudioQuality.High,
        };`

关于c# - AVAudioRecorder 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31764817/

相关文章:

c# - 无法从远程机器获取元数据,但本地没问题

c# - 检查未读邮件

ios - 将.ipa从发行版转移到开发者资料

ios - 尝试在演示过程中显示 UIViewController!-警告

xamarin - ZXingBarcodeImageView(QR 代码)在 iOS 上显示,但在 Android 上不显示(Xamarin.Forms)

c# - 如何编写PDF编辑器?

iphone - 如何将 View 添加到页面控件

android - 如何授予使用 usb 管理器打开 usb 设备的权限? openDevice() 总是返回 null

尝试从相机添加附件时 iOS 11 应用程序崩溃

c# - 当客户端脚本显示时正确调整 ModalPopupExtender 的大小