From bf5bea7bbba5faa3f54d7bef58e9b2df8525f6e9 Mon Sep 17 00:00:00 2001 From: markveronich Date: Tue, 15 May 2018 21:26:57 +0100 Subject: [PATCH] Add support for MediaStream objects Fixes https://github.com/CookPete/react-player/issues/398 https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject --- README.md | 2 +- src/players/FilePlayer.js | 12 +++++++++++- src/props.js | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6325119..89cf747 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ As of Chrome 66, [videos must be `muted` in order to play automatically](https:/ Prop | Description | Default ---- | ----------- | ------- -`url` | The url of a video or song to play +`url` | The url of a video or song to play
  ◦  Can be an [array](#multiple-sources-and-tracks) or [`MediaStream`](https://developer.mozilla.org/en-US/docs/Web/API/MediaStream) object `playing` | Set to `true` or `false` to pause or play the media | `false` `loop` | Set to `true` or `false` to loop the media | `false` `controls` | Set to `true` or `false` to display native player controls
  ◦  Vimeo, Twitch and Wistia player will always display controls | `false` diff --git a/src/players/FilePlayer.js b/src/players/FilePlayer.js index 559ecaf..0005c8a 100644 --- a/src/players/FilePlayer.js +++ b/src/players/FilePlayer.js @@ -26,6 +26,9 @@ function canPlay (url) { } return false } + if (url instanceof window.MediaStream) { + return true + } return ( AUDIO_EXTENSIONS.test(url) || VIDEO_EXTENSIONS.test(url) || @@ -115,6 +118,13 @@ export class FilePlayer extends Component { this.dash.getDebug().setLogToBrowserConsole(false) }) } + if (url instanceof window.MediaStream) { + try { + this.player.srcObject = url + } catch (e) { + this.player.src = window.URL.createObjectURL(url) + } + } } play () { const promise = this.player.play() @@ -170,7 +180,7 @@ export class FilePlayer extends Component { getSource (url) { const useHLS = this.shouldUseHLS(url) const useDASH = this.shouldUseDASH(url) - if (url instanceof Array || useHLS || useDASH) { + if (url instanceof Array || url instanceof window.MediaStream || useHLS || useDASH) { return undefined } if (MATCH_DROPBOX_URL.test(url)) { diff --git a/src/props.js b/src/props.js index 94bb07d..808eae4 100644 --- a/src/props.js +++ b/src/props.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types' const { string, bool, number, array, oneOfType, shape, object, func } = PropTypes export const propTypes = { - url: oneOfType([ string, array ]), + url: oneOfType([ string, array, object ]), playing: bool, loop: bool, controls: bool,