Controlling Embedded Youtube Video on MM



  • Hi all,
    Currently I’m facing an issue with my embedded youtube video on MM. My inspiration of controlling the video comes from Jopyth’s Remote Control module( https://github.com/Jopyth/MMM-Remote-Control ).

    So my idea is that whenever i press the “hide-all” button , the remote will send a notification to my youtube module telling it to play the module while hiding the rest of my module and when “show-all” button is pressed, it hides and pauses the video while showing un-hiding the rest of my modules. However, currently im met with a problem, the function, this.sendnotification doesn’t work. The following is portion that i have edited.

       'show-all-button': function() {
            var buttons = document.getElementsByClassName("edit-button");
            for (var i = 0; i < buttons.length; i++) {
                            if (buttons[i].id!="module_5_youtube"){
                buttons[i].className = buttons[i].className.replace("toggled-off", "toggled-on");
                Remote.showModule(buttons[i].id);
                this.sendNotification("PAUSE_VIDEO", payload);
                            }
                            else{
                buttons[i].className = buttons[i].className.replace("toggled-on", "toggled-off");
                Remote.hideModule(buttons[i].id);
                            }
    

    I have added this line "this.sendNotification(“PAUSE_VIDEO”, payload);"
    but MM doesnt process it.

    Is my usage of the code wrong ?


  • Moderator

    @zichao92 First of all you are editing the remote.js file, aren’t you? This is not on the actual mirror website, so you can not directly send notifications from there. This is the js file for the raspberrypi:8080/remote.html website.

    You could however send the notification through a generic notification request.

    Also this in the part I think you are editing, refers to the dictionary, not the the Remote object, since it is outside of the class definition. You could move it inside to fix this, or change this to Remote.



  • @Jopyth , yea I’m using and modifying your remote module. Thanks for the tip, will try it out soon



  • Hi @Jopyth ,

    I looked through your code regarding generic notification request under your MMM remote. However, im not too sure how to incorporate the notification request into my modification. I tried replacing this.sendNotification("PAUSE_VIDEO", payload); with ?action=NOTIFICATION¬ification=PAUSE_VIDEO, however it doesnt work.

    Can you assists me regarding this?


  • Moderator

    @zichao92 Try something like this:

    var notification = "EXAMPLE";
    var payload = "some payload, could be an object";
    var encoded = encodeURIComponent(JSON.stringify(payload));
    Remote.getWithStatus("action=NOTIFICATION&n"+"otification=" + notification + "&payload=" + encoded);
    

    Edit: Added “+” to remove strange replacement with ¬.



  • @Jopyth ,

    I tried that method to send a notification to the node_helper.js and from there, to broadcast the notification ‘PAUSE_VIDEO’ via sendNotifcation to another module with notificationReceived code at the receiving end.

    However, I got an error stating that sendNotifcation as error reported as TypeError: this.sendNotification is not a function . Is there any work around to this?


  • Moderator

    @zichao92 You can not send notifications from the node_helper.js (they are running in the back end), they can only be send from the modules in the front end, i.e. MMM-Remote-Control.js.

    Edit: Also the method I described above should already do all the forwarding of the nofification until it is broad cast to the other modules. Did you change anything in the other files?

    Edit2: These are the lines which should make everything work: In the node_helper.js and in the MMM-Remote-Control.js.



  • @Jopyth, I realized i was using an older version of your remote control. However, after updating the remote control module, i inserted your recommended line back into this code:

      // edit menu buttons
            "show-all-button": function() {
            var parent = document.getElementById("visible-modules-results");
            var buttons = parent.children;
            for (var i = 0; i < buttons.length; i++) {
             if (buttons[i].id!="module_5_youtube"){
                if (Remote.hasClass(buttons[i], "external-locked")) {
                    continue;
                }
                buttons[i].className = buttons[i].className.replace("toggled-off", "toggled-on");
                Remote.showModule(buttons[i].id);
                var notification = "SCREEN_WAKEUP";
                Remote.getWithStatus("action=NOTIFICATION&n"+"otification=" + notification );
                }
            else{
                    if (Remote.hasClass(buttons[i], "external-locked")) {
                    continue;
                }
                buttons[i].className = buttons[i].className.replace("toggled-on", "toggled-off");
                Remote.hideModule(buttons[i].id);
                            }
    

    I was testing this code with paviro’s PIR module ( https://github.com/paviro/MMM-PIR-Sensor) on this line which is found from his MMM-PIR-Sensor.js .

    	notificationReceived: function(notification, payload) {
    		if (notification === "SCREEN_WAKEUP"){
    			this.sendNotification(notification, payload)
    		}
    	},
    

    I modified the PIR code in such a way that when it receives the SCREEN_WAKEUP notification , it will perform certain actions for me rather than turning on the monitor.



  • Hi all, im not too sure if i have made any mistake regarding the structure of MMM.

    I placed my notificationReceived at the front end of embedded youtube, which i called it youtube.js( it doesnt have any node_helper) :

    Here’s the code of my youtube.js , the codes are heavily based on a the default module called “compliments” :

    module.register("youtube",{
    
    	// Module config defaults.
    	defaults: {
    
    		updateInterval: 30000,
    		fadeSpeed: 4000
    	},
    
    
    
    	// Define required scripts.
    	getScripts: function() {
    		return ["moment.js"];
    //		exec("sudo python /home/pi/NicoRFID/RFID_playlist.py");
    	},
    	getStyles: function() {
    	    return [
    	        'script.css', // will try to load it from the vendor folder, otherwise it will load is from the module folder.
    	    ]
    	},
    
    	// Define start sequence.
    	start: function() {
    		Log.info("Starting module: " + this.name);
    
    		
    		// Schedule update timer.
    		var self = this;
    		setInterval(function() {
    			self.updateDom(self.config.fadeSpeed);
    		}, this.config.updateInterval);
    	},
    		
    
    		
    	/* randomIndex(compliments)
    	 * Generate a random index for a list of compliments.
    	 *
    	 * argument compliments Array - Array with compliments.
    	 *
    	 * return Number - Random index.
    	 */
    
    	/* complimentArray()
    	 * Retrieve an array of compliments for the time of the day.
    	 *
    	 * return compliments Array - Array with compliments for the time of the day.
    	 */
    	
    	/* complimentArray()
    	 * Retrieve a random compliment.
    	 *
    	 * return compliment string - A compliment.
    	 */
    		notificationReceived: function(notification, payload) {
    		if (notification === "PAUSE_VIDEO"){
    			pausethevideo()
    
    		}
    		else if (notification === "PLAY_VIDEO"){
    			playthevideo()
    
    		}
    	},
    
    
    	// Override dom generator.
    	getDom: function() {
    		//var complimentText = this.randomCompliment();
    		//var complimentText = "Hi NUS GOD Puay Hiang ";
    
    		//var compliment = document.createTextNode(complimentText);
    
    		var wrapper = document.createElement("div");
    		wrapper.className = "thin xlarge bright";
    
    		function playthevideo(){
    		var myPlayer = document.getElementById('my-video');
    		myPlayer.playVideo();
    		};
    
    		function stopthevideo(){
    		var myPlayer = document.getElementById('my-video');
    		myPlayer.stopVideo();
    		};
    
    		function pausethevideo(){
    		var myPlayer = document.getElementById('my-video'); 
    		myPlayer.pauseVideo();
    		};
    		
    		
    		wrapper.innerHTML='<div> <div> allowfullscreen></div></div>';
    
    		
    
    		
    
    			
    		//wrapper.appendChild(compliment);
    
    		return wrapper;
    	}
    
    });
    

    So with the notificationReceived line, i intend to control my video using the remote control with these codes :

    / edit menu buttons
            "show-all-button": function() {
            var parent = document.getElementById("visible-modules-results");
            var buttons = parent.children;
            for (var i = 0; i < buttons.length; i++) {
             if (buttons[i].id!="module_5_youtube"){
                if (Remote.hasClass(buttons[i], "external-locked")) {
                    continue;
                }
                buttons[i].className = buttons[i].className.replace("toggled-off", "toggled-on");
                Remote.showModule(buttons[i].id);
                var notification = "PAUSE_VIDEO";
                Remote.getWithStatus("action=NOTIFICATION&n"+"otification=" + notification );
                }
            else{
                    if (Remote.hasClass(buttons[i], "external-locked")) {
                    continue;
                }
                buttons[i].className = buttons[i].className.replace("toggled-on", "toggled-off");
                Remote.hideModule(buttons[i].id);
                            }
    

    Am i doing something wrong here?



  • @Jopyth ,

    I managed to have some developments with your remote control module’s sendNotifcation command. I hooked up the MM using a browser and observed( using dev console, Note to beginners: press F12 on a browser to access this cool feature ) that a notification was send from MM Remote. This means that you were right all along, just that i didnt know how to observe the sendNotification command via dev console.

    Currently, i have another issue. My youtube.js module receives the “PLAY_VIDEO” or “PAUSE_VIDEO” notification but is not able to perform what was desired.

    Here’s my snippet of my code from my youtube.js :

    notificationReceived: function(notification, payload) {
    		if (notification === "PAUSE_VIDEO"){
    			pausethevideo()
    
    		}
    		if (notification === "PLAY_VIDEO"){
    			playthevideo()
    
    		}
    	},
    
    getDom: function() {
    var wrapper = document.createElement("div");
    		wrapper.className = "thin xlarge bright";
    
    		function playthevideo(){
    		var myPlayer = document.getElementById('my-video');
    		myPlayer.playVideo();
    		};
    
    		function stopthevideo(){
    		var myPlayer = document.getElementById('my-video');
    		myPlayer.stopVideo();
    		};
    
    		function pausethevideo(){
    		var myPlayer = document.getElementById('my-video'); 
    		myPlayer.pauseVideo();
    		};
    		
    		
    		wrapper.innerHTML='<div> <div> allowfullscreen></div></div>'; //youtube video link found here.
    
    		
    
    			return wrapper;
    	}
    
    });
    

    However, i got an error from dev console :
    Uncaught TypeError: Cannot read property 'pauseVideo' of null at Class.notificationReceived (youtube.js:69)

    Im suspecting that my pauseVideo/playVideo function wasn’t recognised as it is getDom.

    Will appreciate any help or advice !


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.