回答編集履歴

1

追記

2022/02/27 06:38

投稿

退会済みユーザー
test CHANGED
@@ -7,3 +7,49 @@
7
7
  player.stopVideo()
8
8
  })
9
9
  ```
10
+
11
+ 追記
12
+ JavaScriptを書く場所とかが悪いのかもしれません。
13
+ 以下のコードで動作確認しました。
14
+ ```
15
+ <!DOCTYPE html>
16
+ <html>
17
+ <head>
18
+ <meta charset="UTF-8">
19
+ </head>
20
+ <body>
21
+ <div class="movieWrap">
22
+ <div id="player"></div>
23
+ </div>
24
+ <div class="buttons">
25
+ <button id="pauseMovie" type="button" class="button -pause">停止</button>
26
+ <button id="playMovie" type="button" class="button -play">再生</button>
27
+ </div>
28
+
29
+ <script>
30
+ const tag = document.createElement('script')
31
+ tag.src = "https://www.youtube.com/iframe_api"
32
+ const firstScriptTag = document.getElementsByTagName('script')[0]
33
+ firstScriptTag.parentNode.insertBefore(tag, firstScriptTag)
34
+ let player
35
+ function onYouTubeIframeAPIReady() {
36
+ player = new YT.Player('player', {
37
+ videoId: 'R57QxyjBGWA',
38
+ })
39
+ }
40
+
41
+ // 動画の操作
42
+ const pauseButton = document.getElementById('pauseMovie')
43
+ const playButton = document.getElementById('playMovie')
44
+
45
+ pauseButton.addEventListener('click', () => {
46
+ player.pauseVideo()
47
+ })
48
+
49
+ playButton.addEventListener('click', () => {
50
+ player.playVideo()
51
+ })
52
+ </script>
53
+ </body>
54
+ </html>
55
+ ```