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 kill
ing, 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!
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/