I have this program which used to load mp3 file from cmd argument but when i use the Mix_PlayMusic the audio isn't playing:
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
Mix_Music *mp3 = NULL;
bool loadmedia(char *filepath);
void play();
int main(int argc, char *argv[]){
bool success;
if (SDL_Init(SDL_INIT_AUDIO) < 0)
{
printf("Couldn't initialize SDL: %s\n", SDL_GetError());
success = false;
}
if(Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,2048)<0){
printf("SDL_mixer could not be initialized %s\n",Mix_GetError());
success = false;
}
char *file = argv[1];
bool status= loadmedia(file);
}
bool loadmedia(char *file_path){
bool success = true;
printf("%s is path\n",file_path);
mp3 = Mix_LoadMUS(file_path);
if(mp3==NULL){
printf("media load failed\n");
success = false;
}
else{
printf("media loaded succefully\n");
Mix_PlayMusic(mp3,-1);
play();
}
return success;
}
void play(){
Mix_FreeMusic(mp3);
}
i use sdl2 and i used both wav and mp3 files but nothing is working.
so the problem was SDL_Delay()
wasn't added to program which the file has not time to be played adding SDL_Delay()
fixed the problem.