Created
January 12, 2018 08:28
-
-
Save Shuumatsu/8543a172de13767b112dd44d2600c9f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cache = new WeakMap() | |
export default src => new Promise((resolve, reject) => { | |
if (cache.has(src)) | |
resolve(cache.get(src)) | |
const audio = document.createElement('audio') | |
audio.addEventListener('loadedmetadata', e => { | |
const duration = audio.duration | |
cache.set(src, duration) | |
resolve(duration) | |
}) | |
audio.addEventListener('error', reject) | |
audio.src = src | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment