我正在尝试将麦克风中的现场音频播放到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
不知道您是要传递编码数据还是非编码数据。
只需在调用encode
方法时添加send_audio_packet
参数即可:
async def play_voice(self, input_stream):
...
voice_client.send_audio_packet(data, encode=stream.is_opus())
关于audio - 从PyAudio播放Discord.py声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62051016/