0

I Wanted To Make A Simple Piano Using C++. I Will Check If User Pressed A Key On Keyboard And Play The Piano Sound. Key Sounds Are Downloaded On Desktop And They Are .mp3 Files. So Just For Test I Wrote This Code :

#include "MyTestCodes.h"
//MyTestCodes.h Includes
/*
#include <iostream> //Input/Output Stream For Console
#include <string>   //For String
#include <fstream>  //File Stream 
#include <vector>   //Changable Index Structure
#include <Windows.h>//For System Commands
#include <algorithm>//Easy Sorting For Arrays/Vectors ...
#include <iomanip>  //For Outputting With Given Precision
#include <cmath>    //Easy Math Functions
#include <stdio.h>  //Standard Input/Output For Given Types
#include <conio.h>  //For Getting Key Presses
#include <ctime>    //For Getting Real Time
#include <filesystem>
#include <mmsystem.h>
#include <Mmsystem.h>
#include <mciapi.h>
*/

using namespace std;

#pragma comment(lib, "Winmm.lib")

int main(int argc, char* argv[])
{
    PlaySound("C:/Users/niko/Desktop/key01 F.mp3", NULL, SND_FILENAME);
    // Tried With "\\" Too 
}   

I Also Tried Without MyTestCodes.h And Include Other Libraries And Header Files By Myself (I Wrote MyTestCodes.h By Myself Too). I Mostly Included Those :


#include <Windows.h>
#include <mmsystem.h> 
#include <Mmsystem.h>
#include <mciapi.h>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Note that the dup states that Windows does not support MP3 playback. Also, capitalising every letter of sentences makes them more difficult to read, so please avoid that in future. – Ken Y-N Apr 24 '23 at 07:15
  • @KenY-N My Code Does Not Give Error. It Compiles Succesfully, But Does Not Play Sound I Need – Merd Ceferzade Apr 24 '23 at 07:16
  • 1
    You Need To Read The Entire Answer In The Duplicate Question, Including The Bit About MP3s, Which Is Also In The Advice @KenY-N Gave You. I Am Writing This Way In Case You Cannot Read Sentences That Use Correct Capitalization. – paddy Apr 24 '23 at 07:21
  • Yes, because it is a run-time error - `PlaySound()` should return `FALSE` if you try to play a file format it doesn't support, so please check the return value. – Ken Y-N Apr 24 '23 at 07:22
  • 1
    @KenY-N On 'cout << PlaySound("C:\\Users\\niko\\Desktop\\key01 F.mp3", NULL, SND_FILENAME);' It Gave Me 1, Means It Is True, File Format Is Supported, But Still Gives Error Sound – Merd Ceferzade Apr 24 '23 at 07:26
  • 1
    Presumably these files are really short and just contain a single note? You'd be much better off using uncompressed WAV files, due to the overheads of the MP3 file format they probably aren't even much smaller than the WAVs – Alan Birtles Apr 24 '23 at 07:28
  • 1
    PlaySound can't play MP3 files. Only WAV files. The WAV file can be PCM or encoded with a compression to use a codec at runtime. I believe you can use a tool such as ffmpeg to convert MP3 into a compressed WAV file. But if you just want a simple solution, make your WAV file with uncompressed PCM bytes. – selbie Apr 24 '23 at 07:31
  • Thanks For Help Everyone. I Turned It To .wav, And Worked. Have A Nice Day – Merd Ceferzade Apr 24 '23 at 07:38
  • please don't edit answers into the question. @selbie what was wrong with the duplicates? – Alan Birtles Apr 24 '23 at 09:56

0 Answers0