Click here to Skip to main content
Licence 
First Posted 14 Oct 2002
Views 85,806
Bookmarked 36 times

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

By | 25 Oct 2002 | Article
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

About the Author

lvidaguren



Argentina Argentina

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMp3 File don't work well. PinmemberJamesDK0:56 27 Nov '09  
GeneralSound problem Pinsusspopo8418:33 24 Apr '05  
GeneralRe: Sound problem Pinmemberlvidaguren12:05 17 May '05  
GeneralRe: Sound problem Pinmemberpopo8417:53 18 May '05  
GeneralRe: Sound problem Pinmemberpopo8417:54 18 May '05  
GeneralRe: Sound problem Pinmemberpopo8418:46 18 May '05  
Generalgreat :) PinmemberBrainB0ne4:03 18 Mar '04  
GeneralBug PinmemberGautam Dutta1:21 26 Sep '03  
GeneralRe: Bug Pinmemberlvidaguren2:39 26 Sep '03  
GeneralFound the Actually Problem PinmemberGautam Dutta23:28 26 Sep '03  
GeneralIt is hard to believe this still exists PinmemberRobert Valentino9:34 31 Oct '10  
GeneralTab Order PinmemberColleen7:56 5 Dec '02  
GeneralRe: Tab Order Pinmemberlvidaguren10:39 28 Apr '03  
GeneralNice app - works fine PinmemberSoliant8:36 17 Oct '02  
GeneralRe: Nice app - works fine Pinmember__Stephane Rodriguez__22:40 17 Oct '02  
GeneralRC trick PinmemberDalle2:02 16 Oct '02  
GeneralRe: RC trick Pinmemberlvidaguren2:27 16 Oct '02  
Generalresizing badly redraws Pinmember__Stephane Rodriguez__1:48 16 Oct '02  
GeneralRe: resizing badly redraws Pinmemberlvidaguren7:43 18 Oct '02  
GeneralFixed Pinmemberlvidaguren8:46 19 Oct '02  
GeneralVC6 compilation Pinmember__Stephane Rodriguez__1:46 16 Oct '02  
GeneralCannot compile Pinmember__Stephane Rodriguez__1:45 16 Oct '02  
GeneralRe: Cannot compile Pinmemberlvidaguren2:24 16 Oct '02  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 26 Oct 2002
Article Copyright 2002 by lvidaguren
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid