Add video player

This commit is contained in:
Pierre HUBERT 2021-03-06 11:19:17 +01:00
parent ee14bcb659
commit b63ace2dde
5 changed files with 83 additions and 54 deletions

View File

@ -1,45 +0,0 @@
/**
* Small audio player
*
* @author Pierre Hubert
*/
class SmallAudioPlayer {
/**
* @param {HTMLElement} target
* @param {String} url
*/
constructor(target, url) {
let link = createElem2({
appendTo: target,
type: "span",
class: "a",
innerHTML: "<i class='fa fa-play-circle'></i> Audio file"
});
link.addEventListener("click", e => {
e.preventDefault();
this.showPlayer(url);
})
}
/**
* Show full screen player
* @param {String} url
*/
async showPlayer(url) {
const tpl = await Page.loadHTMLTemplate("components/audioPlayer.html")
let el = document.createElement("div");
el.innerHTML = tpl;
document.body.appendChild(el);
el.querySelector("audio").src = url;
el.querySelector(".close").addEventListener("click", e => el.remove());
el.addEventListener("click", e => el.remove());
}
}

View File

@ -0,0 +1,65 @@
/**
* Small media player
*
* @author Pierre Hubert
*/
class SmallMediaPlayer {
/**
* @param {HTMLElement} target
* @param {String} url
*/
constructor(target, url, isVideo) {
let link = createElem2({
appendTo: target,
type: "span",
class: "a",
innerHTML: "<i class='fa fa-play-circle'></i> "+(isVideo ? "Video" : "Audio")+" file"
});
link.addEventListener("click", e => {
e.preventDefault();
this.showPlayer(url, isVideo);
})
}
/**
* Show full screen player
* @param {String} url
*/
async showPlayer(url, isVideo) {
try {
const tpl = await Page.loadHTMLTemplate("components/mediaPlayer.html")
let el = document.createElement("div");
el.innerHTML = tpl;
document.body.appendChild(el);
let target = el.querySelector(".media-target");
if (!isVideo) {
let audio = document.createElement("audio");
audio.controls = true;
audio.src = url;
target.appendChild(audio);
}
else {
let video = document.createElement("video");
video.controls = true;
video.src = url;
target.appendChild(video);
}
el.querySelector(".close").addEventListener("click", e => el.remove());
el.addEventListener("click", e => el.remove());
}
catch(e) {
console.error(e);
notify(tr("Failed to paly media!"), "danger");
}
}
}

View File

@ -330,7 +330,11 @@ const ConversationPageConvPart = {
} }
else if(messageFile.type == "audio/mpeg") { else if(messageFile.type == "audio/mpeg") {
new SmallAudioPlayer(messageContentContainer, messageFile.url) new SmallMediaPlayer(messageContentContainer, messageFile.url, false)
}
else if(messageFile.type == "video/mp4") {
new SmallMediaPlayer(messageContentContainer, messageFile.url, true)
} }
// Fallback // Fallback

View File

@ -1,7 +1,7 @@
<!-- Audio player template --> <!-- media player template -->
<style> <style>
.audio-player { .media-player {
position: fixed; position: fixed;
width: 100%; width: 100%;
height: 100%; height: 100%;
@ -14,24 +14,29 @@
align-items: center; align-items: center;
} }
.audio-player div { .media-player div {
display: flex; display: flex;
align-items: center; align-items: center;
flex-direction: column; flex-direction: column;
} }
.audio-player .close { .media-player .close {
color: white; color: white;
margin: 20px; margin: 20px;
opacity: 1; opacity: 1;
text-shadow: unset; text-shadow: unset;
font-weight: unset; font-weight: unset;
} }
.media-target * {
max-width: 100%;
max-height: 100%;
}
</style> </style>
<div class="audio-player"> <div class="media-player">
<div> <div>
<audio controls></audio> <div class="media-target"></div>
<p class="close a">tr("Close")</p> <p class="close a">tr("Close")</p>
</div> </div>

View File

@ -479,8 +479,8 @@ class Dev {
// Password input // Password input
"js/components/passwordInput.js", "js/components/passwordInput.js",
// Small audio player // Small media player
"js/components/smallAudioPlayer.js", "js/components/smallMediaPlayer.js",
//User scripts //User scripts
"js/user/loginTokens.js", "js/user/loginTokens.js",