gpt4 book ai didi

python - 将 winsound 转换为 linux 平台?

转载 作者:太空狗 更新时间:2023-10-29 11:09:57 27 4
gpt4 key购买 nike

在我前段时间编写的聊天脚本中,我使用 winsound python 库在收到新消息时播放“叮”声 (ding.wav)。现在我想知道如何只使用 .ogg 音频文件使它适用于 linux。代码如下:

import sys
import util
import thread
import socket
import winsound

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))
winsound.PlaySound("ding.wav", winsound.SND_FILENAME)
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()

如果有人可以帮助我将其转换为播放 .ogg 文件,那就太棒了:)

谢谢,肖恩。

最佳答案

最后,我最终使用了pygame库:

导入pygame

pygame.init()


pygame.mixer.music.load("ding.ogg")

pygame.mixer.music.play()

关于python - 将 winsound 转换为 linux 平台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13150267/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com