PlaySound how to pause
Aug 15, 2012 at 4:44pm UTC
I am trying to make a program that plays music and
I am not able to make the user pause the music and be able to play where they left off.
This is what I have so far but I still
need to know how to pause the music.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
#include <mmsystem.h>
#pragma comment (lib, "winmm.lib")
using namespace std;
int main(){while ( 1 ){
long int A;
PlaySound(TEXT("C:\\M0.wav" ), NULL, SND_ASYNC|SND_FILENAME|SND_LOOP);
cout << "\t0 = EXIT\n"
"\t1 = STOP\n"
"\t2 = PAUSE\n"
"\t3 = PLAY\n" ;
cin >> A;
system ("CLS" );
if (A == 0){ exit(EXIT_SUCCESS); }
if (A == 1){ PlaySound(0, 0, 0); cout << "To restart music: " ;system ("PAUSE" ); }
if (A == 2){ }
if (A == 3){ }
}
}
I know there are some include files that I don't need but I might use them later.
My question: How do I pause the music?
Aug 15, 2012 at 6:29pm UTC
If you need to be able to pause the music, you will have to switch which API you are using. PlaySound does not support this functionality.
The easiest to use winmm call that does is probably mciSendString. Should be along the lines of:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
mciSendString("open C:\\M0.wav alias MY_SND" );
mciSendString("play MY_SND" );
// etc
mciSendString("pause MY_SND" );
// etc
mciSendString("resume MY_SND" );
// etc
mciSendString("stop MY_SND" );
// etc
See MSDN for information about the mciSendString function and the various MCI command strings. I can't remember the actual signature of mciSendString, so the other unused params will all need default values in real code!
Note this is a rather old school approach, but should do what you want.
Last edited on Aug 18, 2012 at 10:30am UTC
Aug 15, 2012 at 6:31pm UTC
Is there any way to do it with playsound?
Aug 15, 2012 at 7:14pm UTC
andywestken wrote:PlaySound does not support this functionality.
Forseth11 wrote:Is there any way to do it with playsound?
Well, no; because it does not support this functionality.
What's the issue with not doing it with playsound, anyway? If something else works you should just use it, surely?
Aug 16, 2012 at 5:35pm UTC
ok
Aug 16, 2012 at 5:37pm UTC
It gives me errors is there an include file or something?
Aug 18, 2012 at 8:37am UTC
According to the MSDN entry for mciSendString, the header is mmsystem.h, which you already have?
What errors are you getting?
Note the fragment I posted earlier was for illustrative purposed only. As the MSDN entry says, the signature of mciSendString
1 2 3 4 5 6
MCIERROR mciSendString(
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);
so the real code should be
mciSendString("pause MY_SND" , NULL, 0, NULL);
etc.
Last edited on Aug 18, 2012 at 10:30am UTC
Topic archived. No new replies allowed.