commit c3aeac004cca9e0bac2ff969636028fd2ac3cafc
parent fc359b4c1a6f2e0c4f3a2be2235cb6dd0dfbe212
Author: Nirmal Kumar R <tildezero@gmail.com>
Date: Tue, 3 Sep 2024 21:34:20 +0530
Enhance player codec logic
Diffstat:
3 files changed, 50 insertions(+), 48 deletions(-)
diff --git a/emptyfm.js b/emptyfm.js
@@ -49,25 +49,25 @@ async function fetchFMStations() {
}
function populateRecents() {
- let saved = localStorage.getItem('emptyfm') ?? '{ "recents": [] }';
+ let saved = localStorage.getItem('emptyfm') ?? '{ "recents": [] }';
let savedObj = JSON.parse(saved);
- recents = savedObj["recents"];
-
- for (recent of recents) {
- constructRecentEl(recent.name, recent.url);
- }
+ recents = savedObj["recents"];
+
+ for (recent of recents) {
+ constructRecentEl(recent.name, recent.url);
+ }
}
function constructRecentEl(name, url) {
- let recentsEl = document.getElementById("recentList");
+ let recentsEl = document.getElementById("recentList");
let litag = document.createElement("li");
let atag = document.createElement("a");
atag.href = url;
atag.innerText = name;
- atag.onclick = (e) => {
- e.preventDefault();
- changeSource(atag.href);
- };
+ atag.onclick = (e) => {
+ e.preventDefault();
+ changeSource(atag.href);
+ };
litag.appendChild(atag);
recentsEl.appendChild(litag);
@@ -84,32 +84,41 @@ function addToRecents(savedObj, name, url) {
let recentsStr = JSON.stringify({ recents: recents });
localStorage.setItem('emptyfm', recentsStr);
- constructRecentEl(name, url);
+ constructRecentEl(name, url);
}
function changeSource(url) {
- document.getElementById("emptyfm").style.display = "block";
+ document.getElementById("emptyfm").style.display = "block";
player = videojs("emptyfm");
- mediaType = fetchMediaType(url);
+ let [mediaURL, mediaType] = fetchMediaType(url);
player.src({
type: mediaType,
- src: url
+ src: mediaURL
+ });
+ player.play().catch(error => {
+ player.stop()
+ [mediaURL, mediaType] = fetchMediaType(mediaURL, "application/x-mpegURL")
+ player.src({
+ type: mediaType,
+ src: mediaURL
+ })
+ player.play()
});
- player.play();
}
-function fetchMediaType(url) {
- last = url.split("/").pop();
- switch (last) {
- case "stream":
- return "audio/mpeg";
+function fetchMediaType(url, defaultType = "audio/mpeg") {
+ const radioBox = new URL(url)
+ let lastPath = radioBox.pathname.split("/").pop();
+ let mType = lastPath.split('.').pop();
+
+ switch (mType) {
+ case "m3u8":
+ return [url, "application/x-mpegURL"];
default:
- ext = last.split(".").pop();
- switch (ext) {
- case "m3u8":
- return "application/x-mpegURL";
- default:
- return "audio/mpeg";
+ if (defaultType != "audio/mpeg") {
+ return [url, "application/x-mpegURL"];
+ } else {
+ return [url, defaultType];
}
}
}
@@ -128,7 +137,7 @@ function listCountries() {
selectElem.appendChild(option);
}
- }
+ }
})
}
@@ -152,5 +161,4 @@ function listLanguages() {
populateRecents();
listCountries();
-listLanguages();
-
+listLanguages();
+\ No newline at end of file
diff --git a/fonts/jetbrains-mono/JetBrainsMonoRegular.woff2 b/fonts/jetbrains-mono/JetBrainsMonoRegular.woff2
Binary files differ.
diff --git a/index.html b/index.html
@@ -15,14 +15,6 @@
--alt: #b7b7b7;
}
- @font-face {
- font-family: 'JetBrains Mono';
- font-style: normal;
- font-weight: normal;
- src: local('JetBrains Mono Regular'), url('/fonts/jetbrains-mono/JetBrainsMonoRegular.woff2') format('woff');
- font-display: swap;
- }
-
body {
font-size: 17px;
line-height: 1.5;
@@ -33,7 +25,7 @@
margin: 2em auto;
background-color: var(--bg);
color: var(--fg);
- font-family: 'JetBrains Mono', monospace;
+ font-family: verdana, arial, sans-serif, helvetica;
letter-spacing: -.02em;
max-width: 590px;
}
@@ -74,7 +66,7 @@
}
button {
- font-family: 'JetBrains Mono', monospace;
+ font-family: verdana, arial, sans-serif, helvetica;
font-size: 1.2rem;
padding: 0 1em;
background: #15C;
@@ -119,15 +111,15 @@
}
.recents li {
- margin: 1em 0;
+ margin: 1em 0;
}
.recents h3 {
- background: #666;
- color: white;
- margin: 0;
- padding: 0 0.7em;
- font-size: 1rem;
+ background: #666;
+ color: white;
+ margin: 0;
+ padding: 0 0.7em;
+ font-size: 1rem;
}
.video-js {
@@ -160,7 +152,7 @@
<a class="logo" href="/">emptyfm</a>
<div class="tagline">Listen to your favorite FM radio.</div>
<div class="nav">
- <a href="https://github.com/mysticmode/emptyfm">Source code</a>
+ <a href="https://github.com/mysticmode/emptyfm-web">Source code</a>
<a href="https://river.sh">A product by Riverway</a>
</div>
@@ -200,4 +192,4 @@
<script src="emptyfm.js"></script>
</body>
-</html>
+</html>
+\ No newline at end of file