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.
This commit is contained in:
Vadim Zeitlin
2015-07-18 00:29:19 +02:00
committed by Vadim Zeitlin
parent a0afc17c75
commit a6a3ad0d66

View File

@@ -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<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(m_sndname)));
CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
wxCFRef<CFURLRef> 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<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(fileName)));
CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
wxCFRef<CFURLRef> 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;
}