Don't keep using invalid wxSound object in the sound sample.

If creating a sound object fails, delete it to ensure that it is recreated
later.

This fixes a minor bug: previously, if an invalid file was used as sound file,
only the first attempt to play it resulted in an error and all the subsequent
ones were just silently ignored. Now every attempt to play an invalid file
results in an error message, as expected.
This commit is contained in:
Vadim Zeitlin 2015-07-18 00:32:56 +02:00 committed by Vadim Zeitlin
parent 605149ed07
commit a788351eb6

View File

@ -81,6 +81,7 @@ public:
private: private:
bool CreateSound(wxSound& snd) const; bool CreateSound(wxSound& snd) const;
wxSound* TryCreateSound() const;
wxSound* m_sound; wxSound* m_sound;
wxString m_soundFile; wxString m_soundFile;
@ -975,6 +976,17 @@ bool MyFrame::CreateSound(wxSound& snd) const
return snd.Create(m_soundFile); return snd.Create(m_soundFile);
} }
wxSound* MyFrame::TryCreateSound() const
{
wxSound* const sound = new wxSound;
if ( !CreateSound(*sound) )
{
delete sound;
return NULL;
}
return sound;
}
void MyFrame::NotifyUsingFile(const wxString& name) void MyFrame::NotifyUsingFile(const wxString& name)
{ {
@ -1054,12 +1066,9 @@ void MyFrame::OnPlaySync(wxCommandEvent& WXUNUSED(event))
{ {
wxBusyCursor busy; wxBusyCursor busy;
if ( !m_sound ) if ( !m_sound )
{ m_sound = TryCreateSound();
m_sound = new wxSound;
CreateSound(*m_sound);
}
if (m_sound->IsOk()) if (m_sound)
m_sound->Play(wxSOUND_SYNC); m_sound->Play(wxSOUND_SYNC);
} }
@ -1067,12 +1076,9 @@ void MyFrame::OnPlayAsync(wxCommandEvent& WXUNUSED(event))
{ {
wxBusyCursor busy; wxBusyCursor busy;
if ( !m_sound ) if ( !m_sound )
{ m_sound = TryCreateSound();
m_sound = new wxSound;
CreateSound(*m_sound);
}
if (m_sound->IsOk()) if (m_sound)
m_sound->Play(wxSOUND_ASYNC); m_sound->Play(wxSOUND_ASYNC);
} }
@ -1089,10 +1095,7 @@ void MyFrame::OnPlayLoop(wxCommandEvent& WXUNUSED(event))
{ {
wxBusyCursor busy; wxBusyCursor busy;
if ( !m_sound ) if ( !m_sound )
{ m_sound = TryCreateSound();
m_sound = new wxSound;
CreateSound(*m_sound);
}
if (m_sound->IsOk()) if (m_sound->IsOk())
m_sound->Play(wxSOUND_ASYNC | wxSOUND_LOOP); m_sound->Play(wxSOUND_ASYNC | wxSOUND_LOOP);