How to play a sound file specified by user with playsound()? - c++

How to play a sound file specified by user in an array using the PlaySound()? I'm using Windows7 and VC++ 2010.
Suppose I use the gets(song); statement to input the complete path of the song from user.

http://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx
PlaySound( song, NULL, SND_FILENAME );

Related

ShellExecuteA but I don't know the extension

std::string str1 = "C:\\Users\\user\\Desktop\\Notes";
ShellExecuteA(NULL, "open", str1.c_str(), NULL, NULL, SW_SHOWNORMAL);
The code isn't worth much.
I have an .mp3 in a DJ program. In one of the tags, it holds the filename of the associated video. All the videos are .mpeg.
With the DJ software's API, I can grab the tag. I know where the video files are (all in one folder). I can open the video with ShellExecuteA(), because whilst the tag might not contain the full filename+extension, I know the extension.
Now the problem - I want to start using .avi or .h254 or whatever. I don't know the extension anymore, and ShellExecuteA() needs an extension.
What can I do?
My guesses are:
If ShellExecuteA() returns an error (not sure it does), if it does I could brute-force it; is it .mpeg? Is it .avi? Is it .h264? etc...
Do a search in the known location with the filename missing the extension, and then grab the full filename with whatever it finds (all file names are unique, even excluding the extension).
I know I could add the extension in the .mp3 tag, but there are reasons why I'd rather not do that.
ShellExecute does not need extension. It needs the exact file name. If extensions are hidden in Windows Explorer - make them visible. If you don't know the extension for other reason, use FindFirst with wildcard * (Notes*) to find the full name.
FindFirstA got me the results I wanted
obviously there are more direct ways than sstream but other stuff is going on that isn't important to the actual problem
std::stringstream sstrm1, sstrm2;
sstrm1 << "C:\\Users\\user\\Desktop\\";
sstrm2 << sstrm1.str() << "Notes.*";
HANDLE hFind;
WIN32_FIND_DATAA data;
hFind = FindFirstFileA(sstrm2.str().c_str(), &data);
sstrm1 << data.cFileName;
ShellExecuteA(NULL, "open", sstrm1.str().c_str(), NULL, NULL, SW_SHOWNORMAL);

Why mciSendString cannot open my mp3 file?

I am trying to play MP3 audio in C++ Visual Studio 17.3.0, but keep getting MCIERROR 275 followed by 263.
My .mp3 file is in the same directory as my .cpp file.
My code goes something like this:
MCIERROR me = mciSendString(TEXT("open ""Music.mp3"" type mpegvideo alias mp3"), NULL, 0, NULL);
while(true){
me = mciSendString(TEXT("play mp3"), NULL, 0, NULL);
}
Have tried different .mp3 files, different directory, and different function for playing the sound (PlaySound()), which gave me a very similar result/error.
What could be the cause of my problem?
The first is to open:
mciSendString("open Summer.mp3 alias song",NULL,0,NULL)
Add the relative path or absolute path of the file after open (depending on the relative position of the music you play and your program)
We could understand alias as replacing your music name with the name after alias, which is convenient for us to carry out subsequent operations, only need to enter your alternative name (to save the trouble if the song name is long)
The last three parameters can be written as I do, because we are just simply playing music, so there is no need to go into details.
Next is to play:
mciSendString("play song repeat",NULL,0,NULL);
play+music name (or an alternative name after alias)+[play selection]
Playback options include repeat, wait.
repeat means to repeat the song.
wait means that the function does not return until the song has finished playing.

Stopping an exe file from being run C++

I am creating an application to manage other applications or exe files on a user's computer, and stop them from accessing them at certain times (like ColdTurkey's application blocking feature).
The way I am trying to do this has not been working so far - I attempted to do this by opening the file dwShareMode set to 0 using the CreateFile function. This seems to work for files such as text files and does not allow the file to be opened, however this is not the case if I try and do this same approach on exe files, and the user is free to open the file.
I assume that exe files are not 'read' in the same way by Windows as a text file is read by notepad and that that means setting the dwShareMode to 0 does not affect it being opened, however I do not know what the difference between these are. Any help would be appreciated.
Code here (for the text file):
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
HANDLE test;
test = CreateFile("test.txt",
GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
cout << "press enter to stop blocking application: ";
string b;
getline(cin, b);
cout << endl;
CloseHandle(test);
return 0;
}
Your code works fine for me to block execution of the file. You do need to specify OPEN_EXISTING instead of CREATE_NEW (because you're not trying to create a new file here).
Not a windows expert -- I'm used to Unix/Linux and use the Cygwin package so I can program "in Unix" on my Windows desktop -- but it looks to me like you need to set the lpSecurityAttributes parameter, the one that comes after dwShareMode.
I think the following page might be helpful:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364399(v=vs.85).aspx

C++ Open file seems to ruin file after run

Simply put, I double click on image1 in its file and it opens. I run the code bellow to open image1 and nothing comes up. So I go into the file with image1 again, double click on it, and windows photo viewer said, "Windows Photo Viewer can't display this picture because the file is empty." I did this with two other test images and the same thing is happening. Nothing important has been lost but this method seems to be erasing whichever file it tries to open and I'm very curious as to why and how I can fix it.
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
void main()
{
std::ofstream imagetest;
imagetest.open("C:\\Users\\Filepath\\image1.jpg");
std::chrono::milliseconds dura(2000);
std::this_thread::sleep_for(dura);//Kept the sleep in because I didn't know if having the console up would affect the file/image from opening.
}
C++ is at lower level than scripts. open does not mean START.
You will have to execute a batch script with START C:\Users\Filepath\image1.jpg.
Or to learn many more libraries to do that in C++...
ofstream stands for “output file stream”. In addition to creating files that doesn’t exist, it also erases the contents of files that do exist. So you are opening an existing file for writing, and blowing away its contents in the process. You probably want ifstream, “input file stream”, for reading.
If you want to “open” the file in the sense of launching the default Windows application to read the file, you can use the Windows start command via system:
system("start \"C:\\Users\\Filepath\\image1.jpg\"");
Or the Windows ShellExecute API:
#include <windows.h>
ShellExecute(
NULL,
"open",
"C:\\Users\\Filepath\\image1.jpg",
NULL,
NULL,
SW_SHOWNORMAL
);
First,
std::ofstream imagetest;
is using the kernel to open the file for reading the file data..
this is probably what is corrupting the file from "opening" when you double click on it in windows
if you want to have windows open the image for viewing using the default application then you need a different method call because ofstream.open is not what you want.
try:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
ShellExecute(NULL,"open","C:\\Users\\Filepath\\image1.jpg",NULL,NULL,SW_SHOW);
If you open a file stream for WRITE, then it will wipe all the content of that file, just like when you do that on a txt file. So you would always want to open the stream for read mode if you don't want that to happen

PlaySound() mmslib does not play existing sound

EDIT: Solved. Simply the .wav file was not accepted by Windows. I plucked one of Windows own files and renamed it to what my previous file was called and it plays without problem.
I don't know why this can't play the existing file. Windows gives a chime in that something is wrong but I have no clue what.
I added a check right before to make sure it exists. I have also tried absolute paths.
string wavPath = "c:\\frog.wav";
struct stat stFileInfo;
bool blnReturn = (stat(wavPath.c_str(), &stFileInfo) == 0); //this returns true
FILE* fp = fopen(wavPath.c_str(), "r");
if (fp) {
fclose(fp); //this triggers
}
PlaySound(wavPath.c_str(), NULL, SND_FILENAME | SND_ASYNC); //m_hinstance
//C:\\Users\\Wollan\\My Code\\A\\Debug\\frog.wav
//TEXT("frog.wav")
//TEXT(wavPath.c_str())
//(LPCSTR)"frog.wav¨
The file plays fine in WMP.
This following code perfectly works:
PlaySound(L"C:\\Windows\\Media\\Cityscape\\Windows Balloon.wav", 0, SND_FILENAME );
Adding SND_ASYNC fails to play.
The documentation says:
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.
And:
PlaySound searches the following directories for sound files: the
current directory; the Windows directory; the Windows system
directory; directories listed in the PATH environment variable; and
the list of directories mapped in a network. If the function cannot
find the specified sound and the SND_NODEFAULT flag is not specified,
PlaySound uses the default system event sound instead.
No other case is specified for this outcome.
Therefore, that you hear a chime indicates that the file is not being found, despite your assurances to the contrary.
I'd double-check the result of that stat call; I can't even find stat in the documentation; it doesn't appear to be part of Windows.
PlaySound(L"C:\Windows\Media\Cityscape\Windows Balloon.wav", 0, SND_FILENAME );
Adding SND_ASYNC fails to play.
This answer is right!
It is because the ASYNC mode plays the music after the function returns.
Your code may have exited before the music plays.
use int x, cin>>x, after PlaySound function, you will find that it works well.