How does one go about...

How does one go about Playing a music file that they picked and playing it in the default player?

I already have this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
string wavfile = "C:\\Documents and Settings\\Jason\\My Documents\\Jason\\My Music\\Dummy.wav";
cout<<"Enter the sound file's directory\n"
"EX: C:\\Documents and Settings\\Jason\\My Documents\\Jason\\My Music\\Dummy.wav\n> ";
cin>>wavfile;
if ((int)ShellExecuteA( NULL, "open", wavfile.c_str(), NULL, NULL, SW_SHOWNORMAL ) <= 32)
  cout << "fooey!\n";
return 0;}
Last edited on
Using system(), just

 
system( "start C:\\...\\Dummy.wav &" );

From straight C++ code, you can use ShellExecute(), which is specifically designed for this kind of thing:

 
#include <windows.h> 
1
2
3
string wavfile = "C:\\...\\Dummy.wav";
if ((int)ShellExecuteA( NULL, "open", wavfile.c_str(), NULL, NULL, SW_SHOWNORMAL ) <= 32)
  cout << "fooey!\n";

Hope this helps.
It does help! Thanks a lot @Duoas
now all I need to do is figure out how to use the default player
Last edited on
1) ShellExecute with open mode will use default program associated (the kind which will be launched if you double-click on file)
2) Windows kernel works with forward slashes perfectly starting with at least WinNT. So use them instead of backward ones which needs escaping. (Or at least use raw string literals).
Literally only place which does not support forward slashes properly is cmd console: it threats them as additional arguments unless whole line is wrapped in quotes (PowerShell works fine)
//Does not work
C:\Users\MiiNiPaa>dir C:/users

//Works fine
C:\Users\MiiNiPaa>dir "C:/users"
ok, cool. thanks!
Topic archived. No new replies allowed.