vue h5 引入外部js进行 amr 的音频播放(audio标签无法播放amr的解决方案)

在处理微信语音中它的音频是amr格式、但是在普通的audio中是不能播放的
在下面有两种方法 js外链接引入/npm引入

// 如果是vue项目 则npm引入
npm install benz-amr-recorder

let BenzAMRRecorder = require('benz-amr-recorder');  //引入
this.amr = null;
this.amr = new BenzAMRRecorder();    //创建
this.amr.initWithUrl(data.audios[0].url);   //初始化
this.amrTime.duration = data.audios[0].duration / 1000;  //音频总时长
this.amr.play()  //播放
this.amr.stop() //停止
// 如果是h5中的项目 则引入外链接amr.js
let url="xxxxxxxxx.amr"
let gAudioContext = new AudioContext();
function getAudioContext() {
    if (!gAudioContext) {
        gAudioContext = new AudioContext();
    }
    return gAudioContext;
}
//开始播放
window.playAmr = function (url) {
    fetchBlob(amrUrl, function (blob) {
        playAmrBlob(blob);
    });
};
function fetchBlob(url, callback) {
    let 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) {
    const reader = new FileReader();
    reader.onload = function (e) {
        const data = new Uint8Array(e.target.result);
        callback(data);
    };
    reader.readAsArrayBuffer(blob);
}
function playAmrBlob(blob, callback) {
    readBlob(blob, function (data) {
        playAmrArray(data);
    });
}
function playAmrArray(array) {
    const samples = AMR.decode(array);
    if (!samples) {
        alert('播放失败,请下载后播放!');
        return;
    }
    playPcm(samples);
}
let preCtx = null;
function playPcm(samples) {
    if (preCtx !== null) {
        stopPlay();
    }
    const ctx = getAudioContext();
    const src = ctx.createBufferSource();
    const buffer = ctx.createBuffer(1, samples.length, 8000);
    if (buffer.copyToChannel) {
        buffer.copyToChannel(samples, 0, 0);
    } else {
        const channelBuffer = buffer.getChannelData(0);
        channelBuffer.set(samples);
    }
    src.buffer = buffer;
    src.connect(ctx.destination);
    src.start();
    preCtx = src;
}
//停止播放
function stopPlay() {
    if (preCtx !== null) {
        preCtx.stop();
    }
    preCtx = null;
}

//调用playAmr() 就可播放音频
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用 Vue.js 控制 HTML5 音频按顺序播放的示例代码: ```html <template> <div> <audio ref="audio" controls @ended="playNext"> <source v-for="(src, index) in sources" :key="index" :src="src" type="audio/mpeg"> Your browser does not support the audio tag. </audio> </div> </template> <script> export default { data() { return { sources: ['audio1.mp3', 'audio2.mp3', 'audio3.mp3'], index: 0 }; }, methods: { playNext() { this.index++; // 播放索引加 1 if (this.index < this.sources.length) { this.$refs.audio.src = this.sources[this.index]; // 切换到下一个音频文件 this.$refs.audio.play(); // 播放音频 } } }, mounted() { this.$refs.audio.src = this.sources[this.index]; // 设置第一个音频文件的路径 this.$refs.audio.play(); // 播放第一个音频文件 } }; </script> ``` 这个代码示例中,我们使用了 Vue.js 的 `ref` 属性来获取 `audio` 元素的引用,并使用 `v-for` 指令动态生成多个 `source` 元素以设置多个音频文件的路径。在 `data` 中定义了一个 `sources` 数组来存储所有音频文件的路径,以及一个 `index` 变量来记录当前播放的是哪个音频文件。在 `methods` 中定义了一个 `playNext` 方法,用于在监听到音频播放结束事件时切换到下一个音频文件并播放。在 `mounted` 生命周期钩子中,我们设置了第一个音频文件的路径,并调用 `play()` 方法开始播放。 这个示例代码中,只需要将所有音频文件的路径添加到 `sources` 数组中即可,Vue.js 代码会自动按顺序播放这些音频文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值