From 0a658dbcc0b5c9bdfbabedd8b50117a335c42d83 Mon Sep 17 00:00:00 2001 From: Vincent Gabriel Date: Tue, 10 Jul 2018 10:35:55 -0700 Subject: [PATCH] added ability to programatically start/stop webcam --- README.md | 4 +++- demo/src/main.vue | 8 ++++++++ dist/index.js | 15 +++++++++++---- package.json | 2 +- src/webcam.vue | 17 +++++++++++++---- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c78758f..f058c78 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,9 @@ npm run dev | name | param | notes | | -------------- | -------- | ----------------------------------------------------------------------- | | capture | void | Capture the current image through the webcam as base64 encoded string | -| changeCamera | deviceId | change the currently selected camera. Must pass in the device ID | +| changeCamera | deviceId | change the currently selected camera. Must pass in the device ID | +| start | void | Programatically Start the camera after stopping it (relies on deviceId prop passed to the component) | +| stop | void | Programatically stop the camera | ## License diff --git a/demo/src/main.vue b/demo/src/main.vue index 591c4c9..7f19783 100644 --- a/demo/src/main.vue +++ b/demo/src/main.vue @@ -24,6 +24,8 @@
+ +
@@ -81,6 +83,12 @@ export default { onStopped(stream) { console.log('On Stopped Event', stream); }, + onStop() { + this.$refs.webcam.stop(); + }, + onStart() { + this.$refs.webcam.start(); + }, onError(error, stream) { console.log('On Error Event', error, stream); }, diff --git a/dist/index.js b/dist/index.js index 154073b..ac1db66 100644 --- a/dist/index.js +++ b/dist/index.js @@ -192,10 +192,7 @@ exports.default = { }); }, changeCamera: function changeCamera(deviceId) { - if (this.$refs.video !== null && this.$refs.video.srcObject) { - this.stopStreamedVideo(this.$refs.video); - } - + this.stop(); this.$emit('camera-change', deviceId); this.loadCamera(deviceId); }, @@ -220,6 +217,16 @@ exports.default = { }); videoElem.srcObject = null; }, + stop: function stop() { + if (this.$refs.video !== null && this.$refs.video.srcObject) { + this.stopStreamedVideo(this.$refs.video); + } + }, + start: function start() { + if (this.deviceId) { + this.loadCamera(this.deviceId); + } + }, testMediaAccess: function testMediaAccess() { var _this3 = this; diff --git a/package.json b/package.json index 8280930..9ea7cb2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-web-cam", - "version": "1.2.2", + "version": "1.2.3", "description": "Webcam component for Vuejs applications", "main": "dist/index.js", "author": { diff --git a/src/webcam.vue b/src/webcam.vue index d7af4f6..c77364c 100644 --- a/src/webcam.vue +++ b/src/webcam.vue @@ -109,10 +109,7 @@ export default { * change to a different camera stream, like front and back camera on phones */ changeCamera(deviceId) { - if(this.$refs.video !== null && this.$refs.video.srcObject) { - this.stopStreamedVideo(this.$refs.video); - } - + this.stop(); this.$emit('camera-change', deviceId); this.loadCamera(deviceId); }, @@ -144,6 +141,18 @@ export default { }); videoElem.srcObject = null; }, + // Stop the video + stop() { + if(this.$refs.video !== null && this.$refs.video.srcObject) { + this.stopStreamedVideo(this.$refs.video); + } + }, + // Start the video + start() { + if(this.deviceId) { + this.loadCamera(this.deviceId); + } + }, /** * test access */