zoukankan      html  css  js  c++  java
  • js 播放音频文件 兼容火狐 谷歌浏览器

    var url='/js/634.ogg';
    window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext ||
    window.msAudioContext;
    try {
    var context = new window.AudioContext();;
    var source = null;
    var audioBuffer = null;
    function playSound() {
    source = context.createBufferSource();
    source.buffer = audioBuffer;
    source.loop = false;
    source.connect(context.destination);
    source.start(0); //立即播放
    }

    function initSound(arrayBuffer) {
    context.decodeAudioData(arrayBuffer, function (buffer) { //解码成功时的回调函数
    audioBuffer = buffer;
    playSound();
    }, function (e) { //解码出错时的回调函数
    console.log('Error decoding file', e);
    });
    }

    function loadAudioFile(url) {
    var xhr = new XMLHttpRequest(); //通过XHR下载音频文件
    xhr.open('GET', url, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function (e) { //下载完成
    initSound(this.response);
    };
    xhr.send();
    }
    loadAudioFile(url);
    } catch (e) {
    console.log('!Your browser does not support AudioContext');
    }

  • 相关阅读:
    NET CORE 数据库迁移
    VUE3.0 解析svg文件
    关于ElementUI的样式不生效
    git命令
    vue 2.x的跨域问题
    Putty 重新启动 linux sqlserver服务
    aspnetcore之session
    Syncfusion 在 core 的架构
    TortoiseSVN创建/合并分支
    正则表达式知识点整理
  • 原文地址:https://www.cnblogs.com/limonyun/p/12893212.html
Copyright © 2011-2022 走看看