Skip to content

Commit 96e763e

Browse files
committed
Use Cocoa to play mp3 files on Mac OS X
1 parent 4610329 commit 96e763e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

ldoce5viewer/qtgui/utils/soundplayer.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
gobject = None
2121

2222

23+
# Cocoa via PyObjC
24+
try:
25+
import AppKit
26+
except:
27+
AppKit = None
28+
29+
2330
# WinMCI
2431
if sys.platform == 'win32':
2532
try:
@@ -148,6 +155,30 @@ def close(self):
148155
pass
149156

150157

158+
class AppKitBackend(Backend):
159+
def __init__(self, parent, temp_dir):
160+
self._sound = None
161+
162+
def stop(self):
163+
if self._sound:
164+
self._sound.stop()
165+
166+
def play(self, data):
167+
if self._sound:
168+
self._sound.stop()
169+
170+
with NamedTemporaryFile(mode='w+b', prefix='',
171+
suffix='.tmp.mp3', delete=False) as f:
172+
f.write(data)
173+
174+
self._sound = AppKit.NSSound.alloc()
175+
self._sound.initWithContentsOfFile_byReference_(f.name, True)
176+
self._sound.play()
177+
178+
def close(self):
179+
self.stop()
180+
181+
151182
class PhononBackend(Backend):
152183
def __init__(self, parent, temp_dir):
153184
self._player = Phonon.createPlayer(Phonon.NoCategory)
@@ -190,6 +221,8 @@ def close(self):
190221

191222
def create_soundplayer(parent, temp_dir):
192223
backends = []
224+
if AppKit:
225+
backends.append(AppKitBackend)
193226
if mp3play:
194227
backends.append(WinMCIBackend)
195228
if gst:

0 commit comments

Comments
 (0)