Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 142 #153

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/app/modules/webcam/webcam/webcam.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class WebcamComponent implements AfterViewInit, OnDestroy {

/** width and height of the active video stream */
private activeVideoSettings: MediaTrackSettings = null;
private promise: Promise<void>;

/**
* If the given Observable emits, an image will be captured and emitted through 'imageCapture' EventEmitter
Expand Down Expand Up @@ -316,7 +317,7 @@ export class WebcamComponent implements AfterViewInit, OnDestroy {
// merge deviceId -> userVideoTrackConstraints
const videoTrackConstraints = WebcamComponent.getMediaConstraintsForDevice(deviceId, userVideoTrackConstraints);

navigator.mediaDevices.getUserMedia(<MediaStreamConstraints>{video: videoTrackConstraints})
this.promise = navigator.mediaDevices.getUserMedia(<MediaStreamConstraints>{video: videoTrackConstraints})
.then((stream: MediaStream) => {
this.mediaStream = stream;
_video.srcObject = stream;
Expand All @@ -329,7 +330,7 @@ export class WebcamComponent implements AfterViewInit, OnDestroy {

// Initial detect may run before user gave permissions, returning no deviceIds. This prevents later camera switches. (#47)
// Run detect once again within getUserMedia callback, to make sure this time we have permissions and get deviceIds.
this.detectAvailableDevices()
return this.detectAvailableDevices()
.then(() => {
this.activeVideoInputIndex = activeDeviceId ? this.availableVideoInputs
.findIndex((mediaDeviceInfo: MediaDeviceInfo) => mediaDeviceInfo.deviceId === activeDeviceId) : -1;
Expand Down Expand Up @@ -389,13 +390,22 @@ export class WebcamComponent implements AfterViewInit, OnDestroy {
* even if it is no longer used by this component.
*/
private stopMediaTracks() {
if (this.mediaStream && this.mediaStream.getTracks) {
// pause video to prevent mobile browser freezes
this.nativeVideoElement.pause();

// getTracks() returns all media tracks (video+audio)
this.mediaStream.getTracks()
.forEach((track: MediaStreamTrack) => track.stop());
if (this.promise !== undefined && this.promise !== null) {
this.promise.then(
() => {
if (this.mediaStream && this.mediaStream.getTracks) {
// pause video to prevent mobile browser freezes
this.nativeVideoElement.pause();
}
// getTracks() return all media tracks (video+audio)f
this.mediaStream.getTracks()
.forEach((track: MediaStreamTrack) => {
track.stop();
this.mediaStream.removeTrack(track);
})
}
)
}
}

Expand Down