flet icon indicating copy to clipboard operation
flet copied to clipboard

audio does not work on the web

Open Gorkor227 opened this issue 2 years ago • 4 comments

Duplicate Check

  • [X] I have searched the opened issues and there are no duplicates

Describe the bug

audio does not work on the web.

Code sample

Logs
import flet as ft

def main(page: ft.Page):
    audio1 = ft.Audio(
        src="https://luan.xyz/files/audio/ambient_c_motion.mp3", autoplay=True
    )
    page.overlay.append(audio1)
    page.add(
        ft.Text("This is an app with background audio."),
        ft.ElevatedButton("Stop playing", on_click=lambda _: audio1.pause()),
        ft.ElevatedButton("playing", on_click=lambda _: audio1.play()),
    )

ft.app(target=main)

To reproduce

flet run --web

Expected behavior

No response

Screenshots / Videos

Screenshots / Video demonstration

[Upload media here]

Operating System

Windows

Operating system details

Microsoft Windows 10 professional

Flet version

0.23.2

Regression

No, it isn't

Suggestions

No response

Logs

Logs
http://127.0.0.1:35886

Additional details

my env: python 3.12.4

Gorkor227 avatar Jul 23 '24 14:07 Gorkor227

Which web browser is that? and on which device?

ndonkoHenri avatar Jul 23 '24 22:07 ndonkoHenri

那是哪种网络浏览器?在哪种设备上?

125.0.6422.61 chrome laptop win10

Gorkor227 avatar Jul 24 '24 03:07 Gorkor227

Issue is browser not flet, if you run that code and inspect the web page, you will see your code is bad logic as browser is complaining about no user gesture so will not autoplay, which I believe was done years ago on purpose so ads cannot start playing on every website you visit.

Why not try something like this instead:

import flet as ft
from flet_core.types import AppView


class ConfigApp:
    def __init__(self, page):
        self.page = page
        # set audio to some empty wav file to get around gesture block for now
        self.audio = ft.Audio(src="https://mp3.sunsaturn.com/silent.wav", autoplay=True)
        self.page.overlay.append(self.audio)

    async def playme(self, e):
        self.audio.src = "https://luan.xyz/files/audio/ambient_c_motion.mp3"
        self.audio.play()
        self.audio.update()


async def main(page: ft.Page):
    config = ConfigApp(page)
    page.add(
        ft.Text("This is an app with background audio."),
        ft.ElevatedButton("Stop playing", on_click=lambda _: config.audio.pause()),
        ft.ElevatedButton("playing", on_click=config.playme),
    )


ft.app(target=main, view=AppView.WEB_BROWSER)

Should be noted when running any code in pure web form that remote site should have CORS enabled as well..

syleishere avatar Jul 24 '24 05:07 syleishere

autoplay was noted to have issues on chrome: https://flet.dev/docs/controls/audio/#:~:text=to%20False.-,NOTE,-Autoplay%20works%20in

ndonkoHenri avatar Jul 24 '24 14:07 ndonkoHenri