Playing sounds in Brython

359 views
Skip to first unread message

dan d.

unread,
Dec 11, 2014, 8:09:52 AM12/11/14
to bry...@googlegroups.com
Hello,

To go back to a question I had asked, in the post about how to display images on the canvas.
My question was : how to play sounds from a Brython program / script?

This is the code I tried, and it works. At least in FireFox. 33.

-----------------------------------------
audio = javascript.JSConstructor(window.Audio)('/media/music.mp3')
audio.play()
audio.pause()
-----------------------------------------

*  replace   "'/media/music.mp3"    by your audio file.

If there is a nicer or better way to do it, I am all ears  :)

Cheers,
Dan

Billy Earney

unread,
Dec 11, 2014, 8:25:26 AM12/11/14
to bry...@googlegroups.com
Dan,

Yesterday Pierre posted the following:

HTML5 provides the AUDIO tag, so you can generate a player with the usual play/pause buttons with :

from browser import document, html
document <= html.AUDIO(src='mymusic.mp3', controls=True)


if you give the audio element an id, you might be able to use the play and pause functions, like in your example above.

Billy

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/ed68ed25-ff6b-4345-973a-e303db661f6e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lamprecht Markus

unread,
May 13, 2019, 1:37:55 AM5/13/19
to brython
For:
audio = javascript.JSConstructor(
window.Audio)('/media/music.mp3')
audio.play()
audio.pause()

I got javascript is not defined.


I then tried:
document["play_1"] <= html.AUDIO(src='https://dict.leo.org/media/audio/V4YsI8VSI3rtmWer5QUVGw.ogg', controls=True)
this does not play the file directly which I want but loads the player into my website





Am Donnerstag, 11. Dezember 2014 14:25:26 UTC+1 schrieb Billy Earney:
Dan,

Yesterday Pierre posted the following:

HTML5 provides the AUDIO tag, so you can generate a player with the usual play/pause buttons with :

from browser import document, html
document <= html.AUDIO(src='mymusic.mp3', controls=True)


if you give the audio element an id, you might be able to use the play and pause functions, like in your example above.

Billy
On Thu, Dec 11, 2014 at 7:09 AM, dan d. <dandels...@gmail.com> wrote:
Hello,

To go back to a question I had asked, in the post about how to display images on the canvas.
My question was : how to play sounds from a Brython program / script?

This is the code I tried, and it works. At least in FireFox. 33.

-----------------------------------------
audio = javascript.JSConstructor(window.Audio)('/media/music.mp3')
audio.play()
audio.pause()
-----------------------------------------

*  replace   "'/media/music.mp3"    by your audio file.

If there is a nicer or better way to do it, I am all ears  :)

Cheers,
Dan

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bry...@googlegroups.com.
Message has been deleted

Peter Garcia

unread,
May 11, 2023, 6:22:13 PM5/11/23
to brython
This conversation has been a big help for one of my projects and i must say thank you so much for helping me figure this out.

What I was able to do was get this:
First I had to import document from browser like this

----------------------------------------------------------------------------------
from browser import document, html
from browser import window
----------------------------------------------------------------------------------

Then instead of using the <= operator, I set it as a variable using equals and got rid of the "controls=true"

----------------------------------------------------------------------------------
document = html.AUDIO(src="(audio web address)")
----------------------------------------------------------------------------------

Then you could Play or pause the audio using:

----------------------------------------------------------------------------------
document.play()
document.pause()
----------------------------------------------------------------------------------

Thank you guys for helping me get this, even if it is years later.
I would love to learn of more ways to enhance this code as well if anyone has any tips.
Reply all
Reply to author
Forward
0 new messages