* Plays audio or video from a media element. * * @chainable * * @example * let beat; * * function setup() { * createCanvas(100, 100); * * background(200); * * // Style the text. * textAlign(CENTER); * textSize(16); * * // Display a message.
()
| 110 | * } |
| 111 | */ |
| 112 | play() { |
| 113 | if (this.elt.currentTime === this.elt.duration) { |
| 114 | this.elt.currentTime = 0; |
| 115 | } |
| 116 | let promise; |
| 117 | if (this.elt.readyState > 1) { |
| 118 | promise = this.elt.play(); |
| 119 | } else { |
| 120 | // in Chrome, playback cannot resume after being stopped and must reload |
| 121 | this.elt.load(); |
| 122 | promise = this.elt.play(); |
| 123 | } |
| 124 | if (promise && promise.catch) { |
| 125 | promise.catch(e => { |
| 126 | // if it's an autoplay failure error |
| 127 | if (e.name === 'NotAllowedError') { |
| 128 | if (typeof IS_MINIFIED === 'undefined') { |
| 129 | p5._friendlyAutoplayError(this.src); |
| 130 | } else { |
| 131 | console.error(e); |
| 132 | } |
| 133 | } else { |
| 134 | // any other kind of error |
| 135 | console.error('Media play method encountered an unexpected error', e); |
| 136 | } |
| 137 | }); |
| 138 | } |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Stops a media element and sets its current time to 0. |