rawpython / remi

Python REMote Interface library. Platform independent. In about 100 Kbytes, perfect for your diet.
Apache License 2.0
3.53k stars 402 forks source link

play sound in remi App #347

Open BradleyWang123 opened 4 years ago

BradleyWang123 commented 4 years ago

How do I auto play sound on remi App web page?

dddomodossola commented 4 years ago

Depends on what you want to do. Do you simply need to play some sounds withoud user control? Here is an example

    import remi.gui as gui
    from remi import start, App
    import os

    class MyApp(App):

        def main(self):
            #creating a container VBox type, vertical (you can use also HBox or Widget)
            main_container = gui.VBox(width=300, height=200, style={'margin':'0px auto'})
            bt = gui.Button("play")
            bt.onclick.connect(self.on_click)
            main_container.append(bt)
            # returning the root widget
            return main_container

        def on_click(self, emitter):
            self.execute_javascript("(new Audio('%s')).play();"%gui.load_resource(audio_filename))

    if __name__ == "__main__":
        # starts the webserver
        start(MyApp)

Or do you differently want to play an audio file providing play/stop controls?

BradleyWang123 commented 4 years ago

Thanks a lot! I want to trigger the sound when I finished something, e.g. face ID recognized then trigger a wave file to welcome!

romanodev commented 4 years ago

I am trying to pass bytes (raw audio data) to the javascript part so I can play audio without using a file. Would that be possible? Thanks

dddomodossola commented 4 years ago

Hello @romanodev yes of course it is possible. I will send you an example today 😉

dddomodossola commented 4 years ago

@romanodev this is for you ;-)

import remi
import remi.gui as gui
from remi import start, App
import os

class MyApp(App):
    def main(self):
        # creating a container VBox type, vertical (you can use also HBox or Widget)
        main_container = gui.VBox(width=300, height=200, style={'margin': '0px auto'})

        bt = gui.Button("play sound")
        bt.onclick.do(self.play)

        main_container.append(bt)

        # returning the root widget
        return main_container

    def play(self, emitter):
        #load a wav file with the following
        data = remi.gui.load_resource('./alarmSound.wav')
        self.execute_javascript('var snd = new Audio("%s"); snd.play();'%data)

        #or generate it
        #import base64
        #data = custom_wave_sine_or_music_generator()
        #data = base64.b64encode(data)
        #if pyLessThan3:
        #    data = data.encode('utf-8')
        #else:
        #    data = str(data, 'utf-8')
        #self.execute_javascript('var snd = new Audio("data:audio/wav;base64,%s"); snd.play();'%data)

if __name__ == "__main__":
    # starts the webserver
    start(MyApp, address='0.0.0.0', port=0, start_browser=True, username=None, password=None)
romanodev commented 4 years ago

@dddomodossola , fantastic, thanks!!