jsmpeg icon indicating copy to clipboard operation
jsmpeg copied to clipboard

url.match is not a function

Open oleaasbo opened this issue 5 years ago • 6 comments

Hello. I have been using jsmpeg for a while and decided to update it to the newest release. I get a TypeError when calling new JSMpeg.Player()

Uncaught TypeError: url.match is not a function at new Player (jsmpeg.min.js:1) at startWebsocket (card.js:63) at HTMLDocument. (card.js:6) at i (jquery-2.2.3.min.js:2) at Object.fireWith [as resolveWith] (jquery-2.2.3.min.js:2) at Function.ready (jquery-2.2.3.min.js:2) at HTMLDocument.J (jquery-2.2.3.min.js:2)

Code lookes like this:

var websocket = new WebSocket(location);
....

var player = new JSMpeg.Player(websocket, {
    canvas: canvas,
    autoplay: true,
    loop: true,
    audio: false,
    pauseWhenHidden: false,
    progressive: true,
    videoBufferSize: 524288, //512*1024 (512kb)
    onPlay: mpegPlay,
    onStalled: mpegStalled,
    onSourceEstablished: mpegEstablished
});

oleaasbo avatar Dec 05 '19 13:12 oleaasbo

You have to call new JSMpeg.Player() with the WebSocket URL, not a WebSocket instance. E.g.:

var player = new JSMpeg.Player('ws://1.2.3.4/', {...}

phoboslab avatar Dec 06 '19 07:12 phoboslab

Ok. Thanks :) I have used the player with an instance of websocket in earlier version of jsmpeg. I execute some code on websocket events (e.g 'connected' or 'close'). Does the current release support that somehow?

oleaasbo avatar Dec 06 '19 07:12 oleaasbo

You can probably use the onSourceEstablished callback in the constructor - this is fired when the connection first receives data.

A close event is not directly exposed, but you can access the WS connection through the player E.g: player.source.socket.addEventListener('close', onCloseCallback);.

Keep in mind though that the WS connection automatically tries to reconnect whenever the connection is lost. You can disable this by specifying reconnectInterval: 0 in the constructor.

phoboslab avatar Dec 06 '19 07:12 phoboslab

Hello, I am also trying to use JSMpeg to view video streamed via web-sockets, however I have already established the Web-socket connection in order to control the video start / stop, my requirements go beyond simple start / stop commands.

Could anyone provide an example on how to get JSMpeg to use a web-socket instance in place of a newly instantiated web-socket.

Thanks in advance.

Chrisbarm

chrisbarm avatar Apr 22 '22 08:04 chrisbarm

Create a new Source class that meets your requirements (see websocket.js for an example) and instantiate the JSMpeg.Player with that class:

var MyCustomWebSocketSource = function(socket, options) {
	this.socket = socket;
	// (...)
};

// Implement the API for this source class:
// .connect(), .write(), .start(), .resume(),
// .established, .complete, .progress
// (again, see websocket.js for an example)

// Create a new player instance with you existing websocket
// and your custom source class:
var player = new JSMpeg.Player(socket, {
	source: MyCustomWebSocketSource
});

phoboslab avatar Apr 22 '22 08:04 phoboslab

I will take a look at this.

Thank you very much for you quick response!

chrisbarm avatar Apr 22 '22 08:04 chrisbarm