Explorar o código

Support playing songs from youtube

Reddo %!s(int64=5) %!d(string=hai) anos
pai
achega
1b77131347
Modificáronse 2 ficheiros con 58 adicións e 0 borrados
  1. 1 0
      The Obelisk.haml
  2. 57 0
      app/Elements/Modules/BGMHandler.ts

+ 1 - 0
The Obelisk.haml

@@ -56,6 +56,7 @@
                 -#inventoryTab.noshrinkFlex
                 #inventoryTarget
           #centerWindow
+            %audio#bgmPlayer{:controls=>"true", :loop=>"true", :style=>"position: absolute; display: none; z-index: 1;"}
             .topBottomFlex
               #currentTurn.growingFlex.scrollbar
                 #turnContainer.topBottomFlex

+ 57 - 0
app/Elements/Modules/BGMHandler.ts

@@ -0,0 +1,57 @@
+/// <reference path="../Elements.ts" />
+module Elements.BGMHandler {
+    var player = <HTMLAudioElement> document.getElementById("bgmPlayer");
+    player.volume = 0.1;
+    player.addEventListener("canplay", function () {
+        this.play();
+        this.style.display = "";
+    });
+    player.addEventListener("error", function () {
+        this.style.display = "none";
+        console.warn("Could not play the BGM ", this.src);
+    });
+    player.addEventListener("pause", function () {
+        this.style.display = "none";
+    });
+
+    function getJsonFromUrl(url) : any {
+        if(!url) url = location.search;
+        var query = url.substr(1);
+        var result = {};
+        query.split("&").forEach(function(part) {
+            var item = part.split("=");
+            result[item[0]] = decodeURIComponent(item[1]);
+        });
+        return result;
+    }
+
+    export function playYT (vidID : string) {
+        let proxyURL = "https://images" + (Math.floor(Math.random() * 33)) + "-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=";
+        let vidInfo = encodeURIComponent("https://www.youtube.com/get_video_info?video_id=") + encodeURIComponent(vidID);
+        fetch(proxyURL + vidInfo).then(response => {
+            if (response.ok) {
+                response.text().then((data : any) => {
+                    data = getJsonFromUrl(data);
+                    let streams = (data.url_encoded_fmt_stream_map + ',' + data.adaptive_fmts).split(',');
+
+                    let valid = [];
+
+                    for (let i = 0; i < streams.length; i++) {
+                        let stream = getJsonFromUrl(streams[i]);
+                        if (stream.type && stream.type.indexOf("audio") == 0) {
+                            valid.push(stream.url);
+                        }
+                    }
+
+                    // Last one has highest quality, if none found results "undefined" which just doesn't play
+                    player.src = valid.pop();
+                });
+            }
+        });
+    }
+
+    /** TODO: Allow players to create a playlist of youtube URLs that vary with certain situations, then play one of those randomly every time situation arises
+              e.g.: A list of songs for Combat, Exploration in the Forest, Talking with the Witch, etc.
+
+     */
+}