Thread: Play sound on a Mac

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    Play sound on a Mac

    >>> Split from C program to play a sound - please don't dig up old posts, see the rules >>>

    Quote Originally Posted by MK27 View Post
    DWK was right about my invocation of "execvp" being wrong -- hopefully i'm about to correct myself.

    If your problem with using "system" or "exec" is that you can't stop playback after it's started, then I have your solution (I once made a perl/Tk mp3 app where this figured in). Linux uses process id's ("pid"s) to track all running processes (see a list with "ps"), and you can signal the mp3 playing process to do different things with it's pid.

    If you use a simple console based app like mpg123, you can call it without any pop-up windows popping up, etc -- it just plays the file. Then get the pid and kill it when you want. In perl "system" forks a process automatically and returns you the new pid (easy). In C, running a separate program with "system" and "exec" does NOT give you a new pid -- so if you kill the "mpg123" process you will kill your entire program. So you have to fork a process (=what I learned about C today).

    Anyway, the following code will play a song until you hit ENTER, then it kills mpg123 (stopping the playback) and leaves a message.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h> // for execlp
    
    int main () {
            pid_t x;      // a special kind of int
            char kil[20] = "kill -s 9 ";
    
            x = fork();   /* now there's actually two "x"s:
    if fork succeeds, "x" to the CHILD PROCESS is the return value of fork (0)
    and "x" to the PARENT PROCESS is the actual system pid of the child process.*/
            
            if (x < 0) {  // just in case fork fails 
                    puts("fork failure");
                    exit(-1);
            }   
            else if (x == 0) { // therefore this block will be the child process 
                    execlp("mpg123", "mpg123", "-q", "/songs/bertha.mp3", 0); 
            }                   // see GNU docs, "system" also works                
            else {  printf("from parent: mpg123 is pid %d\nENTER to quit\n", x);
                    sprintf(kil,"%s%d",kil,x);
                    getchar();  // wait for user input
                    system(kil);
                    printf("All ");
            }       /* witness that the "else if" and "else" blocks are both executed here in parallel. The "else" 
       (parent) block is continuous with the rest of the program (since the PARENT PROCESS is actually
       the program itself) so... */
            printf("done.\n");
            exit(0);
    }
    Get it? Anyway, that might be all you need.
    Hi,

    I wanna use the above code to play *.mp3. I replaced the filename "/songs/bertha.mp3" by a filename i want to play on. i complied and run it with the following command

    gcc *.c
    then

    ./a.out


    i am getting the following

    from parent: mpg123 is pid 9832
    ENTER to quit
    done.


    It doesn't play the mp3. It seams that i missed something. Can you help me?


    Thanks,

    Joealem.

    P.S : I am working on mac.
    Last edited by Joealem; 12-27-2010 at 11:59 AM. Reason: additional message

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you have a program called "mpg123" on your Mac? I don't.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Nor do I.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    I don't have it.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Soooooooooo, change the name "mpg123" to whatever you have on your system that plays mp3 files. (Or I suppose you can go get mpg123.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ program to play sound on Mac OS X
    By amelia25 in forum C++ Programming
    Replies: 3
    Last Post: 06-24-2008, 10:53 AM
  2. DirectX Question
    By bladerunner627 in forum Game Programming
    Replies: 2
    Last Post: 04-04-2005, 11:55 AM
  3. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM