PlaySound function

This is a discussion on PlaySound function within the Windows Programming forums, part of the Platform Specific Boards category; Hi! I have this function: Code: void PlaySoundFile (const char *SoundFile) { if (!PlaySound (SoundFile, NULL, SND_FILENAME | SND_ASYNC)) { ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question 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!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219

    Post

    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.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    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

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    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.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  6. #6
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    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;
    		}
    	}
    }

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    >>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.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Do a search for: mciSendString mp3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21