Okay so I'm having an issue with seeking through videos in my react components with Rails Webpacker. I can make them play but I can't seek through them.
I'm using Rails Active Storage to upload the videos then sending their urls to my react component via an html attribute rendered by rails_blob_path(@post.video) (see below snippet on step 9). In my react component I have a <video /> element with the source being that parsed attribute. From there I have methods that control the element via a React.createRef(). One of the methods (play()) works as expected. However, my seek() method does not and I don't understand why.
I made a minified example (repo) to isolate the problem and here are the following steps I took:
rails new [app] --webpacker=react- cd into [app],
rails active_storage:install rails g scaffold post title:stringrails db:migrate- add line
has_one_attached :videotoapp/models/post.rb - add
:videoto white-listed params inapp/controllers/posts_controller.rb - add below snippet as a form field in
app/views/posts/_form.html.erb
<div class="field">
<%= form.label :video %>
<%= form.file_field :video %>
</div>
- add below to
app/views/posts/show.html.erb
<div id='payload' url='<%= rails_blob_path(@post.video).to_json %>'></div>
<div id='hello-react'></div>
<%= javascript_pack_tag 'hello_react' %>
- change
app/javascript/packs/hello_react.jsxto have:
import React from 'react'
import ReactDOM from 'react-dom'
const node = document.getElementById('payload')
const url = JSON.parse(node.getAttribute('url'))
class App extends React.Component {
componentWillMount() {
this.videoRef = React.createRef()
}
seek = (seekTo = 0) => {
this.videoRef.current.currentTime = seekTo
}
play = () => {
this.videoRef.current.play()
}
render() {
return (
<div>
<video ref={this.videoRef} controls>
<source src={url} type='video/mp4' />
</video>
<button onClick={ (e) => {this.seek(5)} }>+5</button>
<button onClick={ (e) => {this.play()} }>Play</button>
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('hello-react')
)
- start the server with
rails s, go tolocalhost:3000/posts/new - create a post (you'll need a video to upload), you'll be redirected on submitting to the
hello-reactpack.
I've posted this before but have not gotten an answer. This is the first time I've made a separate project and outline of the steps to isolate the issue. Hopefully that relays how desperate I am. Please let me know if I need to explain any steps or if you all need anymore info. Thank you all in advance for your help.
Edit 11/11/18: One answer has pointed out that it is a browser issue. I was using Chrome for all my testing but when I used FireFox it worked as expected. Still not sure how to fix this so that the app works cross-browser.