NodeJS: How to trigger the Pause/Play Media Key on Windows - node.js

I simply want to simulate/trigger the Pause/Play media key. Same thing as when you click on your wireless earbuds to pause/play music currently playing from any program/medium (such as YouTube).
I tried using robotjs, "Error: Invalid key code specified."
robot.keyTap("mediaplaypause");
I tried using xdotool (https://github.com/ebranlard/xdotool-for-windows) and nothing happens:
exec("xdotool key XF86AudioPlay", (error, stdout, stderr) => { ...
No errors though from xdotool.
Any ideas on what I can do to simulate this dang key/function?

Related

How to capture PowerShell Write-Progress text in child spawn process in node.js?

In node.js I create child processes like this for example
child_process.spawn('powershell.exe', ['-executionpolicy', 'remotesigned', '-File', 'save_site_login_web.ps1', data.app_catalogue_url, data.ids.join(",")], { windowsHide:true });
const handler = (data) => {
// send data to client
};
watcher_shell.stdout.on('data', handler);
watcher_shell.stderr.on('data', handler);
However, in the child process, it displays a progress bar like this picture (I got this from running it manually from a powershell terminal). The area on top with the light blue background is static and stays on top, while the text in it updates.
However this progress text doesn't get captured in stdout or stderr. How can I capture this stream in node.js?
Thanks
As mklement0 stated, the progress stream is not directly redirectable.
That being said, since Windows 1809+ there is the new pseudo console (ConPTY) API, which basically allows any program to act like the console, thus capturing output that is written directly to the console.
Doing a quick search, there is the node-pty module which supposedly utilizes the ConPTY API on Windows. I haven't tried it but it looks promising (VS Code is using it for its integrated terminal).
Write-Progress output isn't part of PowerShell's system of output streams, so it cannot be captured - neither in-session from PowerShell, nor externally via stdout or stderr.
From the linked about_Redirection help topic:
There is also a Progress stream in PowerShell, but it does not support redirection.
zett42's helpful answer shows a workaround on Windows that doesn't rely on PowerShell's features.

Detect Key Press in System

I am trying to detect a keypress event outside the console tab, I have allready tried input-event (for linux), hotkeys-js (for browser), and iohook (gives error). How can I do it?
I found a way. I used iohook, but the version 0.6.6 since the new version crashed, and called the start method.
iohook.on('keypress', (res) => {
let key = String.fromCharCode(res.rawcode);
console.log(key);
});
iohook.start();

Electron disable specific camera device access or set default webcam

We are using atom electron to run an app in a "kiosk" type setting, we have 4 webcam devices connected to the physical computer, we want specifically 1 of those to be used for webrtc inside of the electron browser, then other 3 we use some C code to capture still frames. We know the USB path or the /dev/video{#} for the device we want.
Is there a way to either disable access by the view layer to the 3 webcams in node before we launch the electron window? Or another option is to set the default camera before we launch the view layer so that it will default to the webcam we want.
In the view layer we can get a list of devices and see if they are audio or video, but we can't get their /dev/video# or their USB path to figure out which one is the target webrtc cam, so this has not been very helpful yet.
Any help is great, I feel weird for having to post a question since for the last 12 years I have been able to find what I needed by searching, but its been about 3 hours so its time to ask for help.
I don't think Chromium provides a way to retrieve the USB path for a media source. You'll probably need to display a configuration screen to allow the user to select the correct camera (similar to this demo) the first time around and then use the source/device id as a mandatory constraint from that point on.
Another option is to find the correct camera based on the device label, though obviously this will only work if each camera has a distinct label. You can get all the device labels and ids by running this snippet in the DevTools console:
navigator.mediaDevices.enumerateDevices()
.then(devices => devices.forEach(
device => console.log(`kind: ${device.kind}: ${device.label} id=${device.deviceId}`)
))
.catch(err => console.log(err));
Either way, once you have the source/device id you can specify it as a mandatory constraint to ensure that only that one particular camera is used:
navigator.webkitGetUserMedia(
{
audio: false,
video: {
mandatory: {
chromeMediaSourceId: 'the camera source id obtained earlier',
}
}
},
stream => console.dir(stream),
error => console.log(error)
);

Chrome "stalling" when streaming mp3 file from nodejs windows only

We've got a really annoying bug when trying to send mp3 data. We've got the following set up.
Web cam producing aac -> ffmpeg convert to adts -> send to nodejs server -> ffmpeg on server converts adts to mp3 -> mp3 then streamed to browser.
This works *perfectly" on Linux ( chrome with HTML5 and flash, firefox flash only )
However on windows the sound just "stalls", no matter what combination we use ( browser/html5/flash ). If however we shutdown the server the sound then immediately starts to play as we expect.
For some reason on windows based machines it's as if the sound is being buffered "waiting" for something but we don't know what that is.
Any help would be greatly appreciated.
Relevant code in node
res.setHeader('Connection', 'Transfer-Encoding');
res.setHeader('Content-Type', 'audio/mpeg');
res.setHeader('Transfer-Encoding', 'chunked');
res.writeHeader('206');
that.eventEmitter.on('soundData', function (data) {
debug("Got sound data" + data.cameraId + " " + req.params.camera_id);
if (req.params.camera_id == data.cameraId) {
debug("Sending data direct to browser");
res.write(data.sound);
}
});
Code on browser
soundManager.setup({
url: 'http://dashboard.agricamera.co.uk/themes/agricamv2/swf/soundmanager2.swf',
useHTML5Audio: false,
onready: function () {
that.log("Sound manager is now ready")
var mySound = soundManager.createSound({
url: src,
autoLoad: true,
autoPlay: true,
stream: true,
});
}
});
If however we shutdown the server the sound then immediately starts to play as we expect.
For some reason on windows based machines it's as if the sound is being buffered "waiting" for something but we don't know what that is.
That's exactly what's happening.
First off, chrome can play ADTS streams so if possible, just use that directly and save yourself some audio quality by not having to use a second lossy codec in the chain.
Next, don't use soundManager, or at least let it use HTML5 audio. You don't need the Flash fallback these days in most cases, and Chrome is perfectly capable of playing your streams. I suspect this is where your problem lies.
Next, try disabling chunked transfer. Many clients don't like transfer encoding on streams.
Finally, I have seen cases where Chrome's built-in media handling (which I believe varies from OS to OS) cannot sync to the stream. There are a few bug tickets out there for Chromium. If your playback timer isn't incrementing, this is likely your problem and you can simply try to reload the stream programmatically to work around it.

How to resume after pause in jPlayer

I am working with jPlayer 2.4.
I have used the call: player.pause() to pause the audio, this works fine.
However, there is no 'resume' or 'continue' calls, so I used the call: player.play().
But, it starts at the beginning of the song.
What is the proper way to pause and resume a song in jPlayer?
According to jPlayer dev guide:
$(id).jPlayer( "play", [Number: time] ) : jQuery
Description This
method is used to play the media specified using jPlayer("setMedia",
media).
If necessary, the file will begin downloading.
Without the time parameter, new media will play from the start. Open
media will play from where the play-head was when previously paused
using jPlayer("pause", [time]).
So, considere to review your code and make use of the correct jsplayer selector like $('jquery_jplayer').jPlayer('play');. It should do the trick.

Resources