Hello, I'm trying to play an MP3 file to a voice channel from my PC using FFmpegPCMAudio. I've created a voice channel instance and have successfully installed all the necessary packages and requirements. However, I'm encountering an issue when trying to play the file in the voice channel. When I use the voice_channel.play attribute, I receive an error indicating that .play is not a valid attribute. I suspect I might be overlooking something, so I would greatly appreciate any guidance. Thank you.
#Playing MP3 Files in a Voice Channel
1 messages · Page 1 of 1 (latest)
You want to call discord.VoiceClient.play, not VoiceChannel.play (which does not exist).
https://docs.pycord.dev/en/stable/api/voice.html#discord.VoiceClient.play
Pycord
Objects: Attributes average_latency, channel, endpoint, guild, latency, loop, session_id, source, token, user. Methods async disconnect, def is_connected, def is_paused, def is_playing, async move_...
thanks for the tip and my bad for missing your message
No need to apologize. Were you able to fix your problem?
i actually tried implimenting it in my code and use the very same discord.VoiceClient instance to inititiate a connection to the voice channel
it does require two params howener, reconnect and timeout
when i start the bot, it does connect to the vc then leaves after timeout with a timeout error
async def on_ready(self):
print(f'Logged in as {self.user.name}')
self.voice = self.get_channel(config.VOICE_CHANNEL_ID)
self.voice_channel = discord.VoiceClient(discord.Client(), self.voice)
await self.voice_channel.connect(reconnect=True, timeout=10)
await self.change_presence(activity=discord.Game(name='With candles'))
self.check_candles.start()```
(candlestick) PS C:\Users\trade\Documents\candlestick> python main.py
Logged in as Candlestick Detection
Ignoring exception in on_ready
Traceback (most recent call last):
File "C:\Users\trade\Documents\candlestick\Lib\site-packages\discord\client.py", line 378, in _run_event
await coro(*args, **kwargs)
File "C:\Users\trade\Documents\candlestick\bot.py", line 29, in on_ready
await self.voice_channel.connect(reconnect=True, timeout=10)
File "C:\Users\trade\Documents\candlestick\Lib\site-packages\discord\voice_client.py", line 396, in connect
await utils.sane_wait_for(futures, timeout=timeout)
File "C:\Users\trade\Documents\candlestick\Lib\site-packages\discord\utils.py", line 714, in sane_wait_for
raise asyncio.TimeoutError()
TimeoutError
well, i can't say with conviction now since i'm unable to properly conenct the bot to the vc 😅
@broken drum sry for pining you my man, i usually don't do this but i got to get this online asap, covid's killing me rn so i'm losing my brain cells, i need my nap 😅
Hmmm
The way I do it, I don't get the voice channel from self.voice
I use a slash command, so that it has to come from a person, in a voice channel
Like this...
👀
@bot.slash_command(name='config', description='Bot config')
async def config(ctx):
voice = ctx.author.voice
vc = await voice.channel.connect()
Hasn't failed me yet, but it might not work in your scenario.
and that calls the discord.VoiceClient object by default?
ye ig it's not compatible since in my case, it needs to connect to the vc when the bot gets online
I think I am getting my instance of discord.VoiceClient from return value of connect(). not sure.
This is interesting to me, as well. I would like to be able to connect a voice channel without depending on a user.
ye, this is quite interesting, it does connect when using get_channel.connect() but can't play audio from it
@obsidian yacht so what's going on with this at the moment
This @lime falcon
oh well
a timeout error is called when the timeout is reached
which is supposed to happen
How do I make it connect without the timeout error happening? I could use the get channel button but I won't be able to play the audio in the VC unless I use Voice Client that requires the timeout param
does it call timeout error immediately, or after the ten seconds?
After the 10s
As it's supposed to indeed
But I need it to be in the vc for as long the bot is online
And needs to join as the bot gets online
Try increasing the timeout or removing it
removing it defaults to sixty seconds
Tried that too tbh
Same thing happened after the 60s
ah alright
you can get the voice client simply by getting the return type of connecting to the channel
self.voice_channel = self.get_channel(config.VOICE_CHANNEL_ID)
self.voice_client = self.voice_channel.connect()
VoiceClient Docs specify:
You do not create these, you typically get them from e.g. VoiceChannel.connect().
Pycord
Objects: Attributes average_latency, channel, endpoint, guild, latency, loop, session_id, source, token, user. Methods async disconnect, def is_connected, def is_paused, def is_playing, async move_...
at least in my experience, the pycord docs are some of the better ones around. I remember when I was just starting, though, and had no idea what anything meant. So if you have any questions about anything relating to the docs, feel free to ask.
I read that part, but I'm not sure how it helps me tbh
Like after connecting to the voice channel. How do I initialize Voice Client
You don't.
The library does it for you
self.voice_client = self.voice_channel.connect()
is a better and easier way of doing
self.voice_channel = discord.VoiceClient(discord.Client(), self.voice)
await self.voice_channel.connect(reconnect=True, timeout=10)
because the method VoiceChannel.connect() does that all for you
You don't have to worry about anything but the channel you're connecting to.
But then I can't access .play using this method
The connect method of a VoiceChannel returns a VoiceClient. A VoiceClient includes all of the necessary methods to play audio, including play, pause, and resume.
Pycord
Objects: Attributes average_latency, channel, endpoint, guild, latency, loop, session_id, source, token, user. Methods async disconnect, def is_connected, def is_paused, def is_playing, async move_...
Your on ready function would then look something like this:
async def on_ready(self):
print(f'Logged in as {self.user.name}')
self.voice_channel = self.get_channel(config.VOICE_CHANNEL_ID)
self.voice_client = await self.voice_channel.connect(reconnect=True)
await self.change_presence(activity=discord.Game(name='With candles'))
self.check_candles.start()
Being functionally the same
Hmm, I think I'm explaining this wrong. So the connection to the voice channel is an issue only when using the Voice Client class. I can easily connect the bot using the get channel attribute
Yes. You aren't supposed to create your own VoiceClient object. It's unnecessary
Now when it's connected, I want to play an mp3 sound using the .play function
So I'd have something like this:
self.voice_channel.play("sound.mp3")
But when I do that .play isn't a valid attribute of voice channel
Well that's just because I changed your variable names around
voice_channel is no longer your voice client
voice_client is
@obsidian yacht Did this work for you?
You truly are handful man
Forgot to reply but it did worked