如何在不停止程序的情况下使用子进程在python中播放声音文件?

时间:2018-03-06 01:10:45

标签: python audio subprocess

对于我的程序,我一直在使用 int main(int argc, char *argv[]) { ifstream inFile; int numOfLines = 0, numOfTokens = 0, numOfStrings = 0, maxStringLength = 0, l = 0, fileCount=0, mostCommonCount=0; string inputFile, mostCommonList="", word; for(int i = 1; i < argc; i++){ if(strpbrk(argv[i] , "-")){ if(flags.find(string(argv[i]))!=flags.end()) flags[string(argv[i])] = true; else{ cerr << "INVALID FLAG " << argv[i] << endl; exit(1); } } else{ inFile.open(argv[i]); fileCount++; if(!inFile && fileCount==1){ cerr << "UNABLE TO OPEN " << argv[i] << endl; exit(1); } else{ string line; while(getline(inFile, line)) inputFile+=line+='\n'; if(fileCount>1){ cerr << "TOO MANY FILE NAMES" << endl; exit(1); } } } } int linenum = 0; TType tt; Token tok; while((tok = getNextToken(&inFile, &linenum))!=DONE && tok != ERR){ tt = tok.GetTokenType(); word = tok.GetLexeme(); if(flags["-v"]==true){ (tt == ICONST||tt==SCONST||tt==IDENT) ? cout<<enumTypes[tok.GetTokenType()]<<"("<< tok.GetLexeme()<<")"<<endl : cout<< enumTypes[tok.GetTokenType()]<<endl; } if(flags["-mci"]==true){ if(tt==IDENT){ (identMap.find(word)!=identMap.end()) ? identMap[word]++ : identMap[word]=1; if(identMap[word]>mostCommonCount) mostCommonCount = identMap[word]; } } if(flags["-sum"]==true){ numOfTokens++; if(tt==SCONST){ numOfStrings++; l = word.length(); if(l > maxStringLength) maxStringLength = l; } } } if(tok==ERR){ cout << "Error on line" << tok.GetLinenum()<<"("<<tok.GetLexeme()<<")"<<endl; return 0; } if(flags["-mci"]==true){ cout << "Most Common Identifier: "; if(!identMap.empty()){ word =""; for(auto const& it : identMap){ if(it.second==mostCommonCount) word += it.first + ","; } word.pop_back(); cout << word << endl; } } if(flags["-sum"]){ numOfLines = tok.GetLinenum(); numOfLines = tok.GetLinenum(); cout << "Total lines: " << numOfLines << endl; cout << "Total tokens: " << numOfTokens << endl; cout << "Total strings: " << numOfStrings << endl; cout << "Length of longest string: " << maxStringLength << endl; } inFile.close(); return 0; } 为我的Python 3程序播放我的.wav文件。但是,当播放声音时,程序将等待音频文件完成播放,然后才能运行其余的程序。

我知道winsound和pygame有很多方法可以播放与运行程序同步的声音,但我更喜欢使用subprocess,因为pygame还没有为我工作,我可以& #39; t使用winsound,因为我在Mac上。

以下是我所拥有的:

subprocess

import subprocess

0 个答案:

没有答案