-1

I'm working on a C++ game, which I want to play music in the beginning. This is a sample of my code so far:

int main() {
    // Gets user's name
    system("clear");
    system("afplay ~/music.mp3 &>/dev/null &");
    string name;
    cout << "###################\n";
    cout << "# Enter your name #\n";
    cout << "###################\n";
    cin >> name;

    // Greets user
    system("clear");
    cout << "So, your name is " << name << "?\n";
    system("sleep 1.5");
    cout << "Greetings, and welcome to the world of NULL!\n\n";
    system("kill $!");

    return 0;
}

However, the kill $! isn't killing, or stopping the afplay music. I think it is because the system("afplay ~/music.mp3 &>/dev/null &"); isn't outputting the PID into $!.

How can I kill afplay or at least get its PID so I can kill it?

I'm on a Mac and I'm new to C++...

WARNING: This is a bad practice; it could kill important processes and kills all, so it may stop tasks that the user was using!

anonymous
  • 271
  • 3
  • 16
  • Are you sure that call a separate process for play music in application is a good idea? :) – Zefick Feb 17 '17 at 20:12
  • Why wouldn't it be? – anonymous Feb 17 '17 at 20:13
  • @KeeganKuhn well, for one thing, what about if someone has a system that has a program called "afplay" that shuts down their nuclear reactor? You should not be using `system` for anything your program can do itself and until you get very experienced with C++, you probably shouldn't use `system` at all. – David Schwartz Feb 17 '17 at 20:14
  • Well, that would be odd... "afplay" comes with OSX and stands for "Audio File Play". – anonymous Feb 17 '17 at 20:15
  • @KeeganKuhn There are operating systems other than OSX. And who knows what the next version of OSX will include or not include. You are doing things that just happened to work and so you assumed they were safe. This is not a good practice to get into and if you're still learning C++, you should stick to safe habits until you get much more experienced. Otherwise, you may never learn them. – David Schwartz Feb 17 '17 at 20:17
  • @KeeganKuhn it just looks strange, but if it is a Mac's geature then OK. – Zefick Feb 17 '17 at 20:18
  • @KeeganKuhn I think what they are getting at is that you don't make calls to the system unless it is actually necessary. Also, this would launch a seperate program that you have no control over. I would suggest reading about and understanding what libraries and .so/.dll (or whatever the OSX equivalents are) are before making games. Even if you are just starting to learn. – Martin Hollstein Feb 17 '17 at 20:18
  • I'm designing this for Mac OS. I'll make one for Linux and Windows as well. I don't won't people to have to download new C++ sound plugins, it's supposed to be somewhat portable... – anonymous Feb 17 '17 at 20:19
  • @KeeganKuhn For example, on your system, there's a command called `clear` that just happens to clear the screen. There is how things just happen to be, not how they must be. Some other system might have a command called `clear` that clears the print queue. There are sufficient guarantees provided that you do not need to use things that are not guaranteed. So learn the guarantees, use the things that are guaranteed, and save `system` for things that are not possible to do the right way. – David Schwartz Feb 17 '17 at 20:19
  • Libraries are made to be portable @KeeganKuhn. It is pretty obvious that you are new (re: P.P.S.). So I would suggest understanding these concepts and take the advice of the people helping you. – Martin Hollstein Feb 17 '17 at 20:20
  • I've made some games in Python, BASh, and "Batch" before; just trying out C++... – anonymous Feb 17 '17 at 20:20
  • And last, but definitely not least, *never* assume something is guaranteed to work a particular way or do a particular thing just because that's what it happened to do when you tried it. Either there is a guarantee that there is a command called `clear` that clears the screen or there is not. If you think there is, you should know where that guarantee is found and who supplied it. Otherwise, do not rely on it. (If you don't plan to share this program with others maybe it's okay for messing around. But what if those others have a `clear` command that does something else. How do you know?) – David Schwartz Feb 17 '17 at 20:21
  • Okay, I've read other places that C++ needs `system()` to `clear` or `cls`, though. Also, what OS do you use? – anonymous Feb 17 '17 at 20:22
  • @KeeganKuhn Where did you read that? Think about this -- what language do you think the `clear` or `cls` program is written in? On most platforms, it's C or C++. – David Schwartz Feb 17 '17 at 20:24
  • Guess I didn't think of that! ;) Read it on http://stackoverflow.com/questions/17335816/clear-screen-using-c, but now I see the `cout << "\033[2J\033[1;1H";` or `clrscr()` answer! – anonymous Feb 17 '17 at 20:27

1 Answers1

0

Apple's manual page says that you may just call

killall afplay

http://osxdaily.com/2010/12/07/command-line-mp3-player-in-mac-os-x/

Zefick
  • 2,014
  • 15
  • 19
  • Note that this will kill all processes named `afplay` on the system (that the user running the program has permission to kill). So now you've imposed a rule that nobody who uses your program may name any critical program `afplay` or it will be terminated when they run your program. That's really, really bad practice. – David Schwartz Feb 17 '17 at 20:22
  • What do you suggest for playing an audio file in C++, then? – anonymous Feb 17 '17 at 20:24
  • @KeeganKuhn If you want to ask a new question, you need to ask a new question [here](http://stackoverflow.com/questions/ask). – Fantastic Mr Fox Feb 17 '17 at 21:21