0

I've created a little audio player that looks up a chapter of an audiobook at a website I've specified, downloads it, and plays it. The only problem is, I can't pause it. To play the mp3 file I'm using os.system("afplay chapter.mp3")

I've thought about creating a thread with the os.system call in it but I'm pretty sure I can't pause it that way. If the thread was a loop I could just lock a variable it needs to access and unlock when I'm ready to resume. But since this thread would be just one line of code that doesn't seem possible. I've also looked at creating a process and sending SIGSTOP to it. But for some unknown reason that won't work.

import os, signal
from multiprocessing import Process

p = Process(target=play)
p.start()
raw_input("press enter to pause: ")
os.kill(p.pid, signal.SIGSTOP)

The code just executes silently without stopping the process.

I know there are alternatives to afplay but for now I'm just going to stick with the os.system call. So my question is, How can I pause a one-line thread or process? Instead of creating a new process with the Process() call do I need to find the process id of afplay? If so how?

4

2 回答 2

1

os.system创建一个子进程并等待该子进程退出。你可以用 os.execv另一个程序替换一个进程,或者subprocess.Popen用来创建一个你可以找到 pid 的子进程Popen.pid

于 2012-07-17T13:31:41.447 回答
0

为什么不直接使用 subprocess 模块而不是 subprocess 和 os.system?这应该可以让您更好地控制生成的进程。

于 2012-07-17T13:38:19.050 回答