work around mplayer on win32 not handling foreign characters

This commit is contained in:
Damien Elmes 2011-01-13 21:27:35 +09:00
parent 149fbee33e
commit eec317bfc3

View file

@ -8,7 +8,8 @@ Sound support
""" """
__docformat__ = 'restructuredtext' __docformat__ = 'restructuredtext'
import re, sys, threading, time, subprocess, os, signal, errno import re, sys, threading, time, subprocess, os, signal, errno, atexit
import tempfile, shutil
from anki.hooks import addHook, runHook from anki.hooks import addHook, runHook
# Shared utils # Shared utils
@ -50,6 +51,8 @@ processingChain = [
["lame", "rec3.wav", processingDst, "--noreplaygain", "--quiet"], ["lame", "rec3.wav", processingDst, "--noreplaygain", "--quiet"],
] ]
tmpdir = None
# don't show box on windows # don't show box on windows
if sys.platform == "win32": if sys.platform == "win32":
si = subprocess.STARTUPINFO() si = subprocess.STARTUPINFO()
@ -58,6 +61,8 @@ if sys.platform == "win32":
except: except:
# python2.7+ # python2.7+
si.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW si.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
# tmp dir for non-hashed media
tmpdir = tempfile.mkdtemp(prefix="anki")
else: else:
si = None si = None
@ -200,7 +205,18 @@ def queueMplayer(path):
ensureMplayerThreads() ensureMplayerThreads()
while mplayerEvt.isSet(): while mplayerEvt.isSet():
time.sleep(0.1) time.sleep(0.1)
path = path.encode(sys.getfilesystemencoding()) if tmpdir:
# mplayer on windows doesn't like the encoding, so we create a
# temporary file instead
(fd, name) = tempfile.mkstemp(suffix=os.path.splitext(path)[1],
dir=tmpdir)
f = os.fdopen(fd, "wb")
f.write(open(path, "rb").read())
f.close()
# it wants unix paths, too!
path = name.replace("\\", "/")
else:
path = path.encode("utf-8")
mplayerQueue.append(path) mplayerQueue.append(path)
mplayerEvt.set() mplayerEvt.set()
runHook("soundQueued") runHook("soundQueued")
@ -225,7 +241,12 @@ def stopMplayer():
return return
mplayerManager.kill() mplayerManager.kill()
def onExit():
if tmpdir:
shutil.rmtree(tmpdir)
addHook("deckClosed", stopMplayer) addHook("deckClosed", stopMplayer)
atexit.register(onExit)
# PyAudio recording # PyAudio recording
########################################################################## ##########################################################################