65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.86/5 (4 votes)

Oct 15, 2002

2 min read

viewsIcon

115195

downloadIcon

3464

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();
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.