C++ Play video audio in console application - c++

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.

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.

Related

How to play a music file using cpp program? [duplicate]

A few years back, I wrote some util library around DShow/DSound to let me play MP3s in a Windows C++ application. Is that still the normal way to do it in a C++/MFC app, or is that an area of DirectX that has been subsumed into the general Windows APIs?
The motivation is simply we use the standard Windows PlaySound method for WAVs, and would like to be able to play MP3s using a similarly simple API, either provided by Windows or something we write to wrap more complex functionality.
EDIT: this is for a large, commercial, closed-source project. And we only want to play things simply, paying a lot for a library won't fly.
PlaySound() natively supports MP3 as long as it is embedded in a WAV file.
People don't realize that WAV is a container format.
Download the ffmpeg utilities to convert the header and preserve the codec:
ffmpeg -i input.mp3 -c copy -f wav embedded_mp3.wav
You can either use DirectShow but it's not part of DirectX anymore or rely on a third-party library like Bass, FMod, mpg123 or even libwmp3.
If you don't want to use DirectShow anymore (but why change if your existing code keeps working?), you can use MCI:
mciSendString("open la_chenille.mp3 type mpegvideo alias song1", NULL, 0, 0);
mciSendString("play song1", NULL, 0, 0);
mciSendString("close song1", NULL, 0, 0);
IGraphBuilder::RenderFile is an easy way to play any audio file.
Youc could use MCI windows functions,
https://msdn.microsoft.com/en-us/library/ms709626
It can play many of audio file formats including MP3, WAV, MIDI etc.
If I recall correctly it does not require DirectX.
The PlaySound function might also work for you.
If you don't want to pay any licence and wanna do in-house, do the parsing of your mp3 file and pass it to XAudio2.
Its a thing that you can do once (2-3 hours at max) and use always. :P
You could have a look at BASS. It's a simple to use audio library, free for noncommercial use.

OpenAL C++ on Linux

I just need a simple program which allows me to play and stop an audio file. I'm guessing OpenAL is the way to go? All I need is this functionality - start audio file with spacebar and stop audio file with a second press of the spacebar. I notice the OpenAL documentation is quite involved. Can someone point me to something really simple in order to just get the start/stop functionality of a .wav file?
OpenAL may not be the simplest choice. If you use a gui-framework like QT, check for what their ecosystems provide for playing sound (e.g. qsound). Another choice may be Allegro which may feel more straightforward.
For OpenAL, there is a working example to play a wav in the example repositories. Playback can be paused using alSourcePause.
Edit:
For choosing libraries I like to consult Awesome-cpp. The simple_playback.c example of mini_al looks extremely straightforward. Simply use mal_device_stop(&device) to pause the running playback and mal_device_start(&device) to continue. Works perfectly on my machine and seems very portable.

How to play two or more sounds at the same time

I'm quite new to programming, I'm in my first year of my programming; CS1B.
I'm making a text adventure game in Visual Studio 2015, I want to use two sounds, like music in the background and a sound effect.
But whenever I try to do so, the first sound stops when the second sound starts. I'm using the PlaySound() function to do my sounds. I know about FMOD and DirectSound but I don't really know how to set it up.
I tried looking through how to do this, but the stuff I find is really hard for me to follow. Any help will be good. Thanks.
You can separate your sounds into SFX and music (which uses the music system). This way, you can control your background music separately (as well as make use of their music-specific features) from your sound effect events.
In general, you need to have a separate event/sound instance for each of the sound you are playing. If you only have one, it could get 'stolen' and this might be the most possible reason for your problem. It could also be because of your channels.
I am not sure if you are using FMOD Studio or FMOD Designer/FMOD Ex, though, so it really depends on which one you are using. In any case, if you are just starting, I recommend reading up on the FMOD Event System - Best Practices For Programmers
documentation which you can get from the Internet, as well as the FMOD Ex Programmer's API documentation (you will have to have an account to download this, but it's worth it.)
Good luck.
PlaySound only has a single 'voice' to work with, so it can't play more than one sound at a time.
To play more than one sound at a time requires a real-time mixer, so you need to use something else: XAudio2, FMOD, Wwise, MSS, OpenAL, etc.
DirectSound is deprecated and hasn't been updated since 2000. It still exists for BackCompat but has a number of restrictions and other subtle legacy behaviors. The only reason anyone should still be using DirectSound at all is if they are targeting Windows XP.
If you are using C++, you should look at DirectX Tool Kit for Audio which is a simple C++ wrapper for XAudio2.
I would guess simple multithreading would do:
#include <thread>
...
thread playsound(&playsound, this, <other params>);
playsound.detach();
thread othersound(&playsound, this, <other params>);
othersound.detach();
Good luck

Qt5 QMediaPlayer use codecs that are delivered with the program

I need to write a (portable) program that, amongst other things, is able to play a video file. The QMediaPlayer in Qt5 seems to be the obvious choice, but apparently it only uses codecs that are installed on the executing machine.
All I need for the video player is being able to play at least one video format on all (windows) machines without installing anything, and communicate the current status to the main program (currrent time in ms, playing/paused).
Specific question: Can someone give me some hints where I have to look? How can I implement a video codec into my program so that QMediaPlayer can reliably play videos on all machines? Alternatively (but less prefered): How to (easily) implement another video player into the program that is capable of doing the task?
I've already tried my luck with libVLC and one or two other options but it never worked so far and it seems to make my program way more complex than it has to be.
QMediaPlayer can play any format that the platform's media service supports. You should install the right codecs for a video type to be played successfully. So you have to install GStreamer codecs on Linux or if you are on Windows you should install something KLite Codecs.
May be it is good to consider using libraries like QtAV. It will work for most platforms and no additional codecs and plugins are required to install.
QtAV uses FFmpeg and has some interesting features like multiple renderers for 1 player, region of interest(video crop), custom filters, ... It also works fine for both Qt4 and Qt5.
QMediaPlayer uses the underlaying video framework of whatever machine you use, that means there's no way to portable write a player using such class. Your only solution, in my opinion, is switching to FFMpeg playback library, that uses native codecs instead.

Play and pause an .wav or .mp3 file on windows

I am working on a console application where i have to deal with multiple sound file with all of same type(.wav or .mp3). I have the option to chose between the two.
I can play the sound fine using PlaySound() function but i need some extra features.
I need pausing the sound and then play from the position where it paused last time.
OR
Set a time from where playing should start and time for which it should play.
1 of the two options will do the job.
I am working on windows with DEV C++ compiler.
Any help and ideas?
Use the Windows Media Player activeX control for anything more sophistcated than straight playing of a WAV file. It is far easier.
See "Using the Windows Media Player Control in a C++ Program" on MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd564580(v=vs.85).aspx