Thread: Can you play audio with SDL_mixer.h without a extra window showing?

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    Can you play audio with SDL_mixer.h without a extra window showing?

    I got this code from a wiki that taught me how to make C code that plays audio, and with a few changes I was able to get it to take a command-line argument as the music file name. Here it is

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include <SDL/SDL.h>
    #include <SDL/SDL_mixer.h>
    
    
    int main(int argc, char *argv[])
    {
        if (argc != 2)
        {
            fprintf(stderr, "You must enter 1 argument!");
            exit(1);
        }
    
    
        SDL_Surface *screen;            //Pointer to the main screen surface
        Mix_Chunk *sound = NULL;        //Pointer to our sound, in memory
        int channel;                //Channel on which our sound is played
    
    
        int audio_rate = 22050;            //Frequency of audio playback
        Uint16 audio_format = AUDIO_S16SYS;     //Format of the audio we're playing
        int audio_channels = 2;            //2 channels = stereo
        int audio_buffers = 4096;        //Size of the audio buffers in memory
    
    
        //Initialize BOTH SDL video and SDL audio
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
            printf("Unable to initialize SDL: %s\n", SDL_GetError());
            return 1;
        }
    
    
        //Initialize SDL_mixer with our chosen audio settings
        if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
            printf("Unable to initialize audio: %s\n", Mix_GetError());
            exit(1);
        }
    
    
        //Load our WAV file from disk
        sound = Mix_LoadWAV(argv[1]);
        if(sound == NULL) {
            printf("Unable to load WAV file: %s\n", Mix_GetError());
        }
    
    
        //Set the video mode to anything, just need a window
        screen = SDL_SetVideoMode(0, 0, 0, SDL_ANYFORMAT);
        if (screen == NULL) {
            printf("Unable to set video mode: %s\n", SDL_GetError());
            return 1;
        }
    
    
        //Play our sound file, and capture the channel on which it is played
        channel = Mix_PlayChannel(-1, sound, 0);
        if(channel == -1) {
            printf("Unable to play WAV file: %s\n", Mix_GetError());
        }
    
    
        //Wait until the sound has stopped playing
        while(Mix_Playing(channel) != 0);
    
    
        //Release the memory allocated to our sound
        Mix_FreeChunk(sound);
    
    
        //Need to make sure that SDL_mixer and SDL have a chance to clean up
        Mix_CloseAudio();
        SDL_Quit();
    
    
        //Return success!
        return 0;
    }
    The only problem is, even though I guess it needs to create a window to work with SDL, is there any way you could hide it or something so the user can't see it? I don't really want to see it ( I'm planning on running it from Batch files or C applications as an easy way to play an audio file once ), because if I did, I might as well just use a default audio player instead.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    What happens if you remove SDL_INIT_VIDEO and the call to SDL_SetVideoMode?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Salem, it stops playing the audio completely. That was the first thing I tried. The window doesn't pop up when I call the program, but nothing happens.

  4. #4
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Wait, oops, you have to remove the SDL_INIT_VIDEO too? Oh that fixed it, thanks Salem. ( I need to pay more attention when I'm reading )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to play an audio file in c program?
    By yugal0007 in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2009, 09:01 PM
  2. GLUT, window not showing up
    By Poincare in forum C Programming
    Replies: 2
    Last Post: 05-24-2009, 09:48 PM
  3. Window not actually showing.
    By Shamino in forum Windows Programming
    Replies: 3
    Last Post: 02-22-2008, 10:55 AM
  4. Showing a window from inside a DLL
    By Rare177 in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2004, 11:39 AM
  5. Window not showing
    By Rare177 in forum Windows Programming
    Replies: 7
    Last Post: 06-25-2004, 05:38 AM