Search code examples
c++gtkaudio-player

Playing Audio files with GTK and c++


i'm currently building a very simple music player with the gtk+ and c++ code but now i am unable to figure out how to open and play the audio file using C++ code.

#include <gtk/gtk.h>
// simple music player to practice gtk and c++//

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

GtkWidget *window;
GtkWidget *playButton;
GtkWidget *fileButton;
GtkWidget *frame;
GtkWidget *Dialog;


window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);

frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);

playButton = gtk_button_new_with_label("Play");
gtk_widget_set_size_request(playButton,80,40);
gtk_fixed_put(GTK_FIXED(frame),playButton,40,330);

fileButton = gtk_button_new_with_label("Open");
gtk_widget_set_size_request(fileButton,80,40);
gtk_fixed_put(GTK_FIXED(frame),fileButton,40,260);

gtk_widget_show_all(window);
gtk_main();

return 0;
}

so as you can see i have created the open button to select your files and i know the dialog code;

GtkWidget *dialog;

dialog = gtk_file_chooser_dialog_new ("OpenFile",parent_window,GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);

if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  {
    char *filename;

    filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
    open_file (filename);
   g_free (filename);
 }

gtk_widget_destroy (dialog);

but my problem is i do not know how to place the code, i should more than likely create a function and set a callback to that function when the open buttot is clicked. right? And then the second problem coes no matter how hard i search i can't seem to find how to play the audio file, thanks so much in advance!


Solution

  • If you don't mind using external libraries Allegro makes it incredibly easy to play Audio files in a variety of formats. Here is an example of how to play a .wav audio file.

    #include <stdio.h>
    #include <allegro5/allegro.h>
    #include <allegro5/allegro_audio.h>
    #include <allegro5/allegro_acodec.h>
    
    int main(int argc, char **argv){
    
       ALLEGRO_DISPLAY *display = NULL;
       ALLEGRO_SAMPLE *sample=NULL;
    
       if(!al_init()){
          fprintf(stderr, "failed to initialize allegro!\n");
          return -1;
       }
    
       if(!al_install_audio()){
          fprintf(stderr, "failed to initialize audio!\n");
          return -1;
       }
    
       if(!al_init_acodec_addon()){
          fprintf(stderr, "failed to initialize audio codecs!\n");
          return -1;
       }
    
       if (!al_reserve_samples(1)){
          fprintf(stderr, "failed to reserve samples!\n");
          return -1;
       }
    
       sample = al_load_sample( "footstep.wav" );
    
       if (!sample){
          printf( "Audio clip sample not loaded!\n" ); 
          return -1;
       }
    
       display = al_create_display(640, 480);
    
       if(!display){
          fprintf(stderr, "failed to create display!\n");
          return -1;
       }
    
       /* Loop the sample until the display closes. */
       al_play_sample(sample, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);
    
       al_rest(10.0);
    
       al_destroy_display(display);
       al_destroy_sample(sample);
       return 0;
    }