Search code examples
c#audioaudio-recordingraspberry-pi3windows-iot-core-10

Windows IoT Raspberry Pi 3 c# Recording Audio


I am trying to record audio on my Rasp Pi 3 running Windows 10 IoT. I am recording audio to store in USB drive. Can anyone help to advise what did I do wrong. Thanks in advance.

 private async void RecordBtn_Checked(object sender, RoutedEventArgs e)
    {
        //init mediacapture
        audioCapture = new MediaCapture();
        await audioCapture.InitializeAsync();

        StorageFolder externalDevices = KnownFolders.RemovableDevices;
        IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
        StorageFolder usbStorage = externalDrives[0];

        var recordFolder = await usbStorage.CreateFolderAsync("Recording");

        StorageFile recordFile = await recordFolder.CreateFileAsync("record.mp3", Windows.Storage.CreationCollisionOption.GenerateUniqueName);

        audioRecording = await audioCapture.PrepareLowLagRecordToStorageFileAsync(MediaEncodingProfile.CreateMp3(AudioEncodingQuality.High), recordFile);

        await audioRecording.StartAsync();

        isRecording = true;
        RecordStatus.Text = "Recording ... ";

        //PlayRec.IsEnabled = StopRec.IsEnabled = false;


    }

 private async void RecordBtn_Unchecked(object sender, RoutedEventArgs e)
    {
        if (isRecording)
        {
            await audioRecording.StopAsync();
            isRecording = false;

            await audioRecording.FinishAsync();
            RecordStatus.Text = "Recording stopped.";

            //PlayRec.IsEnabled = StopRec.IsEnabled = true;
        }

    }

Solution

  • If you want to capture audio only, try the following:

    audioCapture = new MediaCapture();  
    var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();  
    settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio;  
    settings.MediaCategory = Windows.Media.Capture.MediaCategory.Other;  
    settings.AudioProcessing = Windows.Media.AudioProcessing.Default;  
    await audioCapture.InitializeAsync(settings);  
    

    Also make sure you have set the right capabilities in your Package.appxmanifest file:

    <Capabilities>      
       <DeviceCapability Name="microphone" />  
    </Capabilities>  
    

    Check this tutorial out, it has some good examples:

    https://learn.microsoft.com/en-us/samples/microsoft/windows-iotcore-samples/webcam-app/