obs-websocket-js play audio url

185 views Asked by At

I need to switch the scene and play an audio file through a third party url

Previously it could have been done something like this

const OBSWebSocket = require('obs-websocket-js');
const obs = new OBSWebSocket();

obs.connect(obsSettings)
   .then(() => {
         obs.send('SetCurrentScene', { 'scene-name': 'YourSceneName' });
         obs.send('PlayAudio', { source: 'YourAudioSourceName', file: url });
             console.log('Audio playback started');
         })
         .catch(err => {
             console.error(`OBS connection error: ${err}`);
  });

but now the obs.send command does not work, instead of it obs.call, and let’s say to change the scene I need to use this code

obs.call('SetCurrentProgramScene', { sceneName: 'YourSceneName' });

But how can I play audio in obs? I can't find how this is currently implemented

In general, the essence of my question is to translate the text in a voice message and display it in obs

This is how I do things

const voiceUrl = googleTTS.getAudioUrl(response, {
       lang: 'en',
       slow: false,
       host: 'https://translate.google.com',
  });
console.log(`Voice message: ${voiceUrl}`);
playVoice('Voice', voiceUrl);

I get a link to this voice message, then call the playVoice function to play it in obs

const OBSWebSocket = require('obs-websocket-js').default;
const obs = new OBSWebSocket();

function playVoice(sourceName, voiceUrl) {
    obs.call('SetCurrentProgramScene', { sceneName: 'Main' });
    obs.call('PlayAudio', { source: sourceName, file: voiceUrl });
}

SetCurrentProgramScene with scene change works as it should. But how can I play this audio through url? PlayAudio request not working

2

There are 2 answers

1
Petri Järvisalo On BEST ANSWER

This is not exact answer to your question, I think the websocket api seems not to support this sort of activity anymore (see https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests ) Have you thought different approach to this problem:

You could do just web app using node and express + socket.io... and serve empty (and transparent) webpage that plays the audio using javascript and tag and add the page as browser source.

0
AlbertSY On

With OBS WebSocket, you can play any audio stream as you would do it manually.

But the command PlayAudio you mentioned does not exist.

Instead you should use TriggerMediaInputAction like below

obs.call('TriggerMediaInputAction', { inputName: your_source_name, mediaAction: "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART"});

This only works if you have already setup correctly your_source_name in the scene.

You can do it manually or programmaticaly (by using CreateInput)

You should refer to CreateInput documentation to do so. As this is beyond your original question, i'm not going to elaborate on this subject. You can still ask if you need helps.