質問編集履歴

1

編集

2022/11/22 11:49

投稿

aaaa____
aaaa____

スコア26

test CHANGED
File without changes
test CHANGED
@@ -48,3 +48,38 @@
48
48
  </form>
49
49
  </html>
50
50
  ```
51
+
52
+
53
+ ### 追記
54
+ 回答を参考に以下のようにコードを書き換えました.
55
+
56
+ ```html
57
+ <form action="myForm.cgi" method="post" enctype="multipart/form-data">
58
+ <input type="file" name="myFile" id="myFile" accept="audio/wav">
59
+ <p>Select the .wav file you wish to play, if any</p>
60
+ <script>
61
+ // input要素
62
+ const fileInput = document.getElementById('myFile');
63
+ // changeイベントで呼び出す関数
64
+ const handleFileSelect = () => {
65
+ const files = fileInput.files;
66
+ console.log(files[0]);
67
+ var objectUrl = URL.createObjectURL(files[0]);
68
+ console.log(objectUrl);
69
+ }
70
+ // ファイル選択時にhandleFileSelectを発火
71
+ fileInput.addEventListener('change', handleFileSelect);
72
+ </script>
73
+ <audio id="btn_audio">
74
+ <source src = objectUrl type="audio/wav">
75
+ </audio>
76
+ </form>
77
+ <div class="btn" onclick="audio()">
78
+ <script>
79
+ function audio() {
80
+ document.getElementById('btn_audio').currentTime = 0; //連続クリックに対応
81
+ document.getElementById('btn_audio').play(); //クリックしたら音を再生
82
+ }
83
+ </script>
84
+ </div>
85
+ ```