What I have tried : Webcam in opencv,C++. It play a wav file but since ive added "&" , therefore it keeps playing several instances , rather than wait for the first playback to finish. Thus creating an overlapping echo effect.
What I want : The sound playback shouldnt halt/slow down webcam (it should be asynchronous). A mechanism to know if the sound is being played already, and wait for it to finish. The library or mechanism should work on linux.
int play (string audio_file) {
string str1= "aplay " ;
str1 = str1 + audio_file;
constchar *command = str1.c_str();
system(command);
return 0;
}
int main()
{
VideoCapture cap(1);
cap >> frame;//get the first frame
imshow("Video", frame);
std::future<int> handle;
bool first = true;
while (1)
{
cap >> frame;//capture second frame
imshow("Video", frame);
// If this is the first time, or
// the previous instance has finished and returned an async result, then...
if ( first || handle.valid() ) {
handle = std::async(std::launch::async, play , "Powerup5.wav");
first = false;
}
if (waitKey(30) >= 0)
break;
}
// you should do something with handle here
return 0;
}
Using system() to do work is crusty. It might be the quickest way to get going, but it should only be an interim fix rather than a solution.