Comments (2)
I could not recreate the problem.
Can you get play
in the console of http://aplayer.js.org/
from aplayer.
There is a logical problem in my code, which will result in events listeners remove from elements not being displayed, I believe that caused the problem.
Update:
No, my unbinding the events didn't break the events, the problem is canplay
being triggered after play
, how weird.
Let me illustrate my problem a little:
I'm trying to create a floating music player on page.
When the music is not being played for a while, I would like it to disappear silently.
My first attempt is to check its status periodically by using playing
event and make it disappear if this event not being triggered for a while but it caused performance problem.
Then I turn to play
pause
ended
and error
.
I make it visible upon play
and disappear upon others.
The problem is play
will not be triggered even when autoplay
set to true
thanks to mobile browser limitations.
They I decide to setTimeout(disappear(), 4000)
when canplay
then cancel the timeout if play
triggered.
The experiment confirmed canplay
will be triggered after play
so I have to turn to mobile browser detecting now.
Another Update:
I achieved it by simulating mutex behavior in my code. Whoever try to make APlayer a floating item as I do should try to do it this way.
I'm pasting my code here for reference:
var audioPlayerDisappearTimeOut = null;
var audioPlayer = null;
function activeAudioPlayer() {
$('.audio').on("click", function() {
if (audioPlayer !== null) {
audioPlayer.pause();
}
var option = {
element: document.getElementById("floatingAudioPlayer"), // Optional, player element
narrow: true, // Optional, narrow style
autoplay: true, // Optional, autoplay song(s), not supported by mobile browsers
showlrc: 0, // Optional, show lrc, can be 0, 1, 2, see: ###With lrc
mutex: true, // Optional, pause other players when this player playing
theme: '#c44dff', // Optional, theme color, default: #b7daff #e6d0b2
loop: false, // Optional, loop play music, default: true
music: [{ // Required, music info, see: ###With playlist
title: "", // Required, music title $(this).data("title")
author: "", // Required, music author
url: $(this).data("href") // Required, music url
}]
}
$('#floatingAudioPlayer').animate({
height: "66px"
}, 600);
audioPlayer = new APlayer(option);
/* Change the order of following lines will cause events failed to be triggered. */
hide();
audioPlayer.on("ended", function() {
hide();
});
audioPlayer.on("pause", function() {
hide();
});
audioPlayer.on("play", function() {
show();
});
audioPlayer.on("error", function() {
hide();
});
audioPlayer.init();
});
function show() {
if (audioPlayerDisappearTimeOut !== null) {
clearTimeout(audioPlayerDisappearTimeOut);
audioPlayerDisappearTimeOut = null;
} else {
$('#floatingAudioPlayer').animate({
height: "66px"
}, 600);
}
}
function hide() {
if (audioPlayerDisappearTimeOut === null) {
console.log("will hide");
audioPlayerDisappearTimeOut = setTimeout(function() {
if (audioPlayerDisappearTimeOut !== null) {
$('#floatingAudioPlayer').animate({
height: 0
}, 600);
audioPlayerDisappearTimeOut = null;
}
}, 4000);
}
}
};
These code will ensure following behavior:
On desktop:
- When link to mp3 clicked the floating player will show itself and start playing.
- Clicking on another link will pause current one.
- If paused, ended or error, floating player will disappear after 4 seconds.
On mobile: - When link to mp3 clicked the floating player will show itself and wait user to click on play, otherwise disappear after 4 seconds.
- Clicking on another link will pause current one.
- If paused, ended or error, floating player will disappear after 4 seconds.
Floating player will not disappear if music is playing.
from aplayer.
Related Issues (20)
- Suggestions related to autoplay 与自动播放相关的建议 HOT 2
- How do I put aplayer with fixed mode enabled in the lower-right corner of the hexo blog instead of the lower-left corner HOT 1
- bug
- Purchase/Download button
- adaptive bitrate streaming
- how to Unbind the event handler
- Is there an API to get information about the currently playing audio?如何获取当前正在播放的音频的信息? HOT 1
- Aplayer causes the pinia-plugin-persistedstate plugin to fail
- No ap.setMusic() function
- 2个BUG 播放数注销后,定时器仍在跑;播放数参数定义为fixed:1注销后,再次生成fixed:0 仍是吸地模式
- The player cannot adjust the number and size of lines displayed in the lyrics, only one or two lines can be viewed播放器无法调整歌词显示的行数和大小,只能看一两行 HOT 2
- ap.play() function not work
- Increase the number of lines displayed for lyrics
- 无法获取播放列表长度 HOT 1
- The autoplay:true setting has no effect
- dynamic update lyric. 动态更新歌词 HOT 2
- volume can't be adjusted HOT 1
- 建议添加Chrome的MediaSession全局媒体控件样式的支持 HOT 1
- Invalid player mutex attribute
- Is it possible to add a max mode?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aplayer.