apache / apache/cordova-electron
user gesture interaction (autoplay policy)
- Dominant language
- JavaScript
- Stars
- 135
- Forks
- 60
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 5
Description
# 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](https://electronjs.org/docs/all) ).
Actively setting it to `"autoplayPolicy":"no-user-gesture-required"` in the `webPreferences`has 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](https://cordova.apache.org/docs/en/latest/guide/platforms/electron/index.html#customizing-the-electron's-main-process) and [this issue](https://github.com/electron/electron/issues/13525#issuecomment-410923391):
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-media`and 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
Contributor guide
Research direction
Start in platforms/electron/platform_www/cdv-electron-main.js near the Electron app setup and compare the documented autoplayPolicy behavior with the command-line workaround described in the issue. Reproduce the default Cordova app with cordova-plugin-media and verify that the provided delayed playback example works without a user interaction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- electron, javascript
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100