0

I have this c program which used to search for the mp3 files of given directory and add the files data to linked list.i use SDL2 for audio handling. so much far i would be able to get the playing of mp3 file and what i'm facing here is that i don't know how to play the next song when the first song is played in the linked list.

Here is my code for the program.

#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "file_handle.h"

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

struct tags
{
  const char *artist;
} tags;

Mix_Music *music = NULL;

bool loadmedia(char *filepath);
void play();
void init();
void load_file_directory(char *dir_path);

int main(int argc, char *argv[])
{

  char *file_path;

  if (argc <= 1)
  {
    fprintf(stderr, "no arguments provided!\n");
  }
  else
  {
    file_path = argv[2];
  }

  if (file_path)
  {
    load_file_directory(file_path);
  }

  init();

  int count = 0;
  for (struct song *node = head; node; node = node->next)
  {
    printf("%s\n", node->file_path);
  }
  for (struct song *node = head; node; node = node->next)
  {
    printf("%s\n", node->file_path);
    printf("count is %d\n", count);
    count++;
    Mix_Music *song = Mix_LoadMUS(node->file_path);
    if (Mix_PlayMusic(song, 1) != 0)
    {
      printf("something\n");
    }
    while (!SDL_QuitRequested())
      SDL_Delay(250);
    Mix_FreeMusic(song);
  }

  // char *file = argv[1];

  // bool status = loadmedia(file);
  // if(status){
  //   printf("%s - %s\n",Mix_GetMusicArtistTag(music),Mix_GetMusicTitleTag(music));
  //   if(Mix_PlayMusic(music,1)){
  //     printf("something wrong");
  //     Mix_FreeMusic(music);

  //   }

  // }

  // while(!SDL_QuitRequested())
  // SDL_Delay(250);
}

bool loadmedia(char *file_path)
{

  bool success = false;
  if (file_path)
  {
    music = Mix_LoadMUS(file_path);
    success = true;
  }
  else
  {
    printf("File Loaded Failed\n");
  }

  return success;
}

void init()
{

  bool success;
  if (SDL_Init(SDL_INIT_AUDIO) < 0)
  {
    printf("Couldn't initialize SDL: %s\n", SDL_GetError());
    success = false;
  }

  if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3)
  {
    fprintf(stderr, "could not initialized mixer\n", Mix_GetError());
  }

  if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
  {
    printf("SDL_mixer could not be initialized %s\n", Mix_GetError());
    success = false;
  }
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
devMe
  • 126
  • 10
  • 1
    You could use [Mix_PlayingMusic](https://wiki.libsdl.org/SDL_mixer/Mix_PlayingMusic) to check if any music is playing and if not, switch to next track; or you could set completion callback via [SDL_HookMusicFinished](https://wiki.libsdl.org/SDL_mixer/Mix_HookMusicFinished). With SDL2_Mixer 2.6+ you can also use `Mix_GetMusicPosition`/`Mix_MusicDuration` to get current/max duration of specified music object. – keltar Dec 17 '22 at 16:48
  • thank you i found this helpful. i use a linked list that defined in another header file but when i use the node to use the `HookMusicFinished` the compiler give me an error saying the node is undeclared because of the linked list is defined in another header file. i can use the my header file function.l i already had added header by using include but i can't access the node in my function in main.c file. what can i do?. – devMe Dec 20 '22 at 16:42
  • 1
    Can't say anything without seeing the code and exact error message. That seems to be a separate question, unrelated to sound. Note that hook function will be executed in another thread. – keltar Dec 20 '22 at 17:27
  • Hey i found the problem that because of the node that i declared in the function. setting the variable globally fixed the issue. Thank you!. – devMe Dec 21 '22 at 04:03

0 Answers0