Merge pull request #320 from akinwale/issue253

Issue #253 mp3 audio seek fix
This commit is contained in:
Jeremy Kauffman 2017-07-11 16:46:36 -04:00 committed by GitHub
commit 52393145ec
3 changed files with 41 additions and 13 deletions

View file

@ -7,6 +7,8 @@ import { setSession, getSession } from "utils";
import LoadingScreen from "./loading-screen"; import LoadingScreen from "./loading-screen";
class VideoPlayer extends React.PureComponent { class VideoPlayer extends React.PureComponent {
static MP3_CONTENT_TYPES = ["audio/mpeg3", "audio/mpeg"];
constructor(props) { constructor(props) {
super(props); super(props);
@ -21,7 +23,7 @@ class VideoPlayer extends React.PureComponent {
componentDidMount() { componentDidMount() {
const container = this.refs.media; const container = this.refs.media;
const { mediaType } = this.props; const { contentType, downloadPath, mediaType } = this.props;
const loadedMetadata = e => { const loadedMetadata = e => {
this.setState({ hasMetadata: true, startedPlaying: true }); this.setState({ hasMetadata: true, startedPlaying: true });
this.refs.media.children[0].play(); this.refs.media.children[0].play();
@ -39,12 +41,17 @@ class VideoPlayer extends React.PureComponent {
} }
}; };
player.append( // use renderAudio override for mp3
this.file(), if (VideoPlayer.MP3_CONTENT_TYPES.indexOf(contentType) > -1) {
container, this.renderAudio(container, null, false);
{ autoplay: false, controls: true, enableKeyboard: true }, } else {
renderMediaCallback.bind(this) player.append(
); this.file(),
container,
{ autoplay: false, controls: true },
renderMediaCallback.bind(this)
);
}
document.addEventListener("keydown", this.togglePlayListener); document.addEventListener("keydown", this.togglePlayListener);
const mediaElement = this.refs.media.children[0]; const mediaElement = this.refs.media.children[0];
@ -76,6 +83,20 @@ class VideoPlayer extends React.PureComponent {
} }
} }
renderAudio(container, autoplay) {
if (container.firstChild) {
container.firstChild.remove();
}
// clear the container
const { downloadPath } = this.props;
const audio = document.createElement("audio");
audio.autoplay = autoplay;
audio.controls = true;
audio.src = downloadPath;
container.appendChild(audio);
}
togglePlay(event) { togglePlay(event) {
// ignore all events except click and spacebar keydown, or input events in a form control // ignore all events except click and spacebar keydown, or input events in a form control
if ( if (
@ -101,13 +122,20 @@ class VideoPlayer extends React.PureComponent {
} }
componentDidUpdate() { componentDidUpdate() {
const { mediaType, downloadCompleted } = this.props; const { contentType, downloadCompleted } = this.props;
const { startedPlaying } = this.state; const { startedPlaying } = this.state;
if (this.playableType() && !startedPlaying && downloadCompleted) { if (this.playableType() && !startedPlaying && downloadCompleted) {
const container = this.refs.media.children[0]; const container = this.refs.media.children[0];
player.render(this.file(), container, { autoplay: true, controls: true }); if (VideoPlayer.MP3_CONTENT_TYPES.indexOf(contentType) > -1) {
this.renderAudio(this.refs.media, true);
} else {
player.render(this.file(), container, {
autoplay: true,
controls: true,
});
}
} }
} }

View file

@ -98,6 +98,7 @@ class Video extends React.PureComponent {
poster={poster} poster={poster}
downloadPath={fileInfo.download_path} downloadPath={fileInfo.download_path}
mediaType={mediaType} mediaType={mediaType}
contentType={contentType}
downloadCompleted={fileInfo.completed} downloadCompleted={fileInfo.completed}
/>)} />)}
{!isPlaying && {!isPlaying &&

View file

@ -268,10 +268,9 @@ lbry.getClientSettings = function() {
var outSettings = {}; var outSettings = {};
for (let setting of Object.keys(lbry.defaultClientSettings)) { for (let setting of Object.keys(lbry.defaultClientSettings)) {
var localStorageVal = localStorage.getItem("setting_" + setting); var localStorageVal = localStorage.getItem("setting_" + setting);
outSettings[setting] = outSettings[setting] = localStorageVal === null
localStorageVal === null ? lbry.defaultClientSettings[setting]
? lbry.defaultClientSettings[setting] : JSON.parse(localStorageVal);
: JSON.parse(localStorageVal);
} }
return outSettings; return outSettings;
}; };