Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing SDL_Mixer gives error "No available audio device"

Tags:

c++

ubuntu

sdl

I am working on a C++ application which uses SDL/SDL_Mixer to play wav files. I've been developing the application on a Mac without much problem. However, I do need this application to work on Linux, so I put VirtualBox on my Windows 7 machine with Ubuntu 12.04 LTS. Compilation works fine, until I actually try to initialize the system. Then, SDL_Mixer gives an error "No available audio device."

Here is the code where the error is thrown:

using namespace std;

void simple_sound_init() {
    if (SDL_Init(SDL_INIT_AUDIO) == -1) {
        fprintf(stderr, "init SDL error: %s\n", SDL_GetError());
        SDL_Quit();
        exit(1);
    }

    if (Mix_OpenAudio(SOUNDSAMPLERATE, MIX_DEFAULT_FORMAT, 1, 1024) != 0) {
        fprintf(stderr, "initialize audio error: %s\n", Mix_GetError());
        Mix_CloseAudio();
        SDL_Quit();
        exit(1);
    }

    Mix_ChannelFinished(simple_sound_channel_finised);
}

The exact error I get is:

initialize audio error: No available audio device

P/S: I have searched extensively online for solutions and I've tried checking over the libraries install, etc. However, since it is possible that I've missed something, any basic library suggestions are welcome, and I will confirm that I have them set up.

like image 733
A.P. Avatar asked May 05 '12 19:05

A.P.


1 Answers

I got exactely the same problem in the same situation, but with ubuntu 14.04LTS (Virtual box, W7 as host). But the problem is not the VM configuration (the sound works fine with other ubuntu applications).

I found the answer there: http://forums.libsdl.org/viewtopic.php?t=7609&sid=40fdb9756b8e22e1b8253cda3338845f

Thanks to Ryan C. Gordon who writes:

"If you built your own SDL, you probably didn't have development headers for PulseAudio (or ALSA), so it's trying to use /dev/dsp, which doesn't exist on many modern Linux systems (hence, SDL_Init(SDL_INIT_AUDIO) succeeds, but no devices are found when you try to open one). "apt-get install libasound2-dev libpulse-dev" and rebuild SDL...let the configure script find the new headers so it includes PulseAudio and ALSA support."

I had effectively built SDL and SDL-mixer from source but without any headers from pulseAudio or ALSA. After installing the libs mentionned and recompiled both SDL and SDL_mixer (v1.2), the sounds works fine.

If the problem is still there, he also talks about another solution:

"If you didn't build your own SDL, maybe you can force it to use a different audio path: SDL_AUDIODRIVER=pulse ./mytestprogram or SDL_AUDIODRIVER=alsa ./mytestprogram"

I did not try that as the first solution was the good one in my case.

like image 133
MaXx Avatar answered Oct 18 '22 16:10

MaXx