1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSound class implementation: optional
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "sound.h"
16 #include "wx/object.h"
17 #include "wx/string.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
26 #import <AppKit/AppKit.h>
29 // NB: Vaclav's new wxSound API is really tricky -
30 // Basically, we need to make sure that if the wxSound
31 // object is still in scope we don't release it's NSSound
34 WX_NSSound lastSound=NULL;
35 bool isLastSoundLooping = false;
36 bool isLastSoundInScope = false;
38 // ========================================================================
40 // ========================================================================
41 @interface wxNSSoundDelegate : NSObject
46 - (void)sound:(NSSound *)theSound didFinishPlaying:(BOOL)bOK;
47 @end // interface wxNSSoundDelegate : NSObject
49 @implementation wxNSSoundDelegate : NSObject
51 - (void)sound:(NSSound *)theSound didFinishPlaying:(BOOL)bOK
53 if (bOK && isLastSoundLooping)
55 else if (!isLastSoundInScope)
62 @end // wxNSSoundDelegate
64 // ------------------------------------------------------------------
66 // ------------------------------------------------------------------
74 wxSound::wxSound(const wxString& sFileName, bool isResource)
78 Create(sFileName, isResource);
81 wxSound::wxSound(int size, const wxByte* data)
85 NSData* theData = [[NSData alloc] dataWithBytesNoCopy:(void*)data length:size];
86 m_hSnd = [[NSSound alloc] initWithData:theData];
88 m_cocoaSoundDelegate = [[wxNSSoundDelegate alloc] init];
93 if (m_hSnd != lastSound)
96 [m_cocoaSoundDelegate release];
99 isLastSoundInScope = false;
102 bool wxSound::Create(const wxString& fileName, bool isResource)
104 wxAutoNSAutoreleasePool thePool;
110 //oftype could be @"snd" @"wav" or @"aiff"; nil or @"" autodetects (?)
111 m_hSnd = [[NSSound alloc]
112 initWithContentsOfFile:[[NSBundle mainBundle]
113 pathForResource:wxNSStringWithWxString(fileName)
118 m_hSnd = [[NSSound alloc] initWithContentsOfFile:wxNSStringWithWxString(fileName) byReference:YES];
120 m_cocoaSoundDelegate = [[wxNSSoundDelegate alloc] init];
122 m_sndname = fileName;
123 return m_hSnd != nil;
126 bool wxSound::DoPlay(unsigned flags) const
128 wxASSERT_MSG(!( (flags & wxSOUND_SYNC) && (flags & wxSOUND_LOOP)),
129 wxT("Invalid flag combination passed to wxSound::Play"));
133 if (flags & wxSOUND_ASYNC)
136 isLastSoundLooping = (flags & wxSOUND_LOOP) == wxSOUND_LOOP;
137 isLastSoundInScope = true;
138 [m_hSnd setDelegate:m_cocoaSoundDelegate];
139 return [m_hSnd play];
143 [m_hSnd setDelegate:nil];
146 bool bOK = [m_hSnd play];
148 while ([m_hSnd isPlaying])
150 wxTheApp->Yield(false);
156 bool wxSound::IsPlaying()
158 return [lastSound isPlaying];
163 if (isLastSoundInScope)
165 isLastSoundInScope = false;
167 //remember that even though we're
168 //programatically stopping it, the
169 //delegate will still be called -
170 //so it will free the memory here
171 [((NSSound*&)lastSound) stop];