-
PlaySound function
Hi!
I have this function:
Code:
void PlaySoundFile (const char *SoundFile)
{
if (!PlaySound (SoundFile, NULL, SND_FILENAME | SND_ASYNC))
{
printf ("Error occured!");
}
}
If this function is run in win98, the sound file will not be played. There's always that message "error occured!". Why is that so? Should I initialize sound device? How do I do that, if I have to?
And, one more thing. Will I'll be able to play .mp3 files with this function? If not, which function do play .mp3 files and how to use it.
Thanks!
-
This is the information in the remarks about PlaySound() from MSDN. Also if their is to be a special function to play MP3 files then I do not know of it. So you may be listing the sound file in the char* incorrectly. So you should try the actual path to the file if your not doing so yet.
The sound specified by pszSound must fit into available physical memory and be playable by an installed waveform-audio device driver. 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. For more information about the directory search order, see the documentation for the OpenFile function.
If it cannot find the specified sound, PlaySound uses the default system event sound entry instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE.
-
you can play mp3's by using DirectX (Direct show)
Sunlight has a nice tutorial about it.
You can you put the code in a function or a class (last one will be better if you will add several functions like stop pause; volume , balance, ...)
edit: also, dont forget a clean up function ;)
-
I forgot to mention that the function PlaySoundFile works in WinXP. Path to the file is correct. That's for sure. So, what could be wrong? Any ideas?
About the .mp3 files, I need a function that plays them in console. So, no DirectX and stuff like that.
-
Then that goes to my post earlier.
The sound specified by pszSound must fit into available physical memory and be playable by an installed waveform-audio device driver.
You obviously do not have a proper driver to play mp3 files that is registered in the registry. So you may want to work around it with DirectX as suggested by maes.
-
Here's an example on how to play an mp3 in console.
The only thing you need is the directX SDK from microsoft.
with g_pMediaControl, you can control the mp3 (stop, pause, resume, etc.)
Just copy past the code in a console workspace and it should work.
Code:
#include <stdio.h>
#include <conio.h>
#include <dshow.h>
#pragma comment(lib, "amstrmid.lib")
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
IMediaControl *g_pMediaControl;
IMediaSeeking *g_pMediaSeeking;
IGraphBuilder *g_pGraphBuilder;
IBaseFilter *pSource;
bool PlayMP3(char filename[255])
{
LONGLONG llPos = 0;
IPin *pPin;
WCHAR wFilename[MAX_PATH];
#ifndef UNICODE
MultiByteToWideChar(CP_ACP,0,filename,-1,wFilename,MAX_PATH);
#else
lstrcpy(wFilename,filename);
#endif
::CoInitialize(NULL);
CoCreateInstance(CLSID_FilterGraph, NULL,CLSCTX_INPROC, IID_IGraphBuilder, (void **)&g_pGraphBuilder);
g_pGraphBuilder->QueryInterface(IID_IMediaControl,(void **)&g_pMediaControl);
g_pGraphBuilder->AddSourceFilter(wFilename, wFilename, &pSource);
pSource->FindPin(L"Output", &pPin);
g_pGraphBuilder->Render(pPin);
pPin->Release();
g_pGraphBuilder->QueryInterface(IID_IMediaSeeking, (void **)&g_pMediaSeeking);
g_pMediaSeeking->SetPositions(&llPos, AM_SEEKING_AbsolutePositioning, &llPos, AM_SEEKING_NoPositioning);
g_pMediaControl->Run();
return true;
}
int CleanUp()
{
SAFE_RELEASE(pSource);
SAFE_RELEASE(g_pMediaControl);
SAFE_RELEASE(g_pGraphBuilder);
SAFE_RELEASE(g_pMediaSeeking);
return 0;
}
int main()
{
int key;
for(;; )
{
system("cls"); //yeah yeah, I know, but this is only an example ;)
printf("1 :Play\n");
printf("2 :Quit\n");
key=getch();
switch(key)
{
case '1':
PlayMP3("my.mp3"); //!! enter the the correct path for your mp3
break;
case '2':
CleanUp();
return 0;
}
}
}
-
I tried my function with .wav files and it is not working. The sound in Win98 is working but this function fails to do it's job.
And I don't want directX for playing mp3's. I want pure win32 api.
-
>>I want pure win32 api.
To my knowledge not possible.
Maybe you can use mediaplayer to do it for you or you'll have to find a free library that can decode the mp3 for you. I thought Xing had an open source library to do that, but I'm not sure.
-
Do a search for: mciSendString mp3