美文网首页
丸子学HTML(学习半小时 - H5媒体)

丸子学HTML(学习半小时 - H5媒体)

作者: 丸子小姐__不懂爱 | 来源:发表于2023-11-10 09:30 被阅读0次

音频播放

使用audio标签

一些属性:

  • autoplay 音频在就绪后马上播放
  • controls 向用户显示控件,比如播放按钮
  • loop 每当音频结束时重新开始播放
  • preload 音频在页面加载时进行加载,并预备播放
  • src 要播放的音频的URL

随便练练

       function MiniPlayer() {
            // 音乐列表
            this.songs = [];
            // 音乐名称
            this.musicName = '';
            // 记录当前播放索引
            this.index = 0;
            // 当前播放音乐的时长
            this.duration = 0;
            // 当前播放的位置
            this.currentTime = 0;
            // 播放状态
            this.playStatus = false;
            // 音轨长度
            this.trackLen = 0;
            // 音轨定时器
            this.trackTimer = null;
            // 获取元素
            this.$ = function(className){
                return document.querySelector(className)
            }
        }

        MiniPlayer.prototype = {
            // 初始化
            init: function() {
                this.playerDom = document.createElement('audio');
                this.changeBtnStatus()
                this.prev();
                this.next();
                this.drawTrack();
            },
            // 播放音乐
            play: function() {
                let that = this;
                that.duration = that.playerDom.duration;
                that.playerDom.play();
                that.playProcess();
                
                this.$('.music-name').innerText = this.songs[this.index].replace('./audio/','');
                this.$('.poster-pic').style.animation = `rotateMe 4s linear infinite`
                this.trackTimer = setInterval(function() {
                    for(let i=0; i< that.trackLen; i++){
                        document.getElementsByClassName('items')[i].style.height = that.randomNum(100) + 'px'
                    }
                }, 280)
            },
            // 暂停音乐
            pause: function() {
                clearInterval(this.trackTimer)
                this.playerDom.pause();
            },
            // 播放进度条控制
            playProcess: function() {
                let that = this;
                this.playerDom.oncanplay = function() {
                    that.duration = that.playerDom.duration;
                }
                let timer = setInterval(function() {
                    let dis = that.playerDom.currentTime/that.duration*that.$('.process-bar').clientWidth;
                    that.$('.process').style.width = dis + 'px';
                    // 判断音乐是否播放结束
                    if(that.playerDom.currentTime === that.duration){
                        timer = clearInterval(timer);
                        // 重置进度条
                        that.$('.process').style.width = 0;
                        // 自动跳转至下一曲
                        that.index++;
                        if(that.index > that.songs.length-1){
                            that.index = 0;
                        }
                        that.playerDom.src = that.songs[that.index];
                        that.play()
                    }
                })
            },
            // 绘制音轨
            drawTrack: function() {
                let that = this;
                this.trackLen = Math.floor(this.$('.track-wrapper').clientWidth / this.$('.items').clientWidth)
                let tracks = '';
                for(let i =0; i< this.trackLen; i++){
                    let left = this.$('.items').clientWidth * i + 'px';
                    tracks += `<div class="items" style="left:${left}"></div>`;
                }
                this.$('.track-wrapper').innerHTML = tracks;
               
            },
            // 随机数
            randomNum: function(num){
                return Math.floor(Math.random()*(num+1))
            },
            // 上一曲
            prev: function () {
                let that = this;
                this.$('.prev').onclick = function() {
                    // 重置进度条
                    that.$('.process').style.width = 0;
                    clearInterval(that.trackTimer);
                    --that.index;
                    if(that.index < 0){
                        that.index = that.songs.length -1;
                    }
                    that.playerDom.src = that.songs[that.index];
                    that.play()
                }
            },
            // 下一曲
            next: function () {
                let that = this;
                this.$('.next').onclick = function () {
                    clearInterval(that.trackTimer);
                    // 重置进度条
                    that.$('.process').style.width = 0;
                    ++that.index;
                    if(that.index > that.songs.length-1) {
                        that.index = 0;
                    }
                    that.playerDom.src = that.songs[that.index];
                    that.play()
                }
            },
            // 改变音轨
            changePlaySrc: function() {
                this.playerDom.src = this.songs[this.index];
            },
            // 添加音乐
            addMusic: function(src) {
                this.songs.push(...src);
                this.playerDom.src = this.songs[this.index];
            },
            // 播放暂停按钮切换
            changeBtnStatus: function() {
                let that = this;
                this.$('.play').onclick = function() {
                    if(this.playStatus) {
                        // 暂停
                        this.classList.replace('icon-24gf-pauseCircle', 'icon-24gf-playCircle')
                        that.pause();
                        this.playStatus = false;
                    }else {
                        // 播放
                        this.classList.replace('icon-24gf-playCircle', 'icon-24gf-pauseCircle')
                        that.play(0);
                        this.playStatus = true;
                    }
                }
            }
        }

        let miniPlayer = new MiniPlayer()
        miniPlayer.init()
        miniPlayer.addMusic(['./audio/G.E.M.邓紫棋 - 泡沫.mp3','./audio/大壮 - 伪装 (DJ小鱼儿版).mp3','./audio/白智英 - 총 맞은 것처럼 (Live).mp3'])

视频播放

使用video标签

一些属性:

  • autoplay 视频在就绪后马上播放
  • controls 向用户显示控件,比如播放按钮
  • height 设置视频播放器的高度
  • width 设置视频播放器的宽度
  • loop 当媒介文件王城播放后再次开始播放
  • preload 视频在页面加载时进行加载,并预备播放
  • src 要播放的视频的URL

多媒体播放

● 常用方法:load() 加载、 play() 播放、 pause() 暂停
● 常用属性:
a) currentTime 视频播放的当前进度
b) duration:视频的总时间 100000/60
c) paused:视频播放的状态
● 常用事件:
a) oncanplay: 事件在用户可以开始播放视频/音频(audio/video)时触发。
b) ontimeupdate:通过该事件来报告当前的播放进度.
c) onended:播放完时触发—重置

相关文章

  • ## HTML基础-多媒体标签

    ## HTML基础-多媒体标签 # video标签 # audio标签 # marquee标签(跑马灯) H5新增...

  • <h1>H5学习记录~H5总结文档</h1&g

    H5学习记录~H5总结文档 ------第一部分Html标签

  • H5视频与音频

    声明: 学习资源来源于 HTML 5 视频,本人只是学习一下. H5是HTML、XHTML以及HTML DOM的新...

  • HTML5中新增的标签

    H5中新增了30个标签 在H5之前,HTML只能处理 文本数据和图片数据,到了H5之后,还可以处理 多媒体数据 v...

  • vue 本地https测试userMedia

    接h5渠道的时候遇到一个问题,正好测试一下两种h5调起设备的方案:html媒体捕获、userMedia。 准备: ...

  • 快速Web学习

    参考学习:w3school.com.cn/html5/html_5_intro.asp H5分为:html(内容和...

  • 初学HTML5

    关于H5 要学习HTML5,首先要知道三个东西 HTML、CSS、JavaScript。如果把H5比作一个人,那么...

  • HTML多媒体

    今天学习了HTML传统的多媒体标签 和 标签。HTML多媒体主要包含视频、音频、动画等,在HTML中要插入这些...

  • HTML5

    H5和HTML5有什么关系 首先H5 != HTML5, H5 是一个产品名词 , HTML5 是一个技术名词。 ...

  • 2018-10-30html和css基础

    1.html html版本狭义的H5:HTML的第五个大版本广义的H5:css3+html5+js标签: 表单标签...

网友评论

      本文标题:丸子学HTML(学习半小时 - H5媒体)

      本文链接:https://www.haomeiwen.com/subject/uwchwdtx.html