if ( this.status == "playing" && this.canRecordFlag && (parseInt(stopTime) == parseInt(musicTime * 0.98) || this.timeSection.includes() ) { this.canRecordFlag = false; MediaPlayFn.articleRecord(this, this.video_player.video);
时间: 2023-03-29 15:01:18 浏览: 61
) { console.log("正在播放"); } else { console.log("未播放"); }
回答:这是一段 JavaScript 代码,判断当前状态是否为“playing”,如果是则输出“正在播放”,否则输出“未播放”。
相关问题
function webAudio(options){ //public this.volume = parseInt(options.volume || 100) ; // 音量控制单元,值为Number类型,范围为0-100 this.url = options.url || '' ; // 音频资源url,值类型为String类型 this.autoPlay = !!options.autoPlay; // 是否加载完成自动播放,值类型为Boolean类型 this.loopPlay = !!options.loopPlay; // 是否循环播放,值类型为Boolean类型 //private this.buffer = null; this.context = null; this.sourceAudio = null; this.gainNode = null; this.loadReady = false; //初始化 this.init = function () { window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext; if(!AudioContext){ console.error("您的浏览器不支持HTML5 audio API"); return } this.context = new AudioContext(); this.loadResource(); } //下载音频资源 this.loadResource = function () { var _this = this; var xhr = new XMLHttpRequest(); xhr.open('GET',this.url,true); xhr.responseType = 'arraybuffer'; xhr.onload = function () { _this.context.decodeAudioData(xhr.response,function (buffer) { _this.buffer = buffer; _this.prepareAudio(); this.loadReady = true; }) } xhr.send(); } //是否自动播放 this.prepareAudio = function () { this.autoPlay ? this.startAudio() : ''; } //创建音频 this.createAudio = function () { this.sourceAudio = this.context.createBufferSource();//创建一个音频源 相当于是装音频的容器 this.sourceAudio.buffer = this.buffer;// 告诉音频源 播放哪一段音频 this.gainNode = this.context.createGain();//调节音量 this.sourceAudio.connect(this.gainNode); this.changeVolume();//声音 this.sourceAudio.loop = this.loopPlay;//循环 this.gainNode.connect(this.context.destination);// 连接到输出源 } //重新播放 this.startAudio = function () { this.createAudio(); this.sourceAudio.start(0);//开始播放 } //改变音量 this.changeVolume = function (num) { num = num || 0; this.gainNode.gain.value = (this.volume += num) / 100; } //播放转为暂停 this.pauseAudio = function () { this.gainNode.disconnect(this.context.destination) } //暂停转为播放 this.playAudio = function () { this.gainNode.connect(this.context.destination) } //停止播放 this.stopAudio = function () { this.sourceAudio.stop() } //减小声音 this.decVolume = function () { if(this.volume >= 10){ this.changeVolume(-10); } } //增大声音 this.ascVolume = function () { if(this.volume <= 90){ this.changeVolume(10); } } //静音 this.quietVolume = function () { this.gainNode.gain.value = 0; } //静音恢复 this.recoverVolume = function () { this.changeVolume() } //当前音量 this.getVolume = function () { return (this.gainNode.gain.value).toFixed(2) * 100; } this.init(); return this; } window.test = new webAudio({ volume:100, url:'1.mp3', autoPlay:true, loopPlay:true }); //控制台事件 var pauseEle = document.getElementById("pause"); pauseEle.onclick = function() { if (pauseEle.alt === 'Pause') { test.pauseAudio(); } else { test.playAudio(); } }增加播放暂停图片切换
可以将HTML代码中的播放暂停按钮的img标签的src属性设为一个变量,然后在JavaScript中根据当前状态切换该变量的值,从而实现图片切换。例如,可以在JavaScript中添加一个togglePlayPause函数:
```
function togglePlayPause() {
var playPauseButton = document.getElementById("play-pause-button");
if (playPauseButton.src === "play.png") {
playPauseButton.src = "pause.png";
} else {
playPauseButton.src = "play.png";
}
}
```
并在createAudio函数中调用该函数:
```
this.createAudio = function () {
this.sourceAudio = this.context.createBufferSource();
this.sourceAudio.buffer = this.buffer;
this.gainNode = this.context.createGain();
this.sourceAudio.connect(this.gainNode);
this.changeVolume();
this.sourceAudio.loop = this.loopPlay;
this.gainNode.connect(this.context.destination);
togglePlayPause();
}
```
这样,每次播放或暂停音频时,都会切换播放暂停按钮的图片。需要在HTML代码中给该按钮添加一个id属性为"play-pause-button",并将src属性设为默认的播放按钮图片路径。
$(document).on("keydown", (e) => { if (e.ctrlKey) { switch (e.keyCode){ case 38: wrapper.children().each(function () { $(this).css("background-color", "rgb(" + getRandomChanel() + ", " + getRandomChanel() + ", " + getRandomChanel() + ")") }); break; case 82: case 71: case 66: wrapper.children().each(function () { let colors = ($(this).css("background-color")).match(/[0-9]{1,3}/g); let r = parseInt(colors[0]); let g = parseInt(colors[1]); let b = parseInt(colors[2]); if(e.keyCode === 82 && r > g && r > b){ $(this).remove(); }else if(e.keyCode === 71 && g > r && g > b){ $(this).remove(); }else if(e.keyCode === 66 && b > r && b > g){ $(this).remove(); } }); break; } console.log(e); return false; } });换种方式编写
可以使用普通的键盘事件监听器来替代jQuery的on()方法,也可以使用switch语句中的if-else语句来替代switch语句。另外,可以使用arrow function来替代普通的函数声明。下面是一种可能的替代方式:
```javascript
document.addEventListener("keydown", (e) => {
if (e.ctrlKey) {
if (e.keyCode === 38) {
wrapper.children().each(function () {
$(this).css("background-color", "rgb(" + getRandomChanel() + ", " + getRandomChanel() + ", " + getRandomChanel() + ")");
});
e.preventDefault();
} else if (e.keyCode === 82 || e.keyCode === 71 || e.keyCode === 66) {
wrapper.children().each(function () {
let colors = ($(this).css("background-color")).match(/[0-9]{1,3}/g);
let r = parseInt(colors[0]);
let g = parseInt(colors[1]);
let b = parseInt(colors[2]);
if ((e.keyCode === 82 && r > g && r > b) ||
(e.keyCode === 71 && g > r && g > b) ||
(e.keyCode === 66 && b > r && b > g)) {
$(this).remove();
}
});
e.preventDefault();
}
}
});
```
这个替代方式中,我们使用了addEventListener()方法来替代jQuery的on()方法。我们还使用了if-else语句来替代switch语句,这样能够更加清晰地表达代码逻辑。同时,我们使用了e.preventDefault()来阻止默认行为,这样可以避免在处理键盘事件时出现问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)