cordova-electron icon indicating copy to clipboard operation
cordova-electron copied to clipboard

user gesture interaction (autoplay policy)

Open dirkk0 opened this issue 5 years ago • 1 comments

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

  • [x] I searched for existing GitHub issues
  • [x] I updated all Cordova tooling to most recent version
  • [x] I included all the necessary information above

dirkk0 avatar Oct 06 '19 15:10 dirkk0

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

mihaiav avatar Dec 04 '19 19:12 mihaiav