#Playing MP3 Files in a Voice Channel

1 messages · Page 1 of 1 (latest)

obsidian yacht
#

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.

broken drum
obsidian yacht
broken drum
#

No need to apologize. Were you able to fix your problem?

obsidian yacht
#

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
obsidian yacht
#

@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 😅

broken drum
#

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...

obsidian yacht
#

👀

broken drum
#
@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.

obsidian yacht
obsidian yacht
broken drum
#

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.

obsidian yacht
#

ye, this is quite interesting, it does connect when using get_channel.connect() but can't play audio from it

lime falcon
#

@obsidian yacht so what's going on with this at the moment

lime falcon
#

oh well

#

a timeout error is called when the timeout is reached

#

which is supposed to happen

obsidian yacht
# lime falcon 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

lime falcon
#

does it call timeout error immediately, or after the ten seconds?

obsidian yacht
#

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

lime falcon
#

removing it defaults to sixty seconds

obsidian yacht
#

Same thing happened after the 60s

lime falcon
#

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().
#

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.

obsidian yacht
#

Like after connecting to the voice channel. How do I initialize Voice Client

lime falcon
#

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.

obsidian yacht
lime falcon
#

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

obsidian yacht
lime falcon
#

Yes. You aren't supposed to create your own VoiceClient object. It's unnecessary

obsidian yacht
#

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

lime falcon
#

Well that's just because I changed your variable names around

#

voice_channel is no longer your voice client

#

voice_client is

lime falcon
#

@obsidian yacht Did this work for you?

obsidian yacht
#

Forgot to reply but it did worked