]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/sound.cpp
avoid setting initial position if it was not specified, broken in r70734
[wxWidgets.git] / src / osx / core / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/sound.cpp
3 // Purpose: wxSound class implementation using AudioToolbox
4 // Author: Stefan Csomor
5 // Modified by: Stefan Csomor
6 // Created: 2009-01-01
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #if wxUSE_SOUND
15
16 #include "wx/sound.h"
17
18 #if wxOSX_USE_AUDIOTOOLBOX
19
20 #ifndef WX_PRECOMP
21 #include "wx/object.h"
22 #include "wx/string.h"
23 #include "wx/intl.h"
24 #include "wx/log.h"
25 #include "wx/timer.h"
26 #endif
27
28 #include "wx/file.h"
29
30 #include "wx/osx/private.h"
31
32 #include <AudioToolbox/AudioToolbox.h>
33
34 class wxOSXAudioToolboxSoundData : public wxSoundData
35 {
36 public:
37 wxOSXAudioToolboxSoundData(const wxString& fileName);
38
39 ~wxOSXAudioToolboxSoundData();
40
41 virtual bool Play(unsigned flags);
42
43 virtual void DoStop();
44 protected:
45 static void CompletionCallback(SystemSoundID mySSID, void * soundRef);
46 void SoundCompleted();
47
48 SystemSoundID m_soundID;
49 wxString m_sndname; //file path
50 };
51
52 wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(const wxString& fileName) :
53 m_soundID(NULL)
54 {
55 m_sndname = fileName;
56 }
57
58 wxOSXAudioToolboxSoundData::~wxOSXAudioToolboxSoundData()
59 {
60 DoStop();
61 }
62
63 void
64 wxOSXAudioToolboxSoundData::CompletionCallback(SystemSoundID WXUNUSED(mySSID),
65 void * soundRef)
66 {
67 wxOSXAudioToolboxSoundData* data = (wxOSXAudioToolboxSoundData*) soundRef;
68
69 data->SoundCompleted();
70
71 if (data->IsMarkedForDeletion())
72 delete data;
73 }
74
75 void wxOSXAudioToolboxSoundData::SoundCompleted()
76 {
77 if ( m_flags & wxSOUND_ASYNC )
78 {
79 if (m_flags & wxSOUND_LOOP)
80 AudioServicesPlaySystemSound(m_soundID);
81 else
82 Stop();
83 }
84 else
85 {
86 Stop();
87 CFRunLoopStop(CFRunLoopGetCurrent());
88 }
89
90 }
91
92 void wxOSXAudioToolboxSoundData::DoStop()
93 {
94 if (m_soundID)
95 {
96 AudioServicesDisposeSystemSoundID (m_soundID);
97 m_soundID = NULL;
98
99 wxSound::SoundStopped(this);
100 }
101 }
102
103 bool wxOSXAudioToolboxSoundData::Play(unsigned flags)
104 {
105 Stop();
106
107 m_flags = flags;
108
109 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(m_sndname)));
110 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
111 wxCFRef<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
112
113 AudioServicesCreateSystemSoundID(url, &m_soundID);
114 AudioServicesAddSystemSoundCompletion( m_soundID, CFRunLoopGetCurrent(), NULL, wxOSXAudioToolboxSoundData::CompletionCallback, (void *) this );
115
116 bool sync = !(flags & wxSOUND_ASYNC);
117
118 AudioServicesPlaySystemSound(m_soundID);
119
120 if ( sync )
121 {
122 while( m_soundID )
123 {
124 CFRunLoopRun();
125 }
126 }
127
128 return true;
129 }
130
131 bool wxSound::Create(size_t WXUNUSED(size), const void* WXUNUSED(data))
132 {
133 wxFAIL_MSG( "not implemented" );
134
135 return false;
136 }
137
138 bool wxSound::Create(const wxString& fileName, bool isResource)
139 {
140 wxCHECK_MSG( !isResource, false, "not implemented" );
141
142 m_data = new wxOSXAudioToolboxSoundData(fileName);
143 return true;
144 }
145
146 #endif // wxOSX_USE_AUDIOTOOLBOX
147
148 #endif //wxUSE_SOUND