react项目 循环播放背景音乐

编程入门 行业动态 更新时间:2024-10-17 07:21:13

react项目 循环播放<a href=https://www.elefans.com/category/jswz/34/1761360.html style=背景音乐"/>

react项目 循环播放背景音乐

代码简写

let audioArr;
let audioIndex;
export default class IndexLayout extends React.PureComponent {state = {audioUrl: '',};componentDidMount() {this.getAudio();}
// 切换音乐ChangeMusic = (myAudio) => {if(audioArr.length-1<=audioIndex){audioIndex = 0;myAudio.src = audioArr[0].file_url;myAudio.play();}else{audioIndex = ++audioIndex;myAudio.src = audioArr[audioIndex].file_url;myAudio.play()}}// 获取音频getAudio=()=>{this.props.dispatch({type: 'front/getAudio',payload: {}}).then((data)=>{if(data && data.length > 0){audioArr = data;audioIndex = 0;// 音乐播放const myAudio = new Audio()myAudio.preload = true; //// myAudio.controls = true;myAudio.loop = false;myAudio.src = data[0].file_url;// 播完时候播放下一首myAudio.addEventListener('ended', this.ChangeMusic.bind(this, myAudio), false);myAudio.play();document.getElementById("audioBox").appendChild(myAudio);}})};render() {return (<div id='audioBox' />);}
}

更多推荐

react项目 循环播放背景音乐

本文发布于:2024-03-07 01:59:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1716546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:背景音乐   项目   react

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!