[Python] How to play a mp3 file with PyScript?

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I want to play an mp3 file using the HTML tag like so:

    <audio controls>
    <source id="player" src="sounds//ziege.mp3" type="audio/mpeg">
    </audio>


    Playing the mp3 file trough the player UI works of course but not within PyScript:

    <button py-click="play">Play</button>
    <button py-click="pause">Pause</button>
    <button py-click="stop">Stop</button>

    <script type="py">
    from pyscript import document, display
    from pyweb import pydom
    from pyodide.ffi import to_js

    # probably not necessary - does not work without the conversion either
    sound = to_js(pydom["#player"][0])
    display(sound)

    def play(event):
    display(sound)
    sound.play()

    def pause(event):
    sound.pause()

    def stop(event):
    sound.stop()
    </script>


    Hitting any of the buttons just produce an "AttributeError: play"

    How can I play an mp3 file through the player or is there a PyScript module that I have overlooked so far?

    I am using PyScript version 3.11.3

    Continue reading...

Compartilhe esta Página