discord-net / Discord.Net

An unofficial .Net wrapper for the Discord API (https://discord.com/)
https://discordnet.dev
MIT License
3.34k stars 737 forks source link

BadImageFormatException in Audio #578

Closed hasezoey closed 7 years ago

hasezoey commented 7 years ago

System.BadImageFormatException: "An attempt was made to load a file with an incorrect format (exception of HRESULT: 0x8007000B)" i have opus.dll and libsodium.dll installed and copied the execption comes in var vClient = await client.GetService<AudioService>() .Join(VoiceChannel);

the whole code: `private async void SendAudioFF(string FilePath, string CurrentServer) { ConsoleSendMessage(CurrentServer, true); var VoiceChannel = client.FindServers(CurrentServer).FirstOrDefault().VoiceChannels.FirstOrDefault(); var vClient = await client.GetService() .Join(VoiceChannel);

        var process = Process.Start(new ProcessStartInfo
        { // FFmpeg requires us to spawn a process and hook into its stdout, so we will create a Process
            FileName = "ffmpeg",
            Arguments = $"-i {FilePath} " + // Here we provide a list of arguments to feed into FFmpeg. -i means the location of the file/URL it will read from
                    "-f s16le -ar 48000 -ac 2 pipe:1", // Next, we tell it to output 16-bit 48000Hz PCM, over 2 channels, to stdout.
            UseShellExecute = false,
            RedirectStandardOutput = true // Capture the stdout of the process
        });
        Thread.Sleep(2000); // Sleep for a few seconds to FFmpeg can start processing data.

        int blockSize = 3840; // The size of bytes to read per frame; 1920 for mono
        byte[] buffer = new byte[blockSize];
        int byteCount;

        while (true) // Loop forever, so data will always be read
        {
            byteCount = process.StandardOutput.BaseStream // Access the underlying MemoryStream from the stdout of FFmpeg
                    .Read(buffer, 0, blockSize); // Read stdout into the buffer

            if (byteCount == 0) // FFmpeg did not output anything
                break; // Break out of the while(true) loop, since there was nothing to read.

            vClient.Send(buffer, 0, byteCount); // Send our data to Discord
        }
        vClient.Wait(); // Wait for the Voice Client to finish sending data, as ffMPEG may have already finished buffering out a song, and it is unsafe to return now.
    }`

first i tried it with NAudio and where it wont work i copied the code of ffmpeg and in both are coming the same exception [...] i searched the problem but not found anything i tryed much things but i dont come to a workaround

using Dicord.net, Audio, Commands and Modules

thanks in advance

NAudio Code: `private async void SendAudioNAudio(string FilePath, string CurrentServer) { var VoiceChannel = client.FindServers(CurrentServer).FirstOrDefault().VoiceChannels.FirstOrDefault(); var vClient = await client.GetService() .Join(VoiceChannel); var ChannelCount = client.GetService().Config.Channels; var OutFormat = new WaveFormat(48000, 16, ChannelCount);

        try
        {
            using (var MP3Reader = new Mp3FileReader(FilePath))
            using (var Resampler = new MediaFoundationResampler(MP3Reader, OutFormat))
            {
                Resampler.ResamplerQuality = 60;
                int BlockSize = OutFormat.AverageBytesPerSecond / 50;
                byte[] Buffer = new byte[BlockSize];
                int ByteCount;

                while ((ByteCount = Resampler.Read(Buffer, 0, BlockSize)) > 0)
                {
                    ConsoleSendMessage("BlockSize: " + BlockSize.ToString() + "\nByteCount: " + ByteCount.ToString(), true);
                    if (ByteCount < BlockSize)
                    {
                        for (int i = ByteCount; i < BlockSize; i++)
                            Buffer[i] = 0;
                    }
                    vClient.Send(Buffer, 0, BlockSize);
                }///while Ending
            }
        }
        catch (Exception ex)
        {
            ConsoleSendMessage(ex.Message.ToString(), false);
            MessageBox.Show(ex.Message, ex.GetType().ToString());
        }
    }///SendAudio Ending`
AntiTcb commented 7 years ago

A BadImageFormatException will most likely stem from you having the incorrect binaries for your architecture. (32-bit when you need 64-bit, or vice versa). Check out https://discord.foxbot.me/binaries/ and grab the proper binaries and try again.

hasezoey commented 7 years ago

Thanks that fixed the Problem [...] i searched in the wiki of Discord.NET but only found 1 version of opus ^^