js实现浏览器中多个mp3声音可调节音量混音输出示例代码

代码语言:html

所属分类:多媒体

代码描述:js实现浏览器中多个mp3声音可调节音量混音输出示例代码

代码标签: js 浏览器 多个 mp3 声音 调节 音量 混音 输出 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多MP3混音示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        h1 {
            text-align: center;
        }
        .audio-control {
            margin-bottom: 20px;
        }
        label {
            display: inline-block;
            width: 100px;
        }
        input[type="range"] {
            width: 200px;
        }
    </style>
</head>
<body>
    <h1>多MP3混音示例</h1>
    <div class="audio-control">
        <label for="audio1">音频1音量:</label>
        <input type="range" id="audio1" min="0" max="1" step="0.1" value="0.5">
        <span id="audio1Value">0.5</span>
    </div>
    <div class="audio-control">
        <label for="audio2">音频2音量:</label>
        <input type="range" id="audio2" min="0" max="1" step="0.1" value="0.5">
        <span id="audio2Value">0.5</span>
    </div>
    <div class="audio-control">
        <label for="audio3">音频3音量:</label>
        <input type="range" id="audio3" min="0" max="1" step="0.1" value="0.5">
        <span id="audio3Value">0.5</span>
    </div>
    <button id="playBtn">播放</button>
    <button id="stopBtn">停止</button>

    <script>
        // 创建音频上下文
        const audioContext = new (window.AudioContext || window.webkitAudioContext)();
        
        // 音频文件URL(请替换为实际的MP3文件URL)
        const audioUrls = [
            '//repo.bfw.wiki/bfwrepo/sound/5c89fd22dea6948307.mp3',
            '//repo.bfw.wiki/bfwrepo/sound/5d7cb013d5eab.mp3',
            '//repo.bfw.wiki/bfwrepo/sound/5e16a04d75540.mp3'
        ];
        
        // 存储音频源和增益节点
        const audioSources = [];
        const gainNodes = [];
        
        // 加载音频文件
        function loadAudio(url) {
            return fetch(url)
                .then(response => response.arrayBuffer())
                .then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer));
        }
        
        // 创建音频源和增益节点
        function createAudioSource(audioBuffer) {
            const source = audioContext.createBufferSource();
            source.buffer = audioBuffer;
            
            const gainNode = audioContext.createGain();
       .........完整代码请登录后点击上方下载按钮下载查看

网友评论0