javascript – 如何使用Jquery停止所有音频播放

前端之家收集整理的这篇文章主要介绍了javascript – 如何使用Jquery停止所有音频播放前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 jquery mobile更新页面中的div以播放一些动画.

动画也通过使用document.createElement(‘audio’)播放声音;

我的问题是,当我将页面更新为另一个动画时,旧的声音继续播放.

对不起,这是我的第一个问题,如果我似乎没有正确地说它,我道歉.

这是我的代码..

这是要加载到#animation div中的动画的代码

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. margin: 0px;
  7. padding: 0px;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <div id="container"></div>
  13. <script src="scripts/kinetic-v4.3.2.min.js"></script>
  14. <script src="scripts/jquery-1.9.1.js"></script>
  15. <script>
  16. /*BABY SCENE*/
  17. var stage = new Kinetic.Stage({
  18. container: 'container',width: 578,height: 400
  19. });
  20. var babyLayer = new Kinetic.Layer();
  21. var backLayer = new Kinetic.Layer();
  22.  
  23.  
  24. var imageObj = new Image();
  25. var backObj = new Image();
  26. imageObj.onload = function() {
  27. var baby = new Kinetic.Image({
  28. x: stage.getWidth() / 2 -100,y: stage.getHeight() / 2 -100,image: imageObj,width: 200,height: 200,opacity: 0.0
  29. });
  30.  
  31. var background = new Kinetic.Image({
  32. x: 0,y: 0,image: backObj,height: 400,opacity: 0.0
  33. });
  34.  
  35. // add the shape to the layer
  36. babyLayer.add(baby);
  37. backLayer.add(background);
  38.  
  39. // add the layer to the stage
  40. stage.add(babyLayer);
  41. stage.add(backLayer);
  42. babyLayer.moveToTop();
  43. babyLayer.draw();
  44.  
  45. /*ANIMATION TIMELINE
  46. *
  47. * FIRST EVENT: FADE IN BABY
  48. * SECOND EVENT: BABY TRANSITION
  49. * THIRD EVENT: FADE IN BACKGROUND
  50. */
  51.  
  52. /*1) Fade in Baby*/
  53. setTimeout(function() {
  54. baby.transitionTo({
  55. opacity: 1,duration: 1
  56. });
  57. },200);
  58.  
  59. setTimeout(function() {
  60. /*JQuery Baby Speech*/
  61. $(document).ready(function() {
  62. var babySpeech = document.createElement('audio');
  63. babySpeech.setAttribute('src','../Animations/sounds/babyspeech.mp3');
  64. babySpeech.setAttribute('autoplay','autoplay');
  65. //audioElement.load()
  66. $.get();
  67. babySpeech.addEventListener("load",function() {
  68. babySpeech.play();
  69. },true);
  70. });
  71. },800);
  72.  
  73. /*2) Baby Transition*/
  74. setTimeout(function() {
  75. baby.transitionTo({
  76. x: 140,y: stage.getHeight() / 2 + 59,width: 106,height: 118,opacity: 1,duration: 3
  77. });
  78. },3000);
  79.  
  80. setTimeout(function() {
  81. /*JQuery Baby Giggle*/
  82. $(document).ready(function() {
  83. var baby = document.createElement('audio');
  84. baby.setAttribute('src','../Animations/sounds/baby.mp3');
  85. baby.setAttribute('autoplay','autoplay');
  86. //audioElement.load()
  87. $.get();
  88. baby.addEventListener("load",function() {
  89. baby.play();
  90. },3000);
  91.  
  92. /*3) Fade in Background*/
  93. setTimeout(function() {
  94. background.transitionTo({
  95. opacity: 1,3200);
  96.  
  97. setTimeout(function() {
  98. /*Second JQuery Baby Speech*/
  99. $(document).ready(function() {
  100. var babySpeech = document.createElement('audio');
  101. babySpeech.setAttribute('src',8700);
  102.  
  103.  
  104. };
  105. imageObj.src = '../Animations/images/baby.png';
  106. backObj.src = '../Animations/images/background/babyroom.jpg';
  107.  
  108. </script>

这是主页面代码.当您单击下一个按钮时,它会获取要从数组加载到#animation中的动画页面文件名.

  1. <div id="nav" data-role="content" style="width: 600px; margin: 0 auto; text-align: center; position: relative; top:450px">
  2. <a href="#animation" data-role="button" data-inline="true" id ="back">Back</a>
  3. <a href="#animation" data-role="button" data-inline="true" data-theme="b" id="next">Next</a>
  4. <script>
  5. var count=0;
  6. var link_array = ['baby','bed','book','cat','chair','daddy','dog','mummy','shoe','teddy'];
  7. $("#next").click(function(e) {
  8. if(count!==9)
  9. {
  10. $('audio').stop();
  11. count+=1;
  12. }
  13. $('#animation1wl').load('../Animations/' + link_array[count] + '.html');
  14. $('#animation1wl').trigger('create');
  15. });
  16. $("#back").click(function(e) {
  17. if(count !==0)
  18. {
  19. count-=1;
  20. }
  21. $('#animation1wl').load('../Animations/' + link_array[count] + '.html');
  22. $('#animation1wl').trigger('create');
  23. });
  24. </script>
  25. </div>
  26.  
  27. <div id="animation" data-role="content" style="width: 600px; margin: 0 auto; text-align: left">Problem Loading Resource LNW</div>

当我单击前进或后退按钮时,我希望加载到#animation的上一个动画中的音频停止播放..

这是在#animation中呈现的代码

  1. <div id="animation1wl" data-role="content" style="width: 600px; margin: 0 auto; text-align: left" class="ui-content" role="main">
  2.  
  3.  
  4. <style>
  5. body {
  6. margin: 0px;
  7. padding: 0px;
  8. }
  9. </style>
  10.  
  11.  
  12. <div id="container"><div style="position: relative; display: inline-block; width: 578px; height: 400px;" class="kineticjs-content"><canvas width="578" style="width: 578px; height: 400px; position: absolute;" height="400"></canvas><canvas width="578" style="width: 578px; height: 400px; position: absolute;" height="400"></canvas></div></div>
  13. <script src="scripts/kinetic-v4.3.2.min.js"></script>
  14. <script src="scripts/jquery-1.9.1.js"></script>
  15. <script>
  16. /*BABY SCENE*/
  17. var stage = new Kinetic.Stage({
  18. container: 'container',height: 400
  19. });
  20. var babyLayer = new Kinetic.Layer();
  21. var backLayer = new Kinetic.Layer();
  22.  
  23.  
  24. var imageObj = new Image();
  25. var backObj = new Image();
  26. imageObj.onload = function() {
  27. var baby = new Kinetic.Image({
  28. x: stage.getWidth() / 2 -100,opacity: 0.0
  29. });
  30.  
  31. var background = new Kinetic.Image({
  32. x: 0,opacity: 0.0
  33. });
  34.  
  35. // add the shape to the layer
  36. babyLayer.add(baby);
  37. backLayer.add(background);
  38.  
  39. // add the layer to the stage
  40. stage.add(babyLayer);
  41. stage.add(backLayer);
  42. babyLayer.moveToTop();
  43. babyLayer.draw();
  44.  
  45. /*ANIMATION TIMELINE
  46. *
  47. * FIRST EVENT: FADE IN BABY
  48. * SECOND EVENT: BABY TRANSITION
  49. * THIRD EVENT: FADE IN BACKGROUND
  50. */
  51.  
  52. /*1) Fade in Baby*/
  53. setTimeout(function() {
  54. baby.transitionTo({
  55. opacity: 1,duration: 1
  56. });
  57. },200);
  58.  
  59. setTimeout(function() {
  60. /*JQuery Baby Speech*/
  61. $(document).ready(function() {
  62. var babySpeech = document.createElement('audio');
  63. babySpeech.setAttribute('src','../Animations/sounds/babyspeech.mp3');
  64. babySpeech.setAttribute('autoplay','autoplay');
  65. //audioElement.load()
  66. $.get();
  67. babySpeech.addEventListener("load",function() {
  68. babySpeech.play();
  69. },true);
  70. });
  71. },800);
  72.  
  73. /*2) Baby Transition*/
  74. setTimeout(function() {
  75. baby.transitionTo({
  76. x: 140,duration: 3
  77. });
  78. },3000);
  79.  
  80. setTimeout(function() {
  81. /*JQuery Baby Giggle*/
  82. $(document).ready(function() {
  83. var baby = document.createElement('audio');
  84. baby.setAttribute('src','../Animations/sounds/baby.mp3');
  85. baby.setAttribute('autoplay','autoplay');
  86. //audioElement.load()
  87. $.get();
  88. baby.addEventListener("load",function() {
  89. baby.play();
  90. },3000);
  91.  
  92. /*3) Fade in Background*/
  93. setTimeout(function() {
  94. background.transitionTo({
  95. opacity: 1,3200);
  96.  
  97. setTimeout(function() {
  98. /*Second JQuery Baby Speech*/
  99. $(document).ready(function() {
  100. var babySpeech = document.createElement('audio');
  101. babySpeech.setAttribute('src',8700);
  102.  
  103. console.log($('animation1wl').find('audio')[0]);
  104.  
  105. };
  106. imageObj.src = '../Animations/images/baby.png';
  107. backObj.src = '../Animations/images/background/babyroom.jpg';
  108.  
  109. </script>
  110.  
  111. </div>

任何帮助都会很棒..

非常感谢.

解决方法

你可以这样做:
  1. $('audio').each(function(){
  2. this.pause(); // Stop playing
  3. this.currentTime = 0; // Reset time
  4. });

猜你在找的jQuery相关文章