首页 \ 问答 \ 如何在音频HTML5上淡入/淡出(how to have fade in/out on audio HTML5)

如何在音频HTML5上淡入/淡出(how to have fade in/out on audio HTML5)

我正在创建一个简单的背景音乐,只有一个按钮可以播放和停止音乐。 但我想补充淡化。 但不起作用。

我的代码:

var beepTwo = $("#musicBeat")[0];
beepTwo.play();



$("#dan").click(function() {
  if (beepTwo.paused == false) {
      beepTwo.pause();

  } else {
      beepTwo.play();
  }
});


$beepTwo.animate({volume: newVolume}, 1000);

的jsfiddle

我发现$audio.animate({volume: newVolume}, 1000); 在另一篇文章,但不适合我,或者我没有正确使用它。 无论如何,我想按下按钮时淡入淡出并淡出。 所以它用fadeIn播放并用fadeOut停止。 我怎样才能做到这一点?


I am creating a simple background music which has only one button to play and stop the music. But I would like to add fade to it. But does not work.

My code:

var beepTwo = $("#musicBeat")[0];
beepTwo.play();



$("#dan").click(function() {
  if (beepTwo.paused == false) {
      beepTwo.pause();

  } else {
      beepTwo.play();
  }
});


$beepTwo.animate({volume: newVolume}, 1000);

JSFiddle

I found $audio.animate({volume: newVolume}, 1000);in another post but does not work for me or maybe I didnt use it correctly. Anyway I want to have a fade in and fade out when the button is pressed. So it plays with fadeIn and stops with fadeOut. How can I do that?

更新时间:2022-11-12 11:11

最满意答案

你必须注意HTMLAudioElement( $('#musicBeat')[0] )和jQuery Object( $('#musicBeat') )之间的$('#musicBeat')

play是HTMLAudioElement的一个方法,所以你必须通过它访问它(就像你一样),但.animate是一个jQuery方法,只能在jQuery对象上调用。

而且你必须指定newVolume(不能将其留空)。

var beepTwo = $("#musicBeat");
beepTwo[0].play();

$("#dan").click(function() {  
    if (beepTwo[0].paused == false) {
        beepTwo.animate({volume: 0}, 2000, 'swing', function() {
            // really stop the music 
            beepTwo[0].pause();   
        });
     } else {
         beepTwo[0].play();  
         beepTwo.animate({volume: 1}, 2000);
     }
});

You have to watch out for the difference between the HTMLAudioElement ($('#musicBeat')[0]) and the jQuery Object ($('#musicBeat')).

play is a method of the HTMLAudioElement, so you'll have to access it over that (as you did), but .animate is a jQuery method and can only be called on a jQuery object.

And you have to specify newVolume (can't leave it empty).

var beepTwo = $("#musicBeat");
beepTwo[0].play();

$("#dan").click(function() {  
    if (beepTwo[0].paused == false) {
        beepTwo.animate({volume: 0}, 2000, 'swing', function() {
            // really stop the music 
            beepTwo[0].pause();   
        });
     } else {
         beepTwo[0].play();  
         beepTwo.animate({volume: 1}, 2000);
     }
});

相关问答

更多
  • poster属性的行为实际上是由
  • Firefox不支持MP3。 它不会显示回退消息,因为它支持音频标签。 https://developer.mozilla.org/En/Media_formats_supported_by_the_audio_and_video_elements#MPEG_H.264_(AAC_or_MP3) Firefox doesn't support MP3. It won't show the fallback message because it supports the audio tag. https:// ...
  • 网络摄像头和麦克风支持不是HTML 5规范的特色 新的API 除了指定标记之外,HTML5还指定脚本应用程序编程接口(API)。 现有的文档对象模型(DOM)接口被扩展并记录了事实上的功能。 还有新的API,例如: 立即模式的画布元素 2D绘图定时媒体播放 离线存储数据库(离线网站 应用程序)文件编辑 拖放交叉文档 消息(web消息)浏览器 历史管理MIME类型和 协议处理器注册。 微观数据 一些新功能是HTML5的一部分,主要是因为没有志愿者将HTML5拆分并保留这些功能的单独规范。 Webcam and ...
  • 这似乎工作( jsFiddle演示 ): test_audio_played = false; $('
  • 如果你只是要下载文件,你不需要
  • 不会喜欢这个答案,但Chromecast根本不支持自签名证书。 它构建在Chromium安全模型中。 如果您可以将证书装载到证书商店中,它就可以工作(Android和Fire平板电脑和电视可以做到这一点),但这可能会有所帮助,但Chromecast(和FireStick)无法做到这一点。 Not gonna like this answer, but Chromecast simply won't support self-signed certs. It is built into the Chromiu ...
  • 你必须注意HTMLAudioElement( $('#musicBeat')[0] )和jQuery Object( $('#musicBeat') )之间的$('#musicBeat') 。 play是HTMLAudioElement的一个方法,所以你必须通过它访问它(就像你一样),但.animate是一个jQuery方法,只能在jQuery对象上调用。 而且你必须指定newVolume(不能将其留空)。 var beepTwo = $("#musicBeat"); beepTwo[0].play(); ...
  • avconv可以淡出视频,但似乎不会淡出音频。 我建议你改用sox。 语法是: sox song.mp3 faded.mp3 fade 5 240 8 在这个例子中, faded.mp3是你的源, faded.mp3是输出, 5是淡入时间(如果你只需要淡出,你可以将它保留为零), 240是音频的长度,并且8是淡出时间。 因此,对于您的具体示例,您将使用: sox SPEX_pilot_02.mp3 preteach-words.mp3 fade 0 25 4 资料来源: http : //archive ...
  • play(),pause()等来自HTMLMediaElement接口,这里记录: http : //www.w3.org/TR/html5/video.html#htmlmediaelement play(), pause() etc come from HTMLMediaElement interface, documented here: http://www.w3.org/TR/html5/video.html#htmlmediaelement
  • 您可以使用.scrollTop()来确定用户滚动的距离: $(document).ready(function() { var audioElm = $('#soundTour').get(0); audioElm.play(); var height = $(document).height() - $(window).height(); $(window).scroll(function() { audioElm.volume = 1 - $(wind ...

相关文章

更多

最新问答

更多
  • 在javascript中创建类以创建对象并在Java中创建类和对象之间的区别(Difference between creating a class in javascript to create an object and creating an class and object in Java)
  • Facebook API:将身份验证详细信息从Javascript SDK发送到PHP SDK(Facebook API: Send authentication detail from Javascript SDK to PHP SDK)
  • 如何停止队列动画jquery?(How can I stop queue animation jquery?)
  • 使用C#的井字游戏中的人工智能(Artificial Intelligence in Tic-Tac-Toe using C#)
  • 多少流量可以共享虚拟主机(对于Python Django站点)支持?(How Much Traffic Can Shared Web Hosting (for a Python Django site) support?)
  • 带有CIFilters的CAShapeLayer(CAShapeLayer with CIFilters)
  • 如何在Angular 2中读取JSON #text(How to read in Angular 2 the JSON #text)
  • 如何在xml中读取自闭标签的属性?(How to read self closing tag's attribute in xml?)
  • 无法使用http put将图像上传到亚马逊S3(Cannot upload image to amazon s3 using http put)
  • 文件结束无限循环(end of file infinite while-loop)
  • 在cpp的模板(template in cpp)
  • 在构建库时,clang和clang ++有什么区别?(What's the difference between clang and clang++ when building a library?)
  • ng类中的表达式(expression inside ng-class)
  • 在PHP中获取随机布尔值true / false(Get random boolean true/false in PHP)
  • 管道的高效分块用于严格的字节串(Efficient chunking of conduit for strict bytestring)
  • Python ternary_operator(如果其他标志做了其他操作,则执行其他操作)(Python ternary_operator (do-somthing if flag else do-another))
  • Sencha Touch面具发布(Sencha Touch mask ondisclosure)
  • 验证脚本上的通知[重复](Notices on validation script [duplicate])
  • 朋友功能(friend function)
  • 基于角坐标平移和变换平面几何(Translate and transform plane geometry based on corner coordinates)
  • Rails:'如果在本地运行'条件javascript标记包括(Rails: 'if running locally' conditional javascript tag include)
  • 解压文件(Unzipping files)
  • 使用ui-router以角度加载变量状态(loading in variable states with ui-router in angular)
  • 创建Azure云服务需要多长时间?(how long does it take to create an Azure Cloud Service? How to view log information?)
  • 指向整数的指针数组(Array of pointers to integers)
  • Laravel服务提供商没有看到我的包的主要类(Laravel service provider does not see the main class of my package)
  • 这个关于VSS / RSS / PSS / USS的解释是否准确?(Is this explanation about VSS/RSS/PSS/USS accurate?)
  • 在Django-Admin中通过row-id排序显示项目(Ordering the display items by row-id in Django-Admin)
  • 如何使用cythonize启用`--embed`?(How to enable `--embed` with cythonize?)
  • 用于将文本多行设置的Excel脚本(Excel script for ereasing text multiple rows)