js-php-audioAPI利用AJAX处理后端传来的mp3流文件数据

59 篇文章 0 订阅

js-php-audioAPI利用AJAX处理后端传来的mp3流文件数据 目录


前言

  • 尝试利用浏览器中的audio API

推荐阅读

常见API

  • AudioContext
  • decodeAudioData

代码实现

audio.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>audio api</title>
</head>
<body>
<!--<audio controls="controls" autoplay="autoplay">-->
<!--    <source src="test.mp3" type="audio/mpeg">-->
<!--</audio>-->

<!--<input id="file" type="file">-->
</body>
<script>
init();
// getFile();
function init() {
        // AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext;
        let aCtx;
        try{
            aCtx = new (window.AudioContext || window.webkitAudioContext)();
        }catch (e) {
            console.log(e);
        }
        play(aCtx, 'http://localhost:83/streamFile.php');

    }

//    file
function getFile() {
        const fileDom = document.getElementById('file');
        fileDom.addEventListener('change', event=>{
            const file = event.target.files[0];
            console.log(file);
            const fileReader = new FileReader();
            const audioContext = new (window.AudioContext || window.webkitAudioContext)();

            fileReader.readAsArrayBuffer(file);

            fileReader.onload = ()=>{

                console.log(audioContext);
                audioContext.decodeAudioData(fileReader.result, result=>{
                    console.log('start');
                    console.log(result);
                    const source = audioContext.createBufferSource();
                    source.buffer = result;
                    source.connect(audioContext.destination);
                    source.start();
                },
                 err =>{
                    console.log(`decodeAudioData error:${err}`);
                });
            };
        });
}

//    加载音乐
    function load(ctx, fUrl, succFN, failFN) {
        let req = new XMLHttpRequest();
        req.open('GET', fUrl, true);
        req.responseType = 'arraybuffer';
        req.onprogress = data => {
            console.log(`data:${data}`);
            console.log(`response:${req.response.byteLength}`);
        };
        //旧版的回调函数
        //异步解码
        req.onload = function () {
            console.log(this.response);
        //    解码
            ctx.decodeAudioData(
                this.response,
                buf =>{
                    succFN && succFN(buf);
                },
                (err) => {
                    failFN && failFN(err);
                }
            );
        }
        //新版的promise
        // req.onload = function () {
        //     ctx.decodeAudioData(this.response)
        //         .then();
        // }
        req.send();
    }

//播放音频
    function play(ctx, fUrl) {
        load(ctx, fUrl, buf => {
            //测试是否进入
            console.log('Enter decode...');
            //创建一个node对象
            let sNode = ctx.createBufferSource();
            // console.log(buf);
            sNode.buffer = buf;
            //连接到destination节点进行播放
            sNode.connect(ctx.destination);
            sNode.start(0);
            sNode.loop = true;
        }, err => {
            console.log(`Error with decoding ${err}`);
        });
    }
</script>
</html>

streamFile.php

<?php


namespace streamFile;
spl_autoload_register(function (string $className){
    require_once $className . ".php";
});

class streamFile
{
    private $file;
    /*
    * @param String $file 要发送的文件
    * */
    public function __construct(string $file)
    {
        $this->file = $file;
    }

    public function sendStreamFile() {
        if (!file_exists($this->file))   return false;

        $opt = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: audio/mpeg'
            )
        );

//        header('content-Transfer-Encoding: binary');
//        header('Progma: no-cache');
//        header('icy-br: 128');

        $context = stream_context_create($opt);
        $response = file_get_contents($this->file, false, $context);

        return $response;
    }


    public function openFile(){
        $fp = fopen($this->file, "r");
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Content-Disposition: attachment; filename=test.mp3");
        $buffer = 1024;
        while (!feof($fp)){
            $file_con = fread($fp, $buffer);
            echo $file_con;
        }

        fclose($fp);
    }
}


$ret = new streamFile('test.mp3');
var_dump($ret->openFile());

//echo 'start';

总结

  • 如果出现Uncaught (in promise) DOMException: Unable to decode audio data这种报错,毫无疑问,是后端传来的二进制数据存在问题,需要后端进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值