apache / cordova-electron

Apache Cordova Electron
Apache License 2.0
126 stars 59 forks source link

user gesture interaction (autoplay policy) #102

Open dirkk0 opened 5 years ago

dirkk0 commented 5 years ago

Bug Report

user gesture interaction (autoplay policy)

Problem

The main electron documentation states that autoplayPolicy defaults to no-user-gesture-required which unfortunately isn't the case (see here and search for autoplay ). Actively setting it to "autoplayPolicy":"no-user-gesture-required" in the webPreferenceshas no effect either.

Fortunately this can be overcome by a hack described below.

What is expected to happen?

Sound is playing without a user interaction.

What does actually happen?

Sound is only playing with a user interaction.

Information

I wanted to document both problem and workaround in this issue.

The workaround is based on this part of the docs and this issue: Add

app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');`

after

('const { app, BrowserWindow } = require('electron');')

around line 23 of platforms/electron/platform_www/cdv-electron-main.js.

This effectively adds a command line switch to Chromium.

Command or Code

Take the default cordova application, add the cordova-plugin-mediaand put this code in the onDeviceReady method:

    setTimeout(function () {
      var myMedia = new Media("https://kozco.com/tech/piano2-Audacity1.2.5.mp3")
      myMedia.play({ numberOfLoops: 2 })
    }, 2000)

The sound will play, but only if you click the window within the first seconds. If not, the console shows the infamous 'user interaction required' error.

Environment, Platform, Device

Mac OS 10.13.6 (High Sierra)

Version information

Cordova CLI

Checklist

mihaiav commented 4 years ago

This user-gesture policy is a broader issue of the Electron platform. Electron should have a different trust model (i.e. CORS, user-gesture etc) than a simple website. I believe special flags / options should be supported to disable these "security" requirements. Below are several such use cases: requestFullScreen autoplay click events - it seems Chrome require user interaction for them to be fired. This makes no sense for a remote controlled electron app for example (i.e. a TV app controlled via web sockets). History API (pop events are not triggered without user interaction).

There are some related issues closed for various reasons. Chrome used to have a flag to disable user-gesture but we all well know that Chrome flags are treated as experiments/unstable so they removed them .

https://github.com/electron/electron/issues?q=is%3Aissue+user-gesture+is%3Aclosed