It is well known that Windows API PlaySound()
does not allow playback of 24-bit .wav files.
What is the shortest way to play a 24 bit .wav file in C / C++ with a Windows platform?
(if possible, without needing on third-party libraries)
You might be able to use mciSendString
, but I don't know for sure if it can play 24-bits WAV files, and if it can what the platform requirements are.
MCI works with simple string commands that are sent, using mciSendString
. To open a file and play it (error checking omitted for brevity):
mciSendString("open \"C:\\My Folder\\Sample.wav\" type waveaudio alias sampleSound", NULL, 0, NULL);
mciSendString("play sampleSound from 0", NULL, 0, NULL);
Note that the alias sampleSound
is a custom name you give to the opened resource, so you can refer to it later. You can close the resource using the string "close sampleSound"
, and there are many more commands and options to control playback.
If this does not work, then your best bet is to use DirectShow, which can utulise all the DirectShow filters installed on the system for playback. This SO answer Use Windows built in MP3 decoder to play audio? gives sample code.
Just for reference purpose, I also rewrote MicroVirus' answer into a Python code :
import ctypes
ctypes.windll.WINMM.mciSendStringW(u"open BASS.wav type waveaudio alias sample1", None, 0, None)
ctypes.windll.WINMM.mciSendStringW(u"play sample1 from 0", None, 0, None)
raw_input()
Works with 24-bit .wav files !
mciSendString
, but this post stackoverflow.com/questions/6032795/… seems to indicate that there could be problems with this as well. Have you taken a look at utilising DirectShow: stackoverflow.com/a/8121768/2718186 ? – MicroVirusmciSendString
? (I use Win7 so I don't mind if it won't work with WinXP). – Basj