如何在JavaScript中点击按钮时上传音频。

huangapple go评论86阅读模式
英文:

how to play upload audio on button click in javascript

问题

我有一个代码,在选择文件时播放音频,但我想要在按钮点击时播放音频而不存储它。但在按钮点击时它不起作用。
这是我的Blade文件:

<label for="audioFile" class="input-label">音频文件</label>
<input type="file" id="audioFile" class="form-control" name="audio-file[]" accept="audio/*">

<button onclick="fileSelect(event)" type="button" style="color: cornflowerblue; border: white; background-color: white;"><i class="fa fa-volume-up test"></i></button>
<audio id="audio-preview"></audio>

这是JavaScript代码:

function fileSelect(e) {
   var sound = document.getElementById('audio-preview');
   var reader = new FileReader();
   reader.onload = function(e) {
       sound.src = this.result;
       sound.controls = true;
       sound.play();
   };
   reader.readAsDataURL(e.target.files[0]);
}

还有一件事,当音频播放时,多媒体播放器没有显示。
请有人帮助解决我的问题。
提前感谢您。

英文:

I have a code to play an audio when choosing file but i want to play audio on button click without storing it . but on button click its not working .
there is my blade file

 &lt;label for=&quot;audioFile&quot; class=&quot;input-label&quot;&gt;Audio File&lt;/label&gt;
                        &lt;input type=&quot;file&quot;  id=&quot;audioFile&quot; class=&quot;form-control&quot; name=&quot;audio-file[]&quot; accept=&quot;audio/*&quot;  &gt;

                    &lt;button onclick=&quot;fileSelect(event)&quot; type=&quot;button&quot;  style=&quot;color:cornflowerblue;border:white;background-color:white;&quot;&gt;&lt;i class=&quot;fa fa-volume-up test&quot;&gt;&lt;/i&gt;&lt;/button&gt;
           &lt;audio id=&quot;audio-preview&quot; &gt;&lt;/audio&gt;

This is the javascript code

function fileSelect(e){
   // console.log(e.target.files[0].name);
    var sound = document.getElementById(&#39;audio-preview&#39;);
    var reader = new FileReader();
    reader.onload = function(e) {
        sound.src = this.result;
        sound.controls = true;
        sound.play();
        };
    reader.readAsDataURL(e.target.files[0]);
}

And one more thing when audio is playing the multiplayer is not shown.
Please any one help to solve my issue .
Thanks in Advance,

答案1

得分: 1

你不需要使用FileReader来获取base64 URL,这是一种浪费时间的绕过方式,需要无谓的编码/解码。而是使用URL.createObjectURL

const selectFile = document.querySelector('#selectFile')
const audioFile = document.querySelector('#audioFile')
const sound = document.getElementById('audio-preview')

selectFile.addEventListener('click', () => audioFile.click())

function fileSelect(evt) {
  sound.src = URL.createObjectURL(evt.target.files[0])
  sound.controls = true
  sound.play()
}

audioFile.addEventListener('change', fileSelect)
<input type="file" id="audioFile" accept="audio/*">
<button id="selectFile">Select file</button>
<audio id="audio-preview"></audio><br>
<meta name="color-scheme" content="dark light">
英文:

You don't need to use the FileReader to get a base64 url. that's a wasteful round about way that requires encoding/decoding in vain. instead use URL.createObjectURL

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const selectFile = document.querySelector(&#39;#selectFile&#39;)
const audioFile = document.querySelector(&#39;#audioFile&#39;)
const sound = document.getElementById(&#39;audio-preview&#39;)

selectFile.addEventListener(&#39;click&#39;, () =&gt; audioFile.click())

function fileSelect (evt){
  sound.src = URL.createObjectURL(evt.target.files[0])
  sound.controls = true
  sound.play()
}

audioFile.addEventListener(&#39;change&#39;, fileSelect)

<!-- language: lang-html -->

&lt;input type=&quot;file&quot; id=&quot;audioFile&quot; accept=&quot;audio/*&quot;&gt;
&lt;button id=&quot;selectFile&quot;&gt;Select file&lt;/button&gt;
&lt;audio id=&quot;audio-preview&quot;&gt;&lt;/audio&gt;&lt;br&gt;
&lt;meta name=&quot;color-scheme&quot; content=&quot;dark light&quot;&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月14日 02:09:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定