Code Monkey home page Code Monkey logo

media-player-controller's Introduction

media-player-controller

License npm Downloads Donate Donate

Spawn media player app and control playback. Also allows reading player state through socket or web connection.

Supported Media Players

Player Linux Win MacOS
mpv yes no ?
vlc yes yes ?

? - untested (feedback is welcome)

Player config

var player = new PlayerController({
  app: 'mpv',                         // Media player name to use (mpv/vlc)
  args: [],                           // Player command line args (array of strings)
  cwd: null,                          // Current working dir for media player spawn
  media: '/path/to/video.mp4',        // Media to load on player launch (required)
  ipcPath: '/tmp/media-ctl-socket',   // Path to socket connection file (mpv only)
  httpPort: 9280,                     // HTTP port for local communication (vlc only)
  httpPass: null,                     // HTTP login password (vlc only, defaults to player name)
  detached: false                     // Spawn player as detached process
});

Player functions

Each playback controlling function have an optional callback with eventual error.
They are executed asynchronously by default and can be used after playback-started event.

.launch()                    // Launch media player
.quit()                      // Stop media player process

.play()                      // Resume playback
.pause()                     // Pause playback
.cyclePause()                // Cycle play/pause
.load(mediaPath)             // Load new media file (without closing player)
.seek(position)              // Seek to position (value in seconds)
.setVolume(value)            // Adjust player volume (value 0-1)
.setSpeed(value)             // Adjust playback speed (normal: 1.0)
.setRepeat(isEnabled)        // Repeat playback (true or false)
.cycleVideo()                // Switch active video track
.cycleAudio()                // Switch active audio track
.cycleSubs()                 // Switch active subtitle track
.addSubs(subsPath)           // Add subtitles to currently playing video
.setFullscreen(isEnabled)    // Enable or disable fullscreen (true or false)
.cycleFullscreen()           // Toggle fullscreen on/off
.keepOpen(isEnabled)         // Keep player open after playback (true or false)

.command([args])             // Custom command for IPC/HTTP (array of args)

Please note that some commands are not yet implemented for every player (most of them are). If a command is not available for currently used player, callback will return error with a message saying so.

Available events

.on('app-launch')            // Emited on media player app launch
.on('playback-started')      // Playback started and player can now be controlled
.on('playback', data)        // Data object with current playback event
.on('app-exit', code)        // Exit code emited on media player app close

Usage Example

const PlayerController = require('media-player-controller');

var player = new PlayerController({
  app: 'mpv',
  args: ['--fullscreen'],
  media: 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4'
});

player.on('playback', console.log);

player.on('playback-started', () => {
  console.log('Playback started. Player can now be controlled');
});

player.on('app-exit', (code) => {
  console.log(`Media player closed. Exit code: ${code}`);
});

player.launch(err => {
  if(err) return console.error(err.message);
  console.log('Media player launched');
});

Donation

If you like my work please support it by buying me a cup of coffee :-)

PayPal

media-player-controller's People

Contributors

bertwesarg avatar rafostar avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

bertwesarg yxl76

media-player-controller's Issues

MacOS issue

I tried to install this module on Electron application.
It works very well. On MacOS I have a problem.

Once run the funcion a message log in console say:

Error: Media player exited with error code: 1
    at ChildProcess.<anonymous> (/Users/Shared/Reaper…roller/index.js:146)
    at Object.onceWrapper (events.js:291)
    at ChildProcess.emit (events.js:203)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:272)

I tried to run the command on bash and it works:
/Applications/VLC.app/Contents/MacOS/VLC /HD Video/Videos/A.mp4

and also by child process:

var child_process = require ("child_process");
var proc= child_process.spawn ($('#vlcPath').val(), [$('#video').val() ], { shell: true });

proc.stderr.on ("data", (data) => {
    console.error ("VLC: " + data.toString ());
});

proc.stdout.on ("data", (data) => {
    console.log ("VLC: " + data.toString ());
});

proc.on ("exit", (code, signal) => {
    // Every code > 0 indicates an error.
    console.log ("VLC exited with code " + code);
});

and worked.

So, is there something that I wrong?
I'm here to test if you want to release new updates.

Thx

Player working but can't access its functions

Hi,
first thank you for this package, it is exactly what I needed for a project on a raspberry pi 4 and I can't see anything else similar.
I do have a problem however: I can create a player object and play a video with the opts.media parameters (on mpv or vlc), however I cannot use any method.
Whatever I am using:

player.load("videos/1-01.mp4");
player.setFullscreen(true);
player.setVolume(50);
player.keepOpen(true);

I always have something like:
player.setFullscreen(true);
^
TypeError: player.setFullscreen is not a function

When I use player.load(), I have the following error:
this._load(media, (err) =>
^
TypeError: this._load is not a function
at PlayerController.load

Is there anything wrong on what I am doing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.