首页 > 其他分享 >mux.js的使用

mux.js的使用

时间:2023-02-28 12:11:45浏览次数:31  
标签:buffer 使用 mux js player window ._ new segment

//<script src="https://cdn.bootcdn.net/ajax/libs/mux.js/6.2.0/mux-mp4.min.js"></script>
const AUDIO_MIME = 'audio/mp4; codecs="mp4a.40.2"';
class LivePlayer {
    constructor() {
        this._player = new Audio();
        this._player.autoplay = true;

        this._isPaused = false;
        this._isFirst = true;
        this._url = '';
        this._uri = '';

        this._tmr = null;
    }

    play(url) {
        if (url) {
            this._url = url;
        }
        if (!this._url) return;

        this._uri = '';
        this._isFirst = true;
        this._isPaused = false;

        this._mediaSource?.endOfStream();
        this._transmuxer?.dispose();

        this._mediaSource = new window.MediaSource();
        this._player.src = window.URL.createObjectURL(this._mediaSource);
        this._mediaSource.onsourceopen = () => {
            window.URL.revokeObjectURL(this._player.src);
            this._mediaSource.addSourceBuffer(AUDIO_MIME);
            this._reqRealUrl();
        };

        this._transmuxer = new muxjs.Transmuxer();
        this._transmuxer.on('data', segment => {
            let buffer;
            if (this._isFirst) {
                this._isFirst = false;
                buffer = new Uint8Array(segment.initSegment.byteLength + segment.data.byteLength);
                buffer.set(segment.initSegment, 0);
                buffer.set(segment.data, segment.initSegment.byteLength);
            } else {
                buffer = new Uint8Array(segment.data);
            }
            this._mediaSource.sourceBuffers[0].appendBuffer(buffer);
        });
    }

    pause() {
        this._isPaused = true;
        this._player.pause();
    }

    d(...args) {
        window.console.log(new Date().toLocaleTimeString(), ...args);
    }

    _reqStream() {
        this.d('reqStream', this._uri);
        window.fetch(this._uri)
            .then(res => res.arrayBuffer())
            .then(buffer => {
                this._transmuxer?.push(new Uint8Array(buffer));
                this._transmuxer?.flush();
            });
    }

    _reqRealUrl() {
        if (!this._isPaused) {
            window.fetch(this._url)
                .then(res => res.text())
                .then(m3u8 => {
                    const latest = m3u8.trim().split(/\r\n|\n/).filter(it => it !== '').at(-1);
                    if (this._uri !== latest) {
                        this._uri = latest;
                        this._reqStream();
                    }
                });
        }
        window.clearTimeout(this._tmr);
        this._tmr = window.setTimeout(() => this._reqRealUrl(), 2E3);
    }
}

(function () {
    const player = window.player = new LivePlayer();
    window.expose = new class {
        get volume() { return player._player.volume; }
        set volume(vol) { player._player.volume = vol; }

        get muted() { return player._player.muted; }
        set muted(muted) { player._player.muted = muted; }

        get playingId() { return this._playingId; }

        get paused() { return player._isPaused; }

        constructor() {
            this._playingId = '';
        }

        play(channelId) {
            if (channelId) {
                this._playingId = channelId;
                player.play(`http://live.ximalaya.com/radio-first-page-app/live/${channelId}/64.m3u8?transcode=ts`);
            } else {
                this._playingId && player.play();
            }
        }

        pause() {
            player.pause();
        }
    }
})();

标签:buffer,使用,mux,js,player,window,._,new,segment
From: https://www.cnblogs.com/zh33gl/p/17163567.html

相关文章

  • 使用默认pypi源出现连接超时
    背景信息安装dataworkssdk时报错,原因是连接默认的pypi仓库超时pipinstallaliyun-python-sdk-dataworks-public==4.2.1报错信息Retrying(Retry(total=4,conne......
  • js函数式编程讲解
    什么是函数式编程是一种编程范型,它将电脑运算视为数学上的函数计算,并且避免使用程序状态以及易变对象。函数式编程更加强调程序执行的结果而非执行的过程,倡导利用若干简......
  • js作用域、作用域链和它的一些优化
    前言作用域和作用域链是所有JavaScript开发人员每天都要接触和应用的内容。不管是面试中的作用域链的面试考察,还是日常代码研发中变量与作用域链的构建,它的身影几乎无处不......
  • ChoimoeBot 插件使用指南
    目录插件管理器语句抽象化使用处理好友添加和群邀请定时任务ChatGPT配置使用娶群友配置使用自动回复插件词库配置回复配置日麻小工具手牌分析头像表情包使用图片搜索配置使......
  • Nodejs:ESModule和commonjs,傻傻分不清
    最近写nodejs脚本的时候遇到了commonjs和ESModule的问题,正好之前用得稀里糊涂的,这次好好学习一下。ESModule导出仅导出namedexports:命名导出,每次可以导出一个或......
  • 使用Idea快速开发JSP
    使用Idea快速开发JSP1,在使用idea使用开发Web项目,要配置tomcat 2,在Idea中创建的Web项目:浏览器可以直接访问WebContent中的文件例如http://localhost:8888/MyJs......
  • ZoomIt使用指南(转)
    ZoomIt,这个工具是sysinternals的一个工具,sysinternals里面工具很多,很多都是你不知道干什么用的。所以有个伟大想法,对这个工具集进行一个全面的说明。这个工具是用来演示用的......
  • 关于Android中Spinner的使用
    这是一个关于使用Android中Spinner控件的方法,一个简单的代码及测试效果如下:1、主程序packageext.owen.testSpinner;importandroid.app.Activity;importandroid.os.Bund......
  • 通过使用online表单的获取使用,了解vue.js数组的常用操作
        在开发项目中,经常会遇到对数组的操作,比如对数组的数据进行删减或增加,同时也会对每个数组里的数据进行删减,下面就举个例子说明一下。   直接给一段代码getO......
  • Android高手进阶教程(十五)之---通过Location获取Address的使用!
    大家好,上一节我讲了一下如何通过LocationManager来获取Location,没有看过上一节的同学,可以点击如下链接返回查看:​​Android高手进阶教程十四之---AndroidLocation的使用!​......