naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.47k stars 1.09k forks source link

Bug WasapiLoopbackCapture #1176

Open zdypll opened 1 month ago

zdypll commented 1 month ago

Bug1: System.TypeLoadException: Could not resolve type with token 0100001e from typeref (expected class 'System.OperatingSystem' in assembly 'System.Runtime.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') at NAudio.Wave.WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice () [0x00000] in <1b3f595e963c4b8ab8ee799323fffe28>:0 at NAudio.Wave.WasapiLoopbackCapture..ctor () [0x00000] in <1b3f595e963c4b8ab8ee799323fffe28>:0 at AudioCapture.Start () [0x00002] in D:\Live2D Editor\Assets\Script\AudioCapture.cs:32 UnityEngine.Debug:Log (object) AudioCapture:Start () (at Assets/Script/AudioCapture.cs:48)

c# Script

//NAudio.dll Save => Plugins

using NAudio.Wave; using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine;

public class AudioCapture : MonoBehaviour { public int bytesLength; public byte[] bytes;

//private void Microphone()
//{
//    WaveInEvent waveIn = new WaveInEvent();
//    waveIn.DataAvailable += (sender, e) =>
//    {
//        e.Buffer.ToList().CopyTo(0, bytes, 0, bytes.Length);
//    };
//    waveIn.WaveFormat = new WaveFormat(44100, 16, 1);
//    waveIn.StartRecording();
//}

private void Start()
{
    try
    {
        WasapiLoopbackCapture cap = new WasapiLoopbackCapture();
        cap.DataAvailable += (sender, e) =>
        {
            e.Buffer.ToList().CopyTo(0, bytes, 0, bytes.Length);

            //float[] listByte = Enumerable
            //.Range(0, e.BytesRecorded / 4)
            //.Select(i => BitConverter.ToSingle(e.Buffer))
            //.ToArray();
        };

        cap.StartRecording();

    }
    catch (Exception error)
    {
        Debug.Log(error);
    }
}

}

Bug2:WasapiLoopbackCapture cap = new WasapiLoopbackCapture();//Unity Debug Show dll in code is null error

1.An error occurred while installing NAudio on Nuget in Visual Studio, and the reference name could not be found. After exploring for a long time, the current solution is to copy the DLL file to the plugins special folder of the Unity project.

2.try-catch, code no run, But unity debug show Null error. DLL internal error, unable to catch exception

3.Permission issue, causing Unity to be unable to use NAudio and unable to use it. The Unity Hub and Unity Engine have been set to administrator privileges, but they are invalid

zdypll commented 1 month ago

NAudio.Core 2.2.1 NAudio.Wasapi 2.2.1

zdypll commented 1 month ago

I have been trying for a long time and have been researching NAudio for 3 days now, but I feel disappointed

zdypll commented 1 month ago

By the way, it is necessary to use a framework, even if Unity officially abandons this technology, and it must be used in lower versions of Unity 2022. If NAudio gives up, it means giving up Unity creativity

zdypll commented 1 month ago

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?