Click here to Skip to main content
11,496,146 members (796 online)
Click here to Skip to main content

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

, 25 Oct 2002 92.2K 3.2K 37
The CSoundFileDlg Class allows you to use a file open dialog for sound files, but allows yow to hear the sound before open it.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

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.

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

Share

About the Author

lvidaguren

Argentina Argentina
No Biography provided

Comments and Discussions

 
GeneralMp3 File don't work well. Pin
JamesDK27-Nov-09 1:56
memberJamesDK27-Nov-09 1:56 
GeneralSound problem Pin
popo8424-Apr-05 19:33
susspopo8424-Apr-05 19:33 
GeneralRe: Sound problem Pin
lvidaguren17-May-05 13:05
memberlvidaguren17-May-05 13:05 
GeneralRe: Sound problem Pin
popo8418-May-05 18:53
memberpopo8418-May-05 18:53 
GeneralRe: Sound problem Pin
popo8418-May-05 18:54
memberpopo8418-May-05 18:54 
GeneralRe: Sound problem Pin
popo8418-May-05 19:46
memberpopo8418-May-05 19:46 
Generalgreat :) Pin
BrainB0ne18-Mar-04 5:03
memberBrainB0ne18-Mar-04 5:03 
GeneralBug Pin
Gautam Dutta26-Sep-03 2:21
memberGautam Dutta26-Sep-03 2:21 
GeneralRe: Bug Pin
lvidaguren26-Sep-03 3:39
memberlvidaguren26-Sep-03 3:39 
GeneralFound the Actually Problem Pin
Gautam Dutta27-Sep-03 0:28
memberGautam Dutta27-Sep-03 0:28 
GeneralIt is hard to believe this still exists Pin
Robert Valentino31-Oct-10 10:34
memberRobert Valentino31-Oct-10 10:34 
GeneralTab Order Pin
Colleen5-Dec-02 8:56
memberColleen5-Dec-02 8:56 
GeneralRe: Tab Order Pin
lvidaguren28-Apr-03 11:39
memberlvidaguren28-Apr-03 11:39 
GeneralNice app - works fine Pin
Soliant17-Oct-02 9:36
memberSoliant17-Oct-02 9:36 
GeneralRe: Nice app - works fine Pin
__Stephane Rodriguez__17-Oct-02 23:40
member__Stephane Rodriguez__17-Oct-02 23:40 
GeneralRC trick Pin
Dalle16-Oct-02 3:02
memberDalle16-Oct-02 3:02 
GeneralRe: RC trick Pin
lvidaguren16-Oct-02 3:27
memberlvidaguren16-Oct-02 3:27 
Generalresizing badly redraws Pin
__Stephane Rodriguez__16-Oct-02 2:48
member__Stephane Rodriguez__16-Oct-02 2:48 
GeneralRe: resizing badly redraws Pin
lvidaguren18-Oct-02 8:43
memberlvidaguren18-Oct-02 8:43 
GeneralFixed Pin
lvidaguren19-Oct-02 9:46
memberlvidaguren19-Oct-02 9:46 
GeneralVC6 compilation Pin
__Stephane Rodriguez__16-Oct-02 2:46
member__Stephane Rodriguez__16-Oct-02 2:46 
GeneralCannot compile Pin
__Stephane Rodriguez__16-Oct-02 2:45
member__Stephane Rodriguez__16-Oct-02 2:45 
GeneralRe: Cannot compile Pin
lvidaguren16-Oct-02 3:24
memberlvidaguren16-Oct-02 3:24 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 26 Oct 2002
Article Copyright 2002 by lvidaguren
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid