Help with SDL_mixer (no sound)

Posted by Kaizoku on Stack Overflow See other posts from Stack Overflow or by Kaizoku
Published on 2010-12-25T01:43:23Z Indexed on 2010/12/25 1:54 UTC
Read the original article Hit count: 420

Filed under:
|
|
|
|

Hello,

I have this strange problem with SDL_mixer, it doesn't want to play music. It doesn't throw any error, it just skips it. Any advice? I am compiling on linux with libvorbis.

audio.h

#ifndef AUDIO_H
#define AUDIO_H

#include <string>
#include <SDL/SDL_mixer.h>

class Audio {
private:
    Mix_Music *music;

public:
    Audio();
    virtual ~Audio();

public:
    void setMusic(std::string path);
    void playMusic();
};

#endif  /* AUDIO_H */

audio.cpp

#include "Audio.h"
#include <stdexcept>

Audio::Audio() {
    if (0 == Mix_Init(MIX_INIT_OGG))
        throw std::runtime_error(Mix_GetError());

    if (-1 == Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096))
        throw std::runtime_error(Mix_GetError());
}

Audio::~Audio() {
    Mix_FreeMusic(music);
    Mix_Quit();
}

void Audio::setMusic(std::string path) {
    music = Mix_LoadMUS(path.c_str());

    if (NULL == music)
        throw std::runtime_error(Mix_GetError());
}

void Audio::playMusic() {
    if (NULL != music) {
        if (-1 == Mix_PlayMusic(music, -1))
            throw std::runtime_error(Mix_GetError());
    }
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about linux