1

I've been working on a C++ application that plays a different video depending on what a user did. So Far I've worked with OpenCV to only play the video, since I need to do some changes on the video on real time. Since OpenCV doesn't play sound, I need to use a different library to play the sound independently. So far I tried FFMPEG, but I couldn't get it work, since I'm not experienced on working on C++ (At least in a computer with external libraries, I normally programm it on microcontrollers).

The sound doesn't need to sync with the video and it also only should run on Windows. What is my best option?

Thanks.

Ricardo Alves
  • 1,071
  • 18
  • 36
  • main ability of opencv is to process the frames, not to I/O them. I would use Qt + phonon for this. http://qt-project.org/doc/qt-5/multimedia-examples.html – baci Jul 09 '14 at 11:50
  • I' using OpenCV to process Frames, as I need to process a video in real time. However, OpenCV does not process audio and I need to play it too, exactly how it is in the video. – Ricardo Alves Jul 09 '14 at 12:59
  • opencv can I/O video as image data -actually it uses ffmpeg at lower layer- however its main ability is to process them. For audio it has no capability. – baci Jul 09 '14 at 15:49
  • As I said, I actually only want to run the audio of the video, I don't want to process it. – Ricardo Alves Jul 09 '14 at 16:05

1 Answers1

0

If it does only need to work for Windows, the WINAPI function PlaySound should fulfil your requirements.

#include "windows.h"
#include "mmsystem.h"

void play()
{
    PlaySound(TEXT("sound.wav"), NULL, SND_FILENAME); // plays sound.wav once
}

To make this work you need to link against winmm.lib.

If your file name is not ASCII you should use PlaySoundA (ANSI) or PlaySoundW (unicode) instead.

See also this question if the sound is not played properly.

Community
  • 1
  • 1
Theolodis
  • 4,977
  • 3
  • 34
  • 53
  • I did what you said, but for some reason it plays the "wrong" sound of windows (I must have made something wrong). here's the code: PlaySound(TEXT("C:/Users/Ricardo/Documents/Visual Studio 2013/Projects/SomEVideo/SomEVideo/KinectAudio-02-23-32.wav"), NULL, SND_FILENAME); // plays sound.wav once – Ricardo Alves Jul 09 '14 at 12:41
  • I can't really help you, as I do not understand what "wrong sound of windows" it is playing. You should probably make a new question out of that. – Theolodis Jul 09 '14 at 12:42
  • Here's exactly what I did: First included the headers on stdafx.h, then put only this line of code in the main funtion to test it PlaySound(TEXT("C:/Users/Ricardo/Documents/Visual Studio 2013/Projects/SomEVideo/SomEVideo/KinectAudio-02-23-32.wav"), NULL, SND_FILENAME);. After this, I linked winmm.lib in the project properties. When I runned the program, instead of playing the wav sound I wanted it played this wav sound: https://dl.dropboxusercontent.com/u/45453297/Windows%20Background.wav – Ricardo Alves Jul 09 '14 at 12:55
  • @RicardoAlves from the linked specification: "SND_FILENAME The pszSound parameter is a file name. **If the file cannot be found, the function plays the default sound** unless the SND_NODEFAULT flag is set." – Theolodis Jul 09 '14 at 13:02
  • @RicardoAlves isn't windows using Backslashes? Shouldn't the path be: "C:\Users\Ricardo\[etc...]\KinectAudio-02-23-32.wav" – Theolodis Jul 09 '14 at 13:10
  • @RicardoAlves go ask a new question with that problem, I will probably not be able to help you, sorry I can't reproduce the problem. – Theolodis Jul 09 '14 at 13:19
  • I already tried it. PlaySoundA doesn't even in recognized by the compiler, but PlaySoundW does the same as PlaySound. I really have no clue on what to do next. – Ricardo Alves Jul 09 '14 at 13:40
  • @RicardoAlves try if it does work with a relative path. Your path is probably wrong. – Theolodis Jul 09 '14 at 13:46
  • I've oppened a new wuestion as you said and it explains what I've tried: http://stackoverflow.com/questions/24654961/c-play-audio-in-windows – Ricardo Alves Jul 09 '14 at 13:48
  • The wav file I was testing for some reason doesn't work. I tried with another wav file and it does work. I then tried to run the sound file from my video and it also doesn't work. Any ohter solution to play sound from a video? – Ricardo Alves Jul 09 '14 at 14:14