我正在开发C++游戏,我想一开始就播放音乐。到目前为止,这是我的代码示例:
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;
}
但是,
kill $!
不是kill
或停止afplay
音乐。我认为这是因为system("afplay ~/music.mp3 &>/dev/null &");
没有将PID输出到$!
中。如何才能
kill
afplay
或至少获取其PID,以便可以对其进行kill
?我在Mac上,对C++还是陌生的...
警告:这是一种不良做法;它可能杀死重要进程并杀死所有进程,因此可能会停止用户正在使用的任务!
最佳答案
关于c++ - 使用C++ shell()获取PID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42306529/