Is there a way to play a sound without delay? - c++

My program is playing a sound PlaySound.
The program works fine and I can hear the sound, but when the song end, there is a delay for like 1 second, and then the song play again.
I asked Google, and he gave me this question - PlaySound() Delay
The guy who answerd , said that instead SND_SYNC we need to use SND_ASYNC, I listened to him and did it, but I can't hear anything.
Do you have any suggestions ?
Btw, this is the song I'm currently using for this project - Nyan Cat
I want that this song will be start again immediately, for the user to not hear that there is a Delay.
Final Code:
#include <iostream>
#include <Windows.h>
#include <string>
#pragma comment(lib, "winmm.lib")
int main()
{
std::string pathtosound = "C:\\Users\\roile\\Documents\\Dragonite\\nyan.wav";
while (true) {
PlaySound(pathtosound.c_str(), 0, SND_SYNC);
}
return 0;
}

The SND_LOOP flag is described as follows in Microsoft Docs:
The sound plays repeatedly until PlaySound is called again with the
pszSound parameter set to NULL. If this flag is set, you must also set
the SND_ASYNC flag.
Pay attention to the last sentence, hence the following code will probably work better:
#include <iostream>
#include <Windows.h>
#include <string>
#pragma comment(lib, "winmm.lib")
int main()
{
std::string pathtosound = "C:\\Users\\roile\\Documents\\Dragonite\\nyan.wav";
PlaySound(pathtosound.c_str(), 0, SND_ASYNC | SND_LOOP);
while (true) {
// Stop loop at some point
}
PlaySound(NULL, 0, 0); // Stop sample
return 0;
}

Related

What can I replace with the sleep command in the Windows variant of ncurses?

#include <curses.h>
#include <unistd.h>
#include <iostream>
int main() {
initscr();
mvaddstr(10, 10, "Hello, world");
refresh();
sleep(4);
endwin();
std::cout << "DONE\n";
}
I'm working on a project and I need to take down the curses windows for a while just to write a path to directory in cmd and then let the curses windows come back. I found this code on this site and tried to use sleep command in the code but it didn't work.
So if anyone knew how to solve this please write it down here. Thnx :)
napms is the (portable) curses function to use instead of sleep

MciSendString automatically plays AVI files at fullspeed

I'm trying to play avi files with mciSendString.
Here's my testing code :
#include <iostream>
#include <windows.h>
#include <string>
#pragma comment (lib, "winmm.lib")
using namespace std;
int main(int argc, char* argv[])
{
mciSendString("open D:\\something.avi type avivideo alias myAlias", NULL, 0, NULL);
mciSendString("play myAlias fullscreen from 0", NULL, 0, NULL);
return 0;
}
The thing is it plays the file at fullspeed, as if I had done :
mciSendString("set myAlias speed 0", NULL, 0, NULL);
I tried to set the speed to 1000, to no avail.
Some weird things :
If I set the speed to 2000 for example, the file does play at double speed instead of fullspeed
For some files, it is displayed at normal speed and I can't seem to figure out why.
I tried to find out where it comes from with GSpot (https://en.wikipedia.org/wiki/GSpot) because I already had this software for previous reasons but I couldn't find any correlation.
Please understand that my knowledge in media files is close to nonexistent, so try keeping it as simple as possible.
Thank you.

Can't launch website in C++

Trying to launch a website in Visual C++ 2010 Express, every researched solution has not worked. So I gather I need more in-depth assistance. Here's my code:
#include <stdafx.h>
#include <stdio.h>
#include <iostream>
#include <io.h>
#include <string>
#include <string.h>
#include <conio.h>
#include <windows.h>
using namespace System;
using namespace std;
int main()
{
char name[240];
cout<<"\nHello, Please enter your name: ";
cin.getline(name,240);
cout<<"\nHello "<<name <<", your ID has been confirmed!";
cout<<"\nContinuing to launch website 'xyz' now.";
// system("C:\\Program%Files\\Google\\Chrome\\Application\\chrome.exe");
ShellExecute, TEXT("open"), TEXT(c:\\program
files\\google\\chrome\\application\\chrome.exe), NULL, NULL, SW_SHOWNORMAL;
goto end;
end:
cout<<"\n\nProgram completed, Pess any key to exit. ";
_getch();
return 0;
}
Like this.
ShellExecute(NULL, TEXT("open"), TEXT("c:\\program files\\google\\chrome\\application\\chrome.exe"), NULL, NULL, SW_SHOWNORMAL);
You had missing parens, missing double quotes and quite possibly spurious characters between 'program' and 'files' in your path. You also had a missing parameter to ShellExecute.
Reading a book and learning a minimum of C++ syntax would be a good idea. These are very basic errors.
system("\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" http://heise.de");
works for me.
See How to call an external program with parameters?

Running a C++ Console Program in full screen

How to run a C++ Console Program in full screen ? , using VS2008
Just tested this with cl fullscreen.cpp :
#include <iostream>
#include <windows.h>
#pragma comment(lib, "user32")
int main()
{
::SendMessage(::GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x20000000);
std::cout << "Hello world from full screen app!" << std::endl;
std::cin.get();
}
Unfortunatelly it had duplicated the text on the second monitor :)
try:
#include <iostream>
using namespace std;
int main(){
system("mode 650");
system("pause");
return 0;
}
#include <windows.h>
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE),CONSOLE_FULLSCREEN_MODE,0);
There are not a lot of video adapters around these days that still support this. Run cmd.exe and press Alt+Enter. If you get a message box that says "This system does not support fullscreen mode" then you're done. If it does switch to full screen then you can use SetConsoleDisplayMode() in your main() function. Of course, you don't know what your customer's machine is like, best not to pursue this.
That's what I'm using:
system("mode con COLS=700");
ShowWindow(GetConsoleWindow(),SW_MAXIMIZE);
SendMessage(GetConsoleWindow(),WM_SYSKEYDOWN,VK_RETURN,0x20000000);
It removes the scrollbar :D
For full screen windowed mode: ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
Just a workaround: You could use some sort of earlier DOS video modi, for example...
asm
{
mov ax, 13h
push bp
int 10h
pop bp
}
...to have a resolution of 320x200 pixels.
But I'm not sure if this would work for a windows application... Probably not!
Just add this line (anywhere) before your output,
system("mode 650");
Such as,
#include<bits/stdc++.h>
using namespace std;
int main(){
system("mode 650");
cout<<"Hey, this words are shown in full screen console! "<<endl;
return 0;
}

C++ Auto Format

I am trying to create a program that auto formats my f:\ (USB Drive), and I have this code so far:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
int main ()
{
system ("format f:");
system("pause");
return 0;
}
Now, when I run that, it comes up, with me needing to press Y, then Enter. How can I simulate that?
As Tyler said in a comment, a batch file would be much easier, but this might work:
system( "echo y | format f:" )