Discord.py 从 PyAudio 播放语音
Discord.py Play Voice From PyAudio
我正在尝试将麦克风中的实时音频播放到我的 Discord 机器人中,但我无法将来自 PyAudio 的流转换为 Discord 可以播放的可接受格式。
我试过:
async def play_audio_in_voice():
input_stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = CHUNK)
while True:
await voice_commands.play_voice(input_stream)
async def play_voice(self, input_stream):
guild = #got guild here
if guild.voice_client:
data = input_stream.read(CHUNK, exception_on_overflow = False)
voice_client = guild.voice_client
voice_client.send_audio_packet(data)
并且
async def play_voice(self, input_stream):
guild = #got guild here
if guild.voice_client:
data = input_stream.read(CHUNK, exception_on_overflow = False)
stream = await discord.FFmpegPCMAudio(data)
voice_client = guild.voice_client
voice_client.send_audio_packet(data)
在第一个代码中,我得到:
File "D:\Program Files\lib\site-packages\discord\voice_client.py", line 454, in send_audio_packet
encoded_data = self.encoder.encode(data, self.encoder.SAMPLES_PER_FRAME)
AttributeError: 'NoneType' object has no attribute 'encode'
我知道每次在这里调用该函数时我都会遇到问题,我现在会解决这个问题:)
discord.FFmpegPCMAudio
自动对您的声音数据进行 Opus 编码。但是 send_audio_packet
不知道您传递的是编码数据还是 non-encoded 数据。
调用send_audio_packet
方法时只需添加encode
参数:
async def play_voice(self, input_stream):
...
voice_client.send_audio_packet(data, encode=input_stream.is_opus())
我正在尝试将麦克风中的实时音频播放到我的 Discord 机器人中,但我无法将来自 PyAudio 的流转换为 Discord 可以播放的可接受格式。 我试过:
async def play_audio_in_voice():
input_stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = CHUNK)
while True:
await voice_commands.play_voice(input_stream)
async def play_voice(self, input_stream):
guild = #got guild here
if guild.voice_client:
data = input_stream.read(CHUNK, exception_on_overflow = False)
voice_client = guild.voice_client
voice_client.send_audio_packet(data)
并且
async def play_voice(self, input_stream):
guild = #got guild here
if guild.voice_client:
data = input_stream.read(CHUNK, exception_on_overflow = False)
stream = await discord.FFmpegPCMAudio(data)
voice_client = guild.voice_client
voice_client.send_audio_packet(data)
在第一个代码中,我得到:
File "D:\Program Files\lib\site-packages\discord\voice_client.py", line 454, in send_audio_packet
encoded_data = self.encoder.encode(data, self.encoder.SAMPLES_PER_FRAME)
AttributeError: 'NoneType' object has no attribute 'encode'
我知道每次在这里调用该函数时我都会遇到问题,我现在会解决这个问题:)
discord.FFmpegPCMAudio
自动对您的声音数据进行 Opus 编码。但是 send_audio_packet
不知道您传递的是编码数据还是 non-encoded 数据。
调用send_audio_packet
方法时只需添加encode
参数:
async def play_voice(self, input_stream):
...
voice_client.send_audio_packet(data, encode=input_stream.is_opus())