-1

I'm trying to play an mp3 file as background music for my game but somehow the music cannot start and I get this error "Unrecognized audio format". As far as i know, the SDL_mixer 2.6.3 does support mp3 file and I'm sure that the file path is correct. Here is my code:

 `if(Mix_Init(1)==0)
{
    cout << "Sound can not initilize!" << Mix_GetError() << endl;
}
else
{
    backgroundSound = Mix_LoadMUS("sound/bgmusic.mp3");
    if(backgroundSound == NULL)
        cout << "failed" << Mix_GetError();
}your text`

I want an explanation for the error and how to fix it

1 Answers1

0

You need to pass required audio formats to Mix_Init and check if it succeded (in your case - MIX_INIT_MP3), e.g.

    if(!(Mix_Init(MIX_INIT_MP3) & MIX_INIT_MP3))
    {
        cout << "Sound can not initilize!" << Mix_GetError() << endl;
        return 1;
    }

You can only load sound files after you open sound device, e.g.:

    if(Mix_OpenAudio(48000, AUDIO_S16SYS, 2, 2048) != 0)
    {
        cout << "Can't open audio device: " << Mix_GetError() << endl;
        return 1;
    }
keltar
  • 17,711
  • 2
  • 37
  • 42