JavaScript Why does audio stop playing on scroll? why does audio stop when screen recording,why does audio

Here's a CodePen of what I'm trying to achieve:

http://codepen.io/golfecholima/pen/GJqEOm

It's a little audio bug that sits in the bottom right corner. When a user scrolls past a certain element on the page the <audio src=""> changes. Here's the jQuery:

// BRING IN YOUR AUDIO FILES HERE

var audioOne = 'https://d63kb4t2ifcex.cloudfront.net/201505horton/mp3/Horton_ImNotThisPicture.mp3';  // replace these with your audio files 
var audioTwo = 'https://d63kb4t2ifcex.cloudfront.net/201505horton/mp3/Horton_WeHadBackThen.mp3';  // replace these with your audio files
var audioThree = 'https://d63kb4t2ifcex.cloudfront.net/201505horton/mp3/Horton_IveNeverBeen.mp3';  // replace these with your audio files
var audioFour = 'https://d63kb4t2ifcex.cloudfront.net/201505horton/mp3/Dukakis_OneOfTheValuesofFurlough.mp3';  // replace these with your audio files

// SET YOUR SCROLL BREAKPOINTS HERE 

var breakOne = 0;
var breakTwo = $('.audio-two').offset().top - $(window).height();
var breakThree = $('.audio-three').offset().top - $(window).height();
var breakFour = $('.audio-four').offset().top - $(window).height();


// SET YOUR AUDIO FILE TITLES

var titleOne = 'LISTEN: The first audio file';
var titleTwo = 'LISTEN: The second audio file';
var titleThree = 'LISTEN: The third audio file';
var titleFour = 'LISTEN: The fourth audio file';

// DO THE STUFF

var audioElement = document.createElement('audio');
    audioElement.setAttribute('src', audioOne);
    audioElement.setAttribute('preload', 'auto');
    //audioElement.load()
    $.get();
    audioElement.addEventListener("load", function() {
    audioElement.play();
    }, true);

    // controls the playing of the audio

    $('.audioBug').click(function() {

        if ($(this).hasClass('playing')) {
            audioElement.pause();
            $(this).removeClass('playing');
        } else {
            audioElement.play();
            $(this).addClass('playing');
        }
    });

    // collapses the bug 5 seconds after loading

    function removeBumper() {
        setTimeout(function() {
            $('.audioBug > h6').fadeOut(500, function() {
                $('.audioBug').animate({width: '4rem'}, 'fast');
            });
        }, 5000);
    }

    //hovering over the bug expands it to reveal the audio title. 

    $('.audioBug').hover(function() {
        $(this).animate({width: '28rem'}, 'fast');
        $('.audioBug > h6').fadeIn('slow');
    }, function() {
        setTimeout(function() {
            $('.audioBug > h6').fadeOut(500, function() {
                $('.audioBug').animate({width: '4rem'}, 'slow');
            });
        }, 1000);
    });

    //page loads, audio bug expands to reveal title, then collapses again
    setTimeout(function previewBug() {
        $('.audioBug').animate({width: '28rem'}, 'fast');
    }, 1000);
    setTimeout(function() {
        $('.audioBug > h6').fadeIn(300, function() {
            removeBumper();
        });
    }, 2700);

    //scrolling through set ranges changes the audio file/title and expands the bug anew

    $(window).scroll(function() {
        if ($(this).scrollTop() > breakOne && $(this).scrollTop() < breakTwo) {
            audioElement.setAttribute('src', audioOne);
            $('.audioBug > h6').text(titleOne);
        } 
        else if ($(this).scrollTop() > breakTwo && $(this).scrollTop() < breakThree) {
            audioElement.setAttribute('src', audioTwo);
            $('.audioBug > h6').text(titleTwo);
        } 
        else if ($(this).scrollTop() > breakThree && $(this).scrollTop() < breakFour) {
            audioElement.setAttribute('src', audioThree);
            $('.audioBug > h6').text(titleThree);
        } 
        else if ($(this).scrollTop() > breakFour) {
            audioElement.setAttribute('src', audioFour);
            $('.audioBug > h6').text(titleFour);
        }
    });

It functions how I expect except that as soon as you start scrolling the audio stops even if the next break point hasn't been reached. My understanding is that .scroll() fires any time there's scrolling activity. So I can see where this would disrupt play if the function is constantly check to see which file should be in place.

Is there any way to keep that from happening. I've seen some AJAXy things that look like they might be the right idea but I'm too noobish to have figured that out just yet.

I've tried setTimeout which doesn't seem to help.

(I'm aware that the hover is a little wonky -- not worried about that)

Answer:1

This is a quasi follow-up of Detect version of Java using JavaScript I have the restriction that the javascript should not initialize the java plugin (also not use deployJava, which in turn uses the ...

This is a quasi follow-up of Detect version of Java using JavaScript I have the restriction that the javascript should not initialize the java plugin (also not use deployJava, which in turn uses the ...

  1. find java version linux
  2. find java version mac
  3. find java version windows
  4. find java version cmd
  5. find java version on windows 10
  6. find java version mac terminal
  7. find java version ubuntu
  8. find java version of jar
  9. find java version from class file
  10. find java version installed
  11. find java version in terminal
  12. find java version in unix
  13. find java version linux command
  14. find java version using cmd
  15. find java version on pc
  16. find java version command
  17. find java version intellij
  18. find java version powershell
  19. find java version mac os
  20. find java version in eclipse

I feel like this should be a simple thing to fix, but I just can't get it to work. I am running a jQuery store locator script with google maps. The script requires two javascript files, jquery, and ...

I feel like this should be a simple thing to fix, but I just can't get it to work. I am running a jQuery store locator script with google maps. The script requires two javascript files, jquery, and ...

  1. jquery document ready shorthand
  2. jquery document ready function
  3. jquery document ready vs load
  4. jquery document ready not working
  5. jquery document ready not firing
  6. jquery document ready example
  7. jquery document ready deprecated
  8. jquery document ready shortcut
  9. jquery document ready alternative
  10. jquery document ready event
  11. jquery document ready vanilla js
  12. jquery document ready multiple times
  13. jquery document ready wordpress
  14. jquery document ready ajax
  15. jquery document ready without jquery
  16. jquery document ready vs onload
  17. jquery document ready order
  18. jquery document ready delay
  19. jquery document ready call function
  20. jquery document ready es6

So I know about jQuery's .on() function to give dynamically added element's event handlers. E.g I've used it this way $(document).on("change",Items,function(){/*Code here*/}); But is there a way to ...

So I know about jQuery's .on() function to give dynamically added element's event handlers. E.g I've used it this way $(document).on("change",Items,function(){/*Code here*/}); But is there a way to ...

  1. jquery append html
  2. jquery append after
  3. jquery append before
  4. jquery append not working
  5. jquery append option to select
  6. jquery appendto
  7. jquery append to body
  8. jquery append class
  9. jquery append row to table
  10. jquery append text to div
  11. jquery append li to ul
  12. jquery append vs appendto
  13. jquery append to array
  14. jquery append first
  15. jquery append image
  16. jquery append multiple
  17. jquery append to textarea
  18. jquery append sibling
  19. jquery append to string
  20. jquery append to parent

On our website, we have a page where we have 5 images that are buttons. When you click each button, more detailed information appears using the onclick function. What we would like to do is pull the ...

On our website, we have a page where we have 5 images that are buttons. When you click each button, more detailed information appears using the onclick function. What we would like to do is pull the ...

  1. redirect from anchor
  2. redirect with anchor tag
  3. redirect with anchor link
  4. redirect to action from anchor tag