Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript JS audio player not working on Tumblr blog

xcoder

Coder
Hey all, first time here. Just would like to get some help in knowing why my audio player is not working on my blog. It is showing 0:00 for the duration time. I have the code here to look through. If you need my blog's link, please let me know. Thanks.


JavaScript:
    import lottieWeb from 'https://cdn.skypack.dev/lottie-web';
    class AudioPlayer extends HTMLElement {
      constructor() {
        super();
        const template = document.querySelector('template');
        const templateContent = template.content;
        const shadow = this.attachShadow({
          mode: 'open'
        });
        shadow.appendChild(templateContent.cloneNode(true));
      }
      connectedCallback() {
        everything(this);
      }
    }
    const everything = function(element) { 
  const shadow = element.shadowRoot;

    const audioPlayerContainer = shadow.getElementById('audio-player-container');
    const playIconContainer = shadow.getElementById('play-icon');
    const seekSlider = shadow.getElementById('seek-slider');
    const volumeSlider = shadow.getElementById('volume-slider');
    const muteIconContainer = shadow.getElementById('mute-icon');
    const audio = shadow.querySelector('audio');
    const durationContainer = shadow.getElementById('duration');
    const currentTimeContainer = shadow.getElementById('current-time');
    const outputContainer = shadow.getElementById('volume-output');
    let playState = 'play';
    let muteState = 'unmute';
    let raf = null;
      audio.src = element.getAttribute('data-src');
      const playAnimation = lottieWeb.loadAnimation({
        container: playIconContainer,
        path: 'https://maxst.icons8.com/vue-static/landings/animated-icons/icons/pause/pause.json',
        renderer: 'svg',
        loop: false,
        autoplay: false,
        name: "Play Animation",
      });
      const muteAnimation = lottieWeb.loadAnimation({
        container: muteIconContainer,
        path: 'https://maxst.icons8.com/vue-static/landings/animated-icons/icons/mute/mute.json',
        renderer: 'svg',
        loop: false,
        autoplay: false,
        name: "Mute Animation",
      });
      playAnimation.goToAndStop(14, true);
      const whilePlaying = () => {
        seekSlider.value = Math.floor(audio.currentTime);
        currentTimeContainer.textContent = calculateTime(seekSlider.value);
        audioPlayerContainer.style.setProperty('--seek-before-width', `${seekSlider.value / seekSlider.max * 100}%`);
        raf = requestAnimationFrame(whilePlaying);
      }
      const showRangeProgress = (rangeInput) => {
        if (rangeInput === seekSlider) audioPlayerContainer.style.setProperty('--seek-before-width', rangeInput.value / rangeInput.max * 100 + '%');
        else audioPlayerContainer.style.setProperty('--volume-before-width', rangeInput.value / rangeInput.max * 100 + '%');
      }
      const calculateTime = (secs) => {
       const minutes = Math.floor(secs / 60);
       const seconds = Math.floor(secs % 60);
       const returnedSeconds = seconds < 10 ? `0${seconds}`:`${seconds}`;
       return `${minutes}:${returnedSeconds}`;
     }
      const displayDuration = () => {
        durationContainer.textContent = calculateTime(audio.duration);
      }
      const setSliderMax = () => {
        seekSlider.max = Math.floor(audio.duration);
      }
      const displayBufferedAmount = () => {
        const bufferedAmount = Math.floor(audio.buffered.end(audio.buffered.length - 1));
        audioPlayerContainer.style.setProperty('--buffered-width', `${(bufferedAmount / seekSlider.max) * 100}%`);
      }
      if (audio.readyState > 0) {
        displayDuration();
        setSliderMax();
        displayBufferedAmount();
      } else {
        audio.addEventListener('loadedmetadata', () => {
          displayDuration();
          setSliderMax();
          displayBufferedAmount();
        });
      }
      playIconContainer.addEventListener('click', () => {
        if (playState === 'play') {
          audio.play();
          playAnimation.playSegments([14, 27], true);
          requestAnimationFrame(whilePlaying);
          playState = 'pause';
        } else {
          audio.pause();
          playAnimation.playSegments([0, 14], true);
          cancelAnimationFrame(raf);
          playState = 'play';
        }
      });
      muteIconContainer.addEventListener('click', () => {
        if (muteState === 'unmute') {
          muteAnimation.playSegments([0, 15], true);
          audio.muted = true;
          muteState = 'mute';
        } else {
          muteAnimation.playSegments([15, 25], true);
          audio.muted = false;
          muteState = 'unmute';
        }
      });
      
      audio.addEventListener("ended", () => {
  playIconContainer.click();
  cancelAnimationFrame(raf);
  currentTimeContainer.textContent = "0.00";
  seekSlider.value = "0";
});
      
      audio.addEventListener('progress', displayBufferedAmount);
      seekSlider.addEventListener('input', (e) => {
        showRangeProgress(e.target);
        currentTimeContainer.textContent = calculateTime(seekSlider.value);
        if (!audio.paused) {
          cancelAnimationFrame(raf);
        }
      });
      seekSlider.addEventListener('change', () => {
        audio.currentTime = seekSlider.value;
        if (!audio.paused) {
          requestAnimationFrame(whilePlaying);
        }
      });
      volumeSlider.addEventListener('input', (e) => {
        const value = e.target.value;
        showRangeProgress(e.target);
        outputContainer.textContent = value;
        audio.volume = value / 100;
      });
      if ('mediaSession' in navigator) {
        navigator.mediaSession.metadata = new MediaMetadata({
          title: 'Evenstar',
          artist: 'Howard Shore',
          album: 'The Lord of the Rings'
        });
        navigator.mediaSession.setActionHandler('play', () => {
          if (playState === 'play') {
            audio.play();
            playAnimation.playSegments([14, 27], true);
            requestAnimationFrame(whilePlaying);
            playState = 'pause';
          } else {
            audio.pause();
            playAnimation.playSegments([0, 14], true);
            cancelAnimationFrame(raf);
            playState = 'play';
          }
        });
        navigator.mediaSession.setActionHandler('pause', () => {
          if (playState === 'play') {
            audio.play();
            playAnimation.playSegments([14, 27], true);
            requestAnimationFrame(whilePlaying);
            playState = 'pause';
          } else {
            audio.pause();
            playAnimation.playSegments([0, 14], true);
            cancelAnimationFrame(raf);
            playState = 'play';
          }
        });
        navigator.mediaSession.setActionHandler('seekbackward', (details) => {
          audio.currentTime = audio.currentTime - (details.seekOffset || 10);
        });
        navigator.mediaSession.setActionHandler('seekforward', (details) => {
          audio.currentTime = audio.currentTime + (details.seekOffset || 10);
        });
        navigator.mediaSession.setActionHandler('seekto', (details) => {
          if (details.fastSeek && 'fastSeek' in audio) {
            audio.fastSeek(details.seekTime);
            return;
          }
          audio.currentTime = details.seekTime;
        });
        navigator.mediaSession.setActionHandler('stop', () => {
          audio.currentTime = 0;
          seekSlider.value = 0;
          audioPlayerContainer.style.setProperty('--seek-before-width', '0%');
          currentTimeContainer.textContent = '0:00';
          if (playState === 'pause') {
            playAnimation.playSegments([0, 14], true);
            cancelAnimationFrame(raf);
            playState = 'play';
          }
        });
      }
    }
    customElements.define('audio-player', AudioPlayer)
 
What error are you stuck on then ? Please tell me you have made an effort to understand and fix at least some of them.
The one I shared a screenshot of. I really don't understand, as I am not a programmer. I'm just a blogger. I'd really appreciate some help in fixing this. I want to be able to play music from the audio player on my blog again, as I have before.
 
The one I shared a screenshot of. I really don't understand, as I am not a programmer. I'm just a blogger. I'd really appreciate some help in fixing this. I want to be able to play music from the audio player on my blog again, as I have before.
You did not share a screenshot of an error, only of some checking tool reporting there were too many errors 🙄 You really don't need to be a programmer to realize that without a list of these errors nothing can be done.

When you say "as I have done before" does that imply that this (or something else with the same functionality) has actually worked ? Then how come it does not work now ? What has changed ?
 
You did not share a screenshot of an error, only of some checking tool reporting there were too many errors 🙄 You don't need to be a programmer to realize that without a list of these errors nothing can be done.
When you say "as I have done before" does that imply that this (or something else with the same functionality) has actually worked ? Then how come it does not work now ? What has changed ?
Yes, too many errors. I can't figure that out. Yes it has actually worked. I don't know why it doesn't work now. As far as I know, I had it working on my laptop during the holidays, then when switched to desktop I realized it didn't. I wonder if that is why? Other than that, the only other thing new is that I have done a new blog entry on Tumblr in December. I have not touched any code since.
 
"switched to desktop" ? This is getting murkier by the minute. Why and how did you make that switch ? You can't just change stuff around and then look at someone else to sort out, without having any details, why it stopped working. And how is having done a new blog entry on Tumblr in December relevant ?
 
"switched to desktop" ? This is getting murkier by the minute. Why and how did you make that switch ? You can't just change stuff around and then look at someone else to sort out, without having any details, why it stopped working. And how is having done a new blog entry on Tumblr in December relevant ?
I had to switch to desktop because my laptop was not working well. I don't know if that's relevant to the issue I'm having. My JS code is in Tumblr, in the theme assets section. Is how I imported it to there.

Ok tell me the details you need, and I'll try to list them out. I may not be able to list all of them, but I will try.
 
The list of errors generated by that tool which said you have too many errors might help...
Or it might not. I cannot guarantee I will have the know-how (or the chops, for that matter) to help you further.
 
Back
Top Bottom