Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - AttributeError: 'FFmpegPCMAudio' object has no attribute 'start'

I'm trying to program a discord bot with discord.py, the main goal right now is to have my bot join a voice channel and play an audio file that's on my laptop.

@client.command()
async def sing(ctx):
    user = ctx.author.voice
    if user != None:
        channel_chat = ctx.author.voice.channel
        await channel_chat.connect()
        player = discord.FFmpegPCMAudio(executable="C:/Path_Program/ffmpeg.exe", source=r"C:UsersialwaDesktopMiscMusicelda's Lullaby Ancient Tune Hyrule Warriors Age of Calamity Soundtrack.mp3")
        player.start()
    else:
        await ctx.send("Beep beep! (Be in a voice channel please!)")

This is the block of code that's suppose to play a file, the bot joins the channel just fine but fails to play audio. I do have ffmpeg-python installed and already tried installing ffmpeg but both or one at a time brings the same error. The error itself in full is here

Ignoring exception in command sing:
Traceback (most recent call last):
  File "C:UsersialwaPycharmProjectsTYvenvlibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:UsersialwaPycharmProjectsTYmain.py", line 29, in sing
    player.start()
AttributeError: 'FFmpegPCMAudio' object has no attribute 'start'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:UsersialwaPycharmProjectsTYvenvlibsite-packagesdiscordextcommandsot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:UsersialwaPycharmProjectsTYvenvlibsite-packagesdiscordextcommandscore.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:UsersialwaPycharmProjectsTYvenvlibsite-packagesdiscordextcommandscore.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'FFmpegPCMAudio' object has no attribute 'start'

If anyone can help me understand what to do to fix this problem it would be appreciated!

question from:https://stackoverflow.com/questions/65941297/attributeerror-ffmpegpcmaudio-object-has-no-attribute-start

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

discord.FFmpegPCMAudio isn't a player, it's just an audio source.

In order to play audio files, you need discord.VoiceClient.play:

@client.command()
async def sing(ctx):
    user = ctx.author.voice
    if user is not None:
        channel = ctx.author.voice.channel
        voice = await channel.connect()
        voice.play(discord.FFmpegPCMAudio(executable="C:/Path_Program/ffmpeg.exe", source=r"C:UsersialwaDesktopMiscMusicelda's Lullaby Ancient Tune Hyrule Warriors Age of Calamity Soundtrack.mp3"))
    else:
        await ctx.send("Beep beep! (Be in a voice channel please!)")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...