aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/htmlmediaelement/play/index.html
blob: b65ea9ebf36f7bb630d169cba8615549a7447302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
title: HTMLMediaElement.play()
slug: Web/API/HTMLMediaElement/play
tags:
  - API
  - Audio
  - HTMLMediaElement
  - Interface
  - Media
  - Méthode
  - Reference
  - Video
  - play
translation_of: Web/API/HTMLMediaElement/play
---
<p>{{APIRef("HTML DOM")}} {{SeeCompatTable}}</p>

<p>La méthode <strong><code>HTMLMediaElement.play()</code></strong> essaie de commencer la lecture du média et renvoie une {{jsxref("Promise")}} qui est résolue quand la lecture a commencé avec succès, et est rejetée si la lecture ne peut commencer pour n'importe quelle raison (comme, par exemple, une erreur de permission).</p>

<h2 id="Syntaxe">Syntaxe</h2>

<pre class="syntaxbox">var <em>Promise</em> = <em>HTMLMediaElement</em>.play();</pre>

<h3 id="Paramètres">Paramètres</h3>

<p>Aucun.</p>

<h3 id="Valeur_de_retour">Valeur de retour</h3>

<p>Une {{jsxref("Promise")}} qui est résolue quand la lecture a commencé, ou rejetée si la lecture ne peut commencer.</p>

<h3 id="Exceptions">Exceptions</h3>

<p>Le <em>gestionnaire de rejet</em> de la promesse est appelé avec un nom d'exception comme seul paramètre (contrairement aux exceptions traditionnelles). Les exceptions possibles incluent :</p>

<dl>
 <dt><code>NotAllowedError</code></dt>
 <dd>Le client (navigateur) ou système d'exploitation ne permet pas la lecture du média dans le contexte ou la situation actuelle. Ceci peut arriver, par exemple, si le navigateur oblige l'utilisateur à lancer manuellement le média en cliquant sur le bouton "play".</dd>
 <dt><code>NotSupportedError</code></dt>
 <dd>La source du média (qui peut être spécifié comme un {{domxref("MediaStream")}}, {{domxref("MediaSource")}}, {{domxref("Blob")}}, ou {{domxref("File")}}, par example) ne représente pas un format supporté.</dd>
</dl>

<p>D'autres types d'exceptions peuvent survenir selon l'implémentation du navigateur, du lecteur de média, etc.</p>

<h2 id="Exemple">Exemple</h2>

<p>Cet exemple commence la lecture d'un élément <code>video</code> et met à jour une box avec un message de succès si la lecture a bien démarré et un message d'erreur si une erreur survient.</p>

<pre class="brush: js">let myVideo = document.getElementById("myVideoElement");

myVideo.play().then(() =&gt; {
  document.getElementById("statusText").innerHTML = "Yay ! La vidéo est lancée !";
}).catch((error) =&gt; {
 document.getElementById("statusText").innerHTML = "Erreur: " + error;
});
</pre>

<h2 id="Spécifications">Spécifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th>Spécification</th>
   <th>Statut</th>
   <th>Commentaire</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'embedded-content.html#dom-media-play', 'play()')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Définition initiale; living specification.</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C','embedded-content-0.html#playing-the-media-resource','play()')}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>Définition initiale.</td>
  </tr>
 </tbody>
</table>

<div class="note">
<p>Notez que les versions WHATWG et W3C de la spécification diffèrent (depuis le 20 Avril 2016) par le fait que cette méthode renvoie respectivement une {{jsxref("Promise")}} ou rien du tout.</p>
</div>

<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Fonctionnalité</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Support basique</td>
   <td>{{CompatChrome(50.0)}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("1.9.1")}}<sup>[1]</sup></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Renvoie une promesse</td>
   <td>{{CompatChrome(50.0)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop(53)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Fonctionnalité</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Edge</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Support basique</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(50.0)}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(50.0)}}</td>
  </tr>
  <tr>
   <td>Renvoie une promesse</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile(53)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>