简体   繁体   中英

SDL 2 on windows works incorrectly with audio device

It seems that I've found a bug between the libraries of SDL compiled for mingw and visual studio.

I'm trying to open the audio device with the following code:

#include <SDL.h>
#include <stdio.h>
int main(int argc, char **argv)
{
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        exit(1);
    }

    SDL_AudioSpec wanted_spec, spec;

    wanted_spec.channels = 2;
    wanted_spec.freq = 44100;
    wanted_spec.format = AUDIO_S16SYS;
    wanted_spec.silence = 0;
    wanted_spec.samples = 2048;
    wanted_spec.callback = 0;
    //wanted_spec.userdata = opaque;
    while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
        printf("error cant open audio");
    }
    if (spec.format != AUDIO_S16SYS) {
        printf("error wrong format");
        return -1;
    }

    return 0;
}

When compiling this via MSVS I get "error wrong format"

The same code compiled via mingw32 with the command:

g++ main.c -I/mingw32/include/SDL2 -L/mingw32/lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2

works correctly. Can someone point me to the reason for this? This is done on the same machine. Could this be caused by different versions of SDL libraries?

There were 2 issues:

  1. I've downloaded a new version of SDL2.dll.

  2. This caused the OpenAudio function to fail with XAudio2Create() error.

I resolved this by doing what Wagner Patriota suggested. I've added

 putenv("SDL_AUDIODRIVER=DirectSound");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM