remi icon indicating copy to clipboard operation
remi copied to clipboard

play sound in remi App

Open BradleyWang123 opened this issue 6 years ago • 6 comments

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

BradleyWang123 avatar Dec 02 '19 08:12 BradleyWang123

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?

dddomodossola avatar Dec 02 '19 11:12 dddomodossola

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!

BradleyWang123 avatar Dec 02 '19 12:12 BradleyWang123

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

romanodev avatar Sep 02 '20 18:09 romanodev

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

dddomodossola avatar Sep 03 '20 05:09 dddomodossola

@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)

dddomodossola avatar Sep 03 '20 07:09 dddomodossola

@dddomodossola , fantastic, thanks!!

romanodev avatar Sep 03 '20 21:09 romanodev