-
Notifications
You must be signed in to change notification settings - Fork 414
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
track and manage video state #890
Changes from 4 commits
6c4a4e5
7d29c4b
11a5f9d
61b8e8e
a8988d9
bfe5fe8
ecd2207
345badb
504c546
0cfc9ab
566fb2e
5765c6c
7bc2f88
d3047a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ import { connect } from "react-redux"; | |
import { doChangeVolume } from "redux/actions/app"; | ||
import { selectVolume } from "redux/selectors/app"; | ||
import { doPlayUri, doSetPlayingUri } from "redux/actions/content"; | ||
import { setVideoPause } from "redux/actions/video"; | ||
import { doPlay, doPause, savePosition } from "redux/actions/media"; | ||
// import { setVideoPause } from "redux/actions/video"; | ||
import { | ||
makeSelectMetadataForUri, | ||
makeSelectContentTypeForUri, | ||
|
@@ -15,7 +16,11 @@ import { | |
} from "redux/selectors/file_info"; | ||
import { makeSelectCostInfoForUri } from "redux/selectors/cost_info"; | ||
import { selectShowNsfw } from "redux/selectors/settings"; | ||
import { selectVideoPause } from "redux/selectors/video"; | ||
// import { selectVideoPause } from "redux/selectors/video"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove this comment when ready to merge |
||
import { | ||
selectMediaPaused, | ||
makeSelectMediaPositionForUri, | ||
} from "redux/selectors/media"; | ||
import Video from "./view"; | ||
import { selectPlayingUri } from "redux/selectors/content"; | ||
|
||
|
@@ -29,14 +34,19 @@ const select = (state, props) => ({ | |
playingUri: selectPlayingUri(state), | ||
contentType: makeSelectContentTypeForUri(props.uri)(state), | ||
volume: selectVolume(state), | ||
videoPause: selectVideoPause(state), | ||
// videoPause: selectVideoPause(state), | ||
mediaPaused: selectMediaPaused(state), | ||
mediaPosition: makeSelectMediaPositionForUri(props.uri)(state), | ||
}); | ||
|
||
const perform = dispatch => ({ | ||
play: uri => dispatch(doPlayUri(uri)), | ||
cancelPlay: () => dispatch(doSetPlayingUri(null)), | ||
changeVolume: volume => dispatch(doChangeVolume(volume)), | ||
setVideoPause: val => dispatch(setVideoPause(val)), | ||
// setVideoPause: val => dispatch(setVideoPause(val)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove this comment when ready to merge |
||
doPlay: () => dispatch(doPlay()), | ||
doPause: () => dispatch(doPause()), | ||
savePosition: (id, position) => dispatch(savePosition(id, position)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what kind of id? I assume this is a |
||
}); | ||
|
||
export default connect(select, perform)(Video); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,10 @@ class VideoPlayer extends React.PureComponent { | |
this.togglePlayListener = this.togglePlay.bind(this); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
if (nextProps.videoPause) { | ||
this.refs.media.children[0].pause(); | ||
this.props.setVideoPause(false); | ||
} | ||
componentWillReceiveProps(next) { | ||
const el = this.refs.media.children[0]; | ||
if (!this.props.paused && next.paused && !el.paused) el.pause(); | ||
// if (this.props.paused && !next.paused && el.paused) el.play(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comment please |
||
} | ||
|
||
componentDidMount() { | ||
|
@@ -35,7 +34,10 @@ class VideoPlayer extends React.PureComponent { | |
mediaType, | ||
changeVolume, | ||
volume, | ||
mediaId, | ||
position, | ||
} = this.props; | ||
|
||
const loadedMetadata = e => { | ||
this.setState({ hasMetadata: true, startedPlaying: true }); | ||
this.refs.media.children[0].play(); | ||
|
@@ -68,6 +70,12 @@ class VideoPlayer extends React.PureComponent { | |
document.addEventListener("keydown", this.togglePlayListener); | ||
const mediaElement = this.refs.media.children[0]; | ||
if (mediaElement) { | ||
mediaElement.currentTime = position; | ||
mediaElement.addEventListener("play", () => this.props.doPlay()); | ||
mediaElement.addEventListener("pause", () => this.props.doPause()); | ||
mediaElement.addEventListener("timeupdate", () => | ||
this.props.savePosition(mediaId, mediaElement.currentTime) | ||
); | ||
mediaElement.addEventListener("click", this.togglePlayListener); | ||
mediaElement.addEventListener( | ||
"loadedmetadata", | ||
|
@@ -93,6 +101,7 @@ class VideoPlayer extends React.PureComponent { | |
if (mediaElement) { | ||
mediaElement.removeEventListener("click", this.togglePlayListener); | ||
} | ||
this.props.doPause(); | ||
} | ||
|
||
renderAudio(container, autoplay) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,8 +56,11 @@ class Video extends React.PureComponent { | |
changeVolume, | ||
volume, | ||
uri, | ||
videoPause, | ||
setVideoPause, | ||
doPlay, | ||
doPause, | ||
savePosition, | ||
mediaPaused, | ||
mediaPosition, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was also thinking it should be its own component. Maybe as a separate issue along with displaying a paused video instead of the image wrapper when position has a value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. #825 can work as a reference as the media component has been separated there. Also I think that Video and VideoPlayer names are very specific, we use the same component to render all other types of media as well, so IMO the variables and components should be refactored to reflect the same(also in the PR). |
||
} = this.props; | ||
|
||
const isPlaying = playingUri === uri; | ||
|
@@ -93,6 +96,9 @@ class Video extends React.PureComponent { | |
} | ||
const poster = metadata.thumbnail; | ||
|
||
const mediaId = uri.split("#")[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as I said in a comment below, does this work for |
||
console.log("mediaId:", mediaId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove when ready to merge :) |
||
|
||
return ( | ||
<div | ||
className={klasses.join(" ")} | ||
|
@@ -112,8 +118,12 @@ class Video extends React.PureComponent { | |
downloadCompleted={fileInfo.completed} | ||
changeVolume={changeVolume} | ||
volume={volume} | ||
videoPause={videoPause} | ||
setVideoPause={setVideoPause} | ||
doPlay={doPlay} | ||
doPause={doPause} | ||
savePosition={savePosition} | ||
mediaId={mediaId} | ||
paused={mediaPaused} | ||
position={mediaPosition} | ||
/> | ||
))} | ||
{!isPlaying && ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @flow | ||
import * as actions from "constants/action_types"; | ||
import type { Action, Dispatch } from "redux/reducers/media"; | ||
import lbry from "lbry"; | ||
|
||
export const doPlay = () => (dispatch: Dispatch) => | ||
dispatch({ | ||
type: actions.MEDIA_PLAY, | ||
}); | ||
|
||
export const doPause = () => (dispatch: Dispatch) => | ||
dispatch({ | ||
type: actions.MEDIA_PAUSE, | ||
}); | ||
|
||
export const savePosition = (id: String, position: String) => ( | ||
dispatch: Dispatch | ||
) => | ||
dispatch({ | ||
type: actions.MEDIA_POSITION, | ||
id, | ||
position, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// @flow | ||
import * as actions from "constants/action_types"; | ||
import { handleActions } from "util/redux-utils"; | ||
|
||
export type MediaState = { | ||
paused: Boolean, | ||
positions: Object, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you define a type more specifically than |
||
}; | ||
|
||
export type Action = any; | ||
export type Dispatch = (action: Action) => any; | ||
|
||
const defaultState = { paused: true, positions: {} }; | ||
|
||
export default handleActions( | ||
{ | ||
[actions.MEDIA_PLAY]: (state: MediaState, action: Action) => ({ | ||
...state, | ||
paused: false, | ||
}), | ||
|
||
[actions.MEDIA_PAUSE]: (state: MediaState, action: Action) => ({ | ||
...state, | ||
paused: true, | ||
}), | ||
|
||
[actions.MEDIA_POSITION]: (state: MediaState, action: Action) => { | ||
const { id, position } = action; | ||
return { | ||
...state, | ||
positions: { | ||
...state.positions, | ||
[id]: position, | ||
}, | ||
}; | ||
}, | ||
}, | ||
defaultState | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as settings from "constants/settings"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that |
||
import { createSelector } from "reselect"; | ||
|
||
const _selectState = state => state.media || {}; | ||
|
||
export const selectMediaPaused = createSelector( | ||
_selectState, | ||
state => state.paused | ||
); | ||
|
||
export const makeSelectMediaPositionForUri = uri => | ||
createSelector(_selectState, state => { | ||
const id = uri.split("#")[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this work when the uri does not contain a |
||
return state.positions[id] || null; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import walletReducer from "redux/reducers/wallet"; | |
import shapeShiftReducer from "redux/reducers/shape_shift"; | ||
import subscriptionsReducer from "redux/reducers/subscriptions"; | ||
import videoReducer from "redux/reducers/video"; | ||
import mediaReducer from "redux/reducers/media"; | ||
import { persistStore, autoRehydrate } from "redux-persist"; | ||
import createCompressor from "redux-persist-transform-compress"; | ||
import createFilter from "redux-persist-transform-filter"; | ||
|
@@ -71,6 +72,7 @@ const reducers = combineReducers({ | |
shapeShift: shapeShiftReducer, | ||
subscriptions: subscriptionsReducer, | ||
video: videoReducer, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we still need |
||
media: mediaReducer, | ||
}); | ||
|
||
const bulkThunk = createBulkThunkMiddleware(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove this comment when ready to merge :)