Skip to content

Commit 8f3c975

Browse files
committed
Set volume to null by default
- Fixes cookpete/react-player#357 - Fixes `onStart` not firing when changing URLs - Moves the forced `setVolume` to run on ready instead of every play event
1 parent e35f843 commit 8f3c975

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ Prop | Description | Default
6565
`url` | The url of a video or song to play
6666
`playing` | Set to `true` or `false` to pause or play the media | `false`
6767
`loop` | Set to `true` or `false` to loop the media | `false`
68-
`controls` | Set to `true` or `false` to display native player controls<br />*Note: Vimeo, Twitch and Wistia player controls are not configurable and will always display* | `false`
69-
`volume` | Sets the volume of the appropriate player | `0.8`
70-
`muted` | Mutes the player | `false`
71-
`playbackRate` | Sets the playback rate of the appropriate player<br />*Note: Only supported by YouTube, Wistia, and file paths* | `1`
72-
`width` | Sets the width of the player | `640px`
73-
`height` | Sets the height of the player | `360px`
68+
`controls` | Set to `true` or `false` to display native player controls<br />&nbsp;&nbsp;Vimeo, Twitch and Wistia player will always display controls | `false`
69+
`volume` | Set the volume of the player, between `0` and `1`<br/>&nbsp;&nbsp;`null` uses default volume on all players [`#357`](https://github.com/CookPete/react-player/issues/357) | `null`
70+
`muted` | Mutes the player<br/>&nbsp;&nbsp;Only works if `volume` is set | `false`
71+
`playbackRate` | Set the playback rate of the player<br />&nbsp;&nbsp;Only supported by YouTube, Wistia, and file paths | `1`
72+
`width` | Set the width of the player | `640px`
73+
`height` | Set the height of the player | `360px`
7474
`style` | Add [inline styles](https://facebook.github.io/react/tips/inline-styles.html) to the root element | `{}`
7575
`progressInterval` | The time between `onProgress` callbacks, in milliseconds | `1000`
7676
`playsinline` | Applies the `playsinline` attribute where supported | `false`

src/Player.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default class Player extends Component {
3232
const { url, playing, volume, muted, playbackRate } = this.props
3333
if (url !== nextProps.url) {
3434
this.isLoading = true
35+
this.startOnPlay = true
3536
this.onDurationCalled = false
3637
this.player.load(nextProps.url, this.isReady)
3738
}
@@ -41,11 +42,13 @@ export default class Player extends Component {
4142
if (playing && !nextProps.playing && this.isPlaying) {
4243
this.player.pause()
4344
}
44-
if (volume !== nextProps.volume && !nextProps.muted) {
45-
this.player.setVolume(nextProps.volume)
46-
}
47-
if (muted !== nextProps.muted) {
48-
this.player.setVolume(nextProps.muted ? 0 : nextProps.volume)
45+
if (nextProps.volume !== null) {
46+
if (volume !== nextProps.volume && !nextProps.muted) {
47+
this.player.setVolume(nextProps.volume)
48+
}
49+
if (muted !== nextProps.muted) {
50+
this.player.setVolume(nextProps.muted ? 0 : nextProps.volume)
51+
}
4952
}
5053
if (playbackRate !== nextProps.playbackRate && this.player.setPlaybackRate) {
5154
this.player.setPlaybackRate(nextProps.playbackRate)
@@ -116,8 +119,11 @@ export default class Player extends Component {
116119
if (!this.mounted) return
117120
this.isReady = true
118121
this.isLoading = false
119-
const { onReady, playing } = this.props
122+
const { onReady, playing, volume, muted } = this.props
120123
onReady()
124+
if (muted || volume !== null) {
125+
this.player.setVolume(muted ? 0 : volume)
126+
}
121127
if (playing) {
122128
this.player.play()
123129
}
@@ -126,15 +132,14 @@ export default class Player extends Component {
126132
onPlay = () => {
127133
this.isPlaying = true
128134
this.isLoading = false
129-
const { volume, muted, onStart, onPlay, playbackRate } = this.props
135+
const { onStart, onPlay, playbackRate } = this.props
130136
if (this.startOnPlay) {
131137
if (this.player.setPlaybackRate) {
132138
this.player.setPlaybackRate(playbackRate)
133139
}
134140
onStart()
135141
this.startOnPlay = false
136142
}
137-
this.player.setVolume(muted ? 0 : volume)
138143
onPlay()
139144
if (this.seekOnPlay) {
140145
this.seekTo(this.seekOnPlay)

src/props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const defaultProps = {
6666
playing: false,
6767
loop: false,
6868
controls: false,
69-
volume: 0.8,
69+
volume: null,
7070
muted: false,
7171
playbackRate: 1,
7272
width: '640px',

0 commit comments

Comments
 (0)