From a6a3ad0d66f819d54359137d7313fea31bb02e4e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 18 Jul 2015 00:29:19 +0200 Subject: [PATCH] Fix wxSound::Create() and IsOk() return values in wxOSX. Don't pretend that we created wxSound object successfully without actually doing it: this means that now passing an invalid (e.g. non-existent or using wrong format) file to wxSound::Create()/ctor will return false/result in IsOk() returning false later, just as in the other ports. It also means that playing a successfully created wxSound object won't give any error messages, as unexpectedly happened before. --- src/osx/core/sound.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/osx/core/sound.cpp b/src/osx/core/sound.cpp index db38bd381c..c251b29a36 100644 --- a/src/osx/core/sound.cpp +++ b/src/osx/core/sound.cpp @@ -34,9 +34,9 @@ class wxOSXAudioToolboxSoundData : public wxSoundData { public: - wxOSXAudioToolboxSoundData(const wxString& fileName); + explicit wxOSXAudioToolboxSoundData(SystemSoundID soundID); - ~wxOSXAudioToolboxSoundData(); + virtual ~wxOSXAudioToolboxSoundData(); virtual bool Play(unsigned flags) wxOVERRIDE; @@ -46,14 +46,12 @@ protected: void SoundCompleted(); SystemSoundID m_soundID; - wxString m_sndname; //file path bool m_playing; }; -wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(const wxString& fileName) : - m_soundID(0) +wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(SystemSoundID soundID) : + m_soundID(soundID) { - m_sndname = fileName; m_playing = false; } @@ -108,17 +106,6 @@ bool wxOSXAudioToolboxSoundData::Play(unsigned flags) m_flags = flags; - wxCFRef cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(m_sndname))); - CFStringNormalize(cfMutableString,kCFStringNormalizationFormD); - wxCFRef url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false)); - - OSStatus err = AudioServicesCreateSystemSoundID(url, &m_soundID); - if ( err != 0 ) - { - wxLogError(_("Failed to load sound from \"%s\" (error %d)."), m_sndname, err); - return false; - } - AudioServicesAddSystemSoundCompletion( m_soundID, CFRunLoopGetCurrent(), NULL, wxOSXAudioToolboxSoundData::CompletionCallback, (void *) this ); bool sync = !(flags & wxSOUND_ASYNC); @@ -149,7 +136,20 @@ bool wxSound::Create(const wxString& fileName, bool isResource) { wxCHECK_MSG( !isResource, false, "not implemented" ); - m_data = new wxOSXAudioToolboxSoundData(fileName); + wxCFRef cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(fileName))); + CFStringNormalize(cfMutableString,kCFStringNormalizationFormD); + wxCFRef url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false)); + + SystemSoundID soundID; + OSStatus err = AudioServicesCreateSystemSoundID(url, &soundID); + if ( err != 0 ) + { + wxLogError(_("Failed to load sound from \"%s\" (error %d)."), fileName, err); + return false; + } + + m_data = new wxOSXAudioToolboxSoundData(soundID); + return true; }