在前一段时间正在使用的聊天脚本上,当接收到新消息时,我使用winsound python库播放了“叮”声(ding.wav).现在,我想知道如何仅使用.ogg音频文件才能在Linux上实现此功能.代码如下:import sysimport utilimport thr...

在前一段时间正在使用的聊天脚本上,当接收到新消息时,我使用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平台?


基础教程推荐
猜你喜欢
- Python数据提取-lxml模块 2023-08-11
- 一句话介绍python线程、进程和协程 2023-09-03
- 学习笔记114—ubuntu设置python3.7为默认 2023-11-11
- vmware安装centos、python时踩到的坑 2023-09-03
- python-Windows Azure API:以编程方式创建VM 2023-11-12
- python绘图中的 四个绘图技巧 2023-08-04
- python嵌套try...except如何使用详解 2022-08-30
- Python实现字符串匹配算法代码示例 2023-09-04
- Python 内置方法和属性详解 2023-08-04
- Shell命令从python失败,从shell正常 2023-11-12