在web端播放音频文件

来源:互联网 发布:filmimpact插件 mac版 编辑:程序博客网 时间:2024/06/04 01:33

1. 下载插件js包(该包放在我的资源里面amrjs.js),嵌入到程序中

2. 在html页面使用js

 <img src="__PUBLIC__/assets/images/playerpro.png"  id="imgs{$vo.log_id}" width="25px" height="25px" onclick='amrplay("__ROOT__{$vo['f_path']}",this,{$vo['log_id']});'  style="cursor: pointer;" />    


 <script>
    
    function E(selector) {
        return document.querySelector(selector);
    }
    function amrplay(url,_self,log_id){


    var id="imgs"+log_id;
    // document.getElementById(id).src="__PUBLIC__/assets/images/052.png";
    alert("开始播放语音");
    fetchBlob(url, function(blob) {
            playAmrBlob(blob);
        });
    //document.getElementById(id).src="__PUBLIC__/assets/images/playerpro.png";       
   
    //_self.src="__PUBLIC__/assets/images/playerpro.png";
    }
    
    
    var gAudioContext = new AudioContext();
    function getAudioContext() {
        if (!gAudioContext) {
            gAudioContext = new AudioContext();
        }
        return gAudioContext;
    }


    function fetchBlob(url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.responseType = 'blob';
        xhr.onload = function() {
            callback(this.response);
        };
        xhr.onerror = function() {
            alert('Failed to fetch ' + url);
        };
        xhr.send();
    }


    function readBlob(blob, callback) {
        var reader = new FileReader();
        reader.onload = function(e) {
            var data = new Uint8Array(e.target.result);
            callback(data);
        };
        reader.readAsArrayBuffer(blob);
    }


    function fetchAndReadBlob(url, callback) {
        fetchBlob(url, function(blob) {
            readBlob(blob, callback);
        });
    }


    function playAmrBlob(blob, callback) {
        readBlob(blob, function(data) {
            playAmrArray(data);
        });
    }


    function convertAudioBlobToAmr(blob) {
        readBlob(blob, function(data) {
            var ctx = getAudioContext();
            ctx.decodeAudioData(data.buffer, function(audioBuffer) {
                var pcm;
                if (audioBuffer.copyFromChannel) {
                    pcm = new Float32Array(audioBuffer.length);
                    audioBuffer.copyFromChannel(pcm, 0, 0);
                } else {
                    pcm = audioBuffer.getChannelData(0);
                }
                var amr = AMR.encode(pcm, audioBuffer.sampleRate, 7);
                playAmrArray(amr);
            });
        });
    }


    function playAmrArray(array) {
        var samples = AMR.decode(array);
        if (!samples) {
            alert('Failed to decode!');
            return;
        }
        playPcm(samples);
    }


    function playPcm(samples) {
        var ctx = getAudioContext();
        var src = ctx.createBufferSource();
        var buffer = ctx.createBuffer(1, samples.length, 8000);
        if (buffer.copyToChannel) {
            buffer.copyToChannel(samples, 0, 0)
        } else {
            var channelBuffer = buffer.getChannelData(0);
            channelBuffer.set(samples);
        }


        src.buffer = buffer;
        src.connect(ctx.destination);
        
        src.start();
    }


    function convertAmrBlobToWav(blob) {
        readBlob(blob, function(data) {
            var buffer = AMR.toWAV(data);
            var url = URL.createObjectURL(new Blob([buffer], { type: 'audio/x-wav' }));
            // Play wav buffer
            var audio = new Audio(url);
            audio.onloadedmetadata = audio.onerror = function() {
                URL.revokeObjectURL(url);
            };
            audio.play();
        });
    }


    </script>