From 2eb5cb50bb923bd4d714db74e3aa5f001f666aca Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 18 Jul 2015 00:32:56 +0200 Subject: [PATCH] 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 is a backport of a788351eb6ecaa45f1fe91fb64b6e2b070e29697 from master) --- samples/sound/sound.cpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/samples/sound/sound.cpp b/samples/sound/sound.cpp index de18ca59cf..85dca32f96 100644 --- a/samples/sound/sound.cpp +++ b/samples/sound/sound.cpp @@ -81,6 +81,7 @@ public: private: bool CreateSound(wxSound& snd) const; + wxSound* TryCreateSound() const; wxSound* m_sound; wxString m_soundFile; @@ -975,6 +976,17 @@ bool MyFrame::CreateSound(wxSound& snd) const 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) { @@ -1054,12 +1066,9 @@ void MyFrame::OnPlaySync(wxCommandEvent& WXUNUSED(event)) { wxBusyCursor busy; if ( !m_sound ) - { - m_sound = new wxSound; - CreateSound(*m_sound); - } + m_sound = TryCreateSound(); - if (m_sound->IsOk()) + if (m_sound) m_sound->Play(wxSOUND_SYNC); } @@ -1067,12 +1076,9 @@ void MyFrame::OnPlayAsync(wxCommandEvent& WXUNUSED(event)) { wxBusyCursor busy; if ( !m_sound ) - { - m_sound = new wxSound; - CreateSound(*m_sound); - } + m_sound = TryCreateSound(); - if (m_sound->IsOk()) + if (m_sound) m_sound->Play(wxSOUND_ASYNC); } @@ -1089,10 +1095,7 @@ void MyFrame::OnPlayLoop(wxCommandEvent& WXUNUSED(event)) { wxBusyCursor busy; if ( !m_sound ) - { - m_sound = new wxSound; - CreateSound(*m_sound); - } + m_sound = TryCreateSound(); if (m_sound->IsOk()) m_sound->Play(wxSOUND_ASYNC | wxSOUND_LOOP);