如何让Python播放声音?
我正在编写Python聊天脚本,客户端如下所示:如何让Python播放声音?,python,sockets,Python,Sockets,我正在编写Python聊天脚本,客户端如下所示: import sys import util import thread import socket class ClientSocket(): rbufsize = -1 wbufsize = 0 def __init__(self, address, nickname=''): if type(address) == type(()) and type(address[0]) == type('') and type(a
import sys
import util
import thread
import socket
class ClientSocket():
rbufsize = -1
wbufsize = 0
def __init__(self, address, nickname=''):
if type(address) == type(()) and type(address[0]) == type('') and
type(address[1])== type(1):
pass
else:
print ('Address is of incorrect type. \n' +
'Must be (serverHost (str), serverPort (int)).')
sys.exit(1)
if nickname:
self.changeNick(nickname)
else:
self.changeNick(raw_input('Nickname: '))
self.prompt_on = False
self.address = address
def connect(self):
self.connection=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect(self.address)
self.rfile = self.connection.makefile('rb', self.rbufsize)
self.wfile = self.connection.makefile('wb', self.wbufsize)
self.wfile.write('/nick ' + self.nickname + '\n')
def serve_forever(self):
self.connect()
thread.start_new_thread(self.acceptinput,())
line = ""
while line not in ('/exit','/quit', '/q'):
self.prompt_on = True
line = raw_input(self.prompt)
self.prompt_on = False
if line[:2] == '/n' or line[:5] == '/nick':
self.changeNick(line.split(' ', 1)[1].strip())
self.wfile.write(line + '\n')
self.close()
self.connection.shutdown(socket.SHUT_RDWR)
self.connection.close()
def changeNick(self, newNick):
self.nickname = newNick
self.prompt = self.nickname+': '
self.backspace = '\b' * len(self.prompt)
def acceptinput(self):
while 1:
data = self.rfile.readline().strip()
if data:
self.writedata(data)
if 'Nickname successfully changed to' in data:
self.changeNick(data.split('"')[1])
def writedata(self, data):
if self.prompt_on:
output = data if len(data) >= len(self.prompt) else data + ' '
(len(self.prompt) - len(data))
sys.stdout.write(self.backspace + output + '\n' + self.prompt)
sys.stdout.flush()
else:
print data
def close(self):
if not self.wfile.closed:
self.wfile.flush()
self.wfile.close()
self.rfile.close()
def main():
serverHost = raw_input('Server IP/Hostname: ')
if not serverHost:
serverHost = util.getIP()
else:
serverHost = socket.gethostbyname(serverHost)
serverPort = input('Server Port: ')
address = (serverHost, serverPort)
client = ClientSocket(address)
print 'Connecting to server on %s:%s' % (serverHost, serverPort)
client.serve_forever()
if __name__ == '__main__':
main()
我想知道是否有办法在每次收到新消息时播放声音。可能使用winsound吗?(http://stackoverflow.com/questions/307305/play-a-sound-with-python)
谢谢:)我很惊喜这是标准的库模块。请注意,这仅在Windows上可用
在刷新之后,writa_data函数中会发生什么。这能达到你想要的效果吗
import winsound
# Play Windows exit sound.
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
好吧,算了!!!谢谢你的帮助!:DDD
您可以在第101行的
sys.stdout.write(self.backspace+output+'\n'+self.prompt)上方添加winsound.PlaySound(“SystemExit”,winsound.SND\u别名)
。现在,每当有新消息传来,它就会发出“叮!”。如果要使用脚本运行所在目录中的自定义.wav声音,请在第101行添加winsound.PlaySound(“ding.wav”,winsound.SND_文件名)
。享受吧 socket程序与声音有什么关系?试着用小例子来说明你的问题。。只是,我是Python新手,你会把winsound.PlaySound(“SystemExit”,winsound.SND_别名)
放在哪一行上?在-sys.stdout.flush()之前/之后试试看啊,我知道你现在在说什么了。我在sys.stdout.flush()
之前和之后都尝试过它,但它不起作用,它关闭了它。我明白了。我会尝试将它作为write_data中的最后一条语句,并查看它是否被调用,我会执行importpdb;pdb.set_trace()
进入调试并逐步完成。好的。。我比我想象的更能理解这件事。。。你是怎么做到的。。?我想你可能需要充分解释一下。。。