Giter Site home page Giter Site logo

audio.js's Introduction

audio.js

HTML5的音频播放组件,兼容解决iOS系统下自动播放(autoplay)和循环播放(loop)的问题。

目前整体设计比较简单,不依赖任何库,主要是解决移动端h5页面背景音乐播放的问题,之后会根据需求再做升级优化。

参数

  • src - 音频的地址(字符串,建议使用mp3格式,体积可能会比较大但是兼容性很好)
  • autoPlay - 设置是否自动播放音频(布尔值,需要同时设置src
  • loop - 设置是否循环播放音频(布尔值,需要同时设置duration
  • duration - 音频的时长(数值,配合循环播放时使用,单位是秒)
  • onXXX - audio事件的回调响应(函数,例如onPlaying会在音频播放的时候执行此回调)

属性

  • .audio - 实际的音频对象

接口

  • .load(src) - 加载音频文件(传入音频文件地址的优先级高于参数设置的音频文件地址)
  • .play() - 播放音频文件
  • .pause() - 暂停播放

使用示例

首先需要引入播放组件,默认会产生一个全局变量 - WebAudio(如果通过require.js或者其它模块管理工具的话,可以不生成全局变量)

    
    //自动播放和循环播放的模式
    var bgAudio = new WebAudio({
        src: "http://www.w3school.com.cn/i/horse.mp3",
        autoPlay: true,
        loop: true,
        duration: 1.5
    });
        
    //按需播放模式
    var music = new WebAudio({
        src: "http://www.w3school.com.cn/i/horse.mp3",
        onUpdatetime: function(e){
            console.log(music.audio.currentTime);
        }
    });
    //do something ...
    coin.addEventListener("touchstart", function(){
        music.load();
        music.play();
    }, false);
    

说明

iOS出于某些原因(用户体验?流量?)取消了音频自动播放的功能,需要用户的交互才能加载和播放音频。

解决思路就是在document元素上绑定touchstart事件,然后再加载和播放音频(如果页面中有播放开关,需要注意播放开关的显隐策略)。

如果要在iOS下循环播放就要在播放结束前(通过updatetime事件比对currentTimeduration来判断),将currentTime调整为0即可实现循环播放。

audio.js's People

Contributors

tjuking avatar

Stargazers

 avatar 夏天的风 avatar Jose Arturo Garcia avatar  avatar Lewis avatar  avatar 风云0312 avatar zhangolve avatar zbing avatar Pauli avatar Juwan avatar  avatar aboutboy avatar  avatar  Chris avatar  avatar  avatar vincentYang avatar Momoca avatar

Watchers

James Cloos avatar  avatar

audio.js's Issues

关于自动播放

博主 你好,我有注意到你在document上绑定了 touch事件 我这边是在用户不触摸屏幕的情况下自动播放音乐 尝试了模拟触摸但是没有成功,求指点一二。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.