Click here to Skip to main content
15,881,139 members
Articles / Desktop Programming / MFC
Article

CSoundFileDlg - An Open/Save dilaog for sound files with Preview (or prelisten)

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
25 Oct 20022 min read 113.9K   3.5K   37   23
The CSoundFileDlg Class allows you to use a file open dialog for sound files, but allows yow to hear the sound before open it.

Sample Image

Introduction

Using CSoundFIleDlg you can listen to a sound file before actually opening it, kind of like when choosing Windows' sounds. I wanted to be able to do that in a program I was writing; it was a timer so I didn't immediately need the sound, and I found it interesting working out what sound to play when a selection had been made. Since I couldn't find the code to do it anywhere I wrote it, I'm not aware if someone has written any similar code.

How to use the classes provided

Include the following files in your project:

  • SoundFileDlg.h
  • SoundFileDlg.cpp
  • PreListenWnd.h
  • PreListenWnd.cpp

And add the line:

"#include ""SoundFileDlg.rc"" //SoundFileDlg template resource\r\n"

in <YourProject.rc> in the TEXTINCLUDE part, this way whenever the resources are changed your resource include won't be deleted.

or

#include SoundFileDlg.rc //SoundFileDlg template resource

In your <res/YourProject.rc2>

or  

just copy the template dialog code from SoundFileDlg.rc to your resource file. (the SoundFileDlg.rc file only has the template window resource)

IMPORTANT Link the Vfw32.lib Library

The usage is the same as a regular CFileDialog

What's the difference with a normal CFileDialog

The difference is very simple the dialog has a play button and a slider; actually it's a preview window (CPreListenWnd) .

When the CSoundFileDlg is created, the preview window is created, (with no file).

void CSoundFileDlg::OnInitDone()
{
    CFileDialog::OnInitDone();
    CRect cr,winRect,prevWndRect;    
    CWnd * pWnd = this->GetParent();
    pWnd->GetWindowRect(winRect);
    pWnd->ScreenToClient(winRect);
    SetWindowPos(&wndBottom,0/*::GetSystemMetrics(SM_CXSCREEN)*/,
                      0/*::GetSystemMetrics(SM_CYSCREEN)*/,0,0,0);
    if(m_bOpenFileDialog)
    {
        ...
        //get the position and size of the PreListenWnd
        int newWidth = winRect.Width()*2/3,margin=20,newHeight=24;

        prevWndRect.bottom = winRect.bottom - margin;
        prevWndRect.top = prevWndRect.bottom - newHeight;
        prevWndRect.left = winRect.left + (winRect.Width()-newWidth)/2;
        prevWndRect.right = prevWndRect.left + newWidth;
        //Pre-Listen Window 
        m_cwndPreview.Create(pWnd,WS_CHILD | MCIWNDF_NOMENU,prevWndRect,"");
        ...
    }
}

When the selected file is changed the file is opened in the prelisten window

void CSoundFileDlg::OnFileNameChange()
{
    if(!m_bOpenFileDialog){
        CFileDialog::OnFileNameChange();
        return;
    }
    CString Path = GetPathName();
    CFile chkFile;
    if(!chkFile.Open(GetPathName(),CFile::modeRead|CFile::shareDenyNone))
        m_cwndPreview.Close();
    else
    {
        chkFile.Close();
        m_cwndPreview.Open((LPCSTR)Path,0);
    }
    //Redraw preview window because if not a strange line is drawn after 
    // Open or Close
    ResizePreviewWindow();
    if(m_chkAutoPrev.GetCheck())
        m_cwndPreview.Play();
}

CPreListenWnd reference

This preview it's a very simple class, derivated from CWnd that has some of MCIWnd.

Construction/Initialization

CPreListenWnd();<br>
BOOL Create(CWnd* pParentWnd, DWORD dwStyle, const RECT& rect, LPCSTR szFile=NULL);

File and Device Management

LONG Open(LPCSTR szFileName, DWORD flags=0);
LONG Close();

Playback Options

LONG Play();
LONG Stop();

Compatibility

Tested using VC++ 7 with MFC on Windows XP. Should work fine on other Windows OSs (95/98/NT/ME/200) also. The Demo File was VC++ 7, converted to VC++6 by the VC++7 to VC++6 project converter by Stephane Rodriguez, I didn't test on VC6 so I don't know if there is any incompatibility, I think it works fine.

Conclusion

Before I wrote this code I thought everything had been written (and maybe it has), but i didn't find this one anywhere, I think it's not a bad idea, at least I find it useful. Any suggestions, improvements or bugs detected are welcome.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMp3 File don't work well. Pin
JamesDK27-Nov-09 0:56
JamesDK27-Nov-09 0:56 
GeneralSound problem Pin
popo8424-Apr-05 18:33
popo8424-Apr-05 18:33 
GeneralRe: Sound problem Pin
lvidaguren17-May-05 12:05
lvidaguren17-May-05 12:05 
GeneralRe: Sound problem Pin
popo8418-May-05 17:53
popo8418-May-05 17:53 
GeneralRe: Sound problem Pin
popo8418-May-05 17:54
popo8418-May-05 17:54 
GeneralRe: Sound problem Pin
popo8418-May-05 18:46
popo8418-May-05 18:46 
Generalgreat :) Pin
BrainB0ne18-Mar-04 4:03
BrainB0ne18-Mar-04 4:03 
GeneralBug Pin
Member 13054826-Sep-03 1:21
Member 13054826-Sep-03 1:21 
GeneralRe: Bug Pin
lvidaguren26-Sep-03 2:39
lvidaguren26-Sep-03 2:39 
GeneralFound the Actually Problem Pin
Member 13054826-Sep-03 23:28
Member 13054826-Sep-03 23:28 
GeneralIt is hard to believe this still exists Pin
Robert Valentino31-Oct-10 9:34
Robert Valentino31-Oct-10 9:34 
GeneralTab Order Pin
Colleen5-Dec-02 7:56
Colleen5-Dec-02 7:56 
GeneralRe: Tab Order Pin
lvidaguren28-Apr-03 10:39
lvidaguren28-Apr-03 10:39 
GeneralNice app - works fine Pin
TigerNinja_17-Oct-02 8:36
TigerNinja_17-Oct-02 8:36 
GeneralRe: Nice app - works fine Pin
Stephane Rodriguez.17-Oct-02 22:40
Stephane Rodriguez.17-Oct-02 22:40 
GeneralRC trick Pin
Anders Dalvander16-Oct-02 2:02
Anders Dalvander16-Oct-02 2:02 
GeneralRe: RC trick Pin
lvidaguren16-Oct-02 2:27
lvidaguren16-Oct-02 2:27 
Generalresizing badly redraws Pin
Stephane Rodriguez.16-Oct-02 1:48
Stephane Rodriguez.16-Oct-02 1:48 
GeneralRe: resizing badly redraws Pin
lvidaguren18-Oct-02 7:43
lvidaguren18-Oct-02 7:43 
GeneralFixed Pin
lvidaguren19-Oct-02 8:46
lvidaguren19-Oct-02 8:46 
GeneralVC6 compilation Pin
Stephane Rodriguez.16-Oct-02 1:46
Stephane Rodriguez.16-Oct-02 1:46 
GeneralCannot compile Pin
Stephane Rodriguez.16-Oct-02 1:45
Stephane Rodriguez.16-Oct-02 1:45 
GeneralRe: Cannot compile Pin
lvidaguren16-Oct-02 2:24
lvidaguren16-Oct-02 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.