简体   繁体   English

如何在 Vue 中检索 mp3 或 wav 音频文件的持续时间

[英]How to retrive the duration of a mp3 or wav audio file in Vue

Im trying to achive when i upload an audio file to see te duration of the uploaded mp3 or wav, but i don't know how to do that in vue, any sugestions ?当我上传音频文件以查看上传的 mp3 或 wav 的持续时间时,我试图实现,但我不知道如何在 vue 中做到这一点,有什么建议吗? I have find this script but i don't have a clue how to manage'it with vue.我找到了这个脚本,但我不知道如何使用 vue 管理它。 Any help appreciated, thanks.任何帮助表示赞赏,谢谢。

Javascript: Javascript:

var objectUrl;

    $("#audio").on("canplaythrough", function(e){
        var seconds = e.currentTarget.duration;
        var duration = moment.duration(seconds, "seconds");
        
        var time = "";
        var hours = duration.hours();
        if (hours > 0) { time = hours + ":" ; }
        
        time = time + duration.minutes() + ":" + duration.seconds();
        $("#duration").text(time);
        
        URL.revokeObjectURL(objectUrl);
    });
    
    $("#file").change(function(e){
        var file = e.currentTarget.files[0];
       
        $("#filename").text(file.name);
        $("#filetype").text(file.type);
        $("#filesize").text(file.size);
        
        objectUrl = URL.createObjectURL(file);
        $("#audio").prop("src", objectUrl);
    });

http://jsfiddle.net/derickbailey/s4P2v/ http://jsfiddle.net/derickbailey/s4P2v/

What i have tried so far but in console i get: 0:00:到目前为止我已经尝试过但在控制台中我得到:0:00:

 <input type="file" id="file" ref="file" v-on:change="handleFileUpload()"/>
  <button @click="submitAudioFile"> SUBMIT</button>

  handleFileUpload () { 
      this.file = this.$refs.file.files[0]  
    },
submitAudioFile () {
     let objectUrl = ''
          const myAudio = this.$refs.file.files[0]
          objectUrl = URL.createObjectURL(myAudio)
          const seconds = myAudio.duration
          const duration = moment.duration(seconds, 'seconds')
        
          let time = ''
          const hours = duration.hours()
          if (hours > 0) { time = `${hours  }:`  }
        
          time = `${time + duration.minutes()  }:${  duration.seconds()}`
          console.log(time)
        
          URL.revokeObjectURL(objectUrl)
}

I have done this with:我已经这样做了:

duration () {    
      const files = document.getElementById('file').files
      const file = files[0] 
      const reader = new FileReader()
      const audio = document.createElement('audio')
      reader.onload = function (e) {
        audio.src = e.target.result
        audio.addEventListener('durationchange', function () { 
          const seconds  = audio.duration
          const duration = moment.duration(seconds, 'seconds')
          let time = ''
          const hours = duration.hours()
          if (hours > 0) { time = `${hours}:` } 
          time = `${time + duration.minutes()  }:${duration.seconds()}`
          console.log(time)
        }, false) 
        audio.addEventListener('onerror', function () {
          alert('Cannot get duration of this file.')
        }, false)
      }
      reader.readAsDataURL(file)
    }

You have to use onloadmetadata event listener to access duration of audio.您必须使用 onloadmetadata 事件侦听器来访问音频的持续时间。

myAudio.onloadedmetadata = ()=> {
  console.log(myAudio.duration)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AngularJS - 解码字节数组和播放音频文件(Wav / MP3) - AngularJS - Decode Byte Array and Play Audio File (Wav/MP3) 将 Blob WebM 转换为音频文件(WAV 或 MP3) - Converting Blob WebM to audio file (WAV or MP3) 如何获取 mp3 文件的大小和持续时间? - How to get the size and duration of an mp3 file? 如何将.wav数据转换为.mp3并在Meteor应用程序中播放音频 - How to convert .wav data to .mp3 and play audio in Meteor app 如何在 Vue3 中播放 mp3 文件 - How to play mp3 file in Vue3 如何将普通音频(如.mp3或.wav)中的BLOB对象(音频)转换为更新到数据库 - how to convert a BLOB object (audio) in a normal audio like .mp3 or .wav for update to database 智能带宽节省:如何阻止 HTML5 mp3 音频文件不必要地加载,同时显示总长度? - Smart bandwith saving: how to stop HTML5 mp3 audio file from loading unneccessarily, whilst showing the total lenght duration? 使用“ Web Audio API”和“ AudioContext”使用JavaScript获取MP3文件的持续时间 - Getting the duration of an MP3 file with JavaScript using “Web Audio API” and “AudioContext” 如何使用npm mp3进行WAV - How to use npm mp3 to wav Javascript帮助-随机Wav / mp3的音频播放波形 - Javascript help - audio play waveform of random Wav/mp3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM