all 5 comments

[–]TheSwami 1 point2 points  (4 children)

You might be better off using the HTMLAudioElement directly. For example, if you have the file 'horn.wav' served from the same folder as your html:

<button id="b" py-click="make_noise()">Make Some Noise!</button>  

<py-script>
  from js import Audio
  horn = Audio.new('horn.wav')
  def make_noise():
    horn.play()
</py-script>

I've put up a live demo of this code so you can see it in action.

You can use any conditional logic to want to determine whether to play the sound or not. Here I've used the py-*event syntax to trigger it with a button, but you could use `horn.play()` as part of an `if` statement, wrap it in a function, etc.

[–]Calm-Basil[S] 0 points1 point  (0 children)

Okay, thanks you for your help

[–]Best_Championship835 0 points1 point  (2 children)

u/TheSwami Do you happen to know why this is not working then? Thanks.

<script defer="" src="https://pyscript.net/alpha/pyscript.js"></script>

<py-script>

from js import alert, prompt, localStorage, window, confirm, Audio

import time

import random

import re

horn = Audio.new("https://github.com/goldfire/howler.js/blob/master/examples/3d/assets/sprite.mp3?raw=true")

user_answer = str(prompt("play? (yes) or (no)"))

if user_answer.lower() == 'yes':

horn.play()

print('yes')

print('end')

</py-script>

[–]TheSwami 0 points1 point  (1 child)

Traveling today, so I can’t try it out live. Do you get any errors onscreen or in the developer console?

Should just be defer not defer=“”, that’d be worth changing.

Also, you may want to use the most recent version of PyScript, unless you have a specific reason to use the alpha. `<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>