2017-07-02 10:00:22 +02:00
|
|
|
const { remote } = require("electron");
|
2017-06-07 07:36:12 +02:00
|
|
|
import React from "react";
|
|
|
|
import { Thumbnail } from "component/common";
|
|
|
|
import player from "render-media";
|
|
|
|
import fs from "fs";
|
2017-07-02 17:35:30 +02:00
|
|
|
import { setSession, getSession } from "utils";
|
2017-06-10 19:07:28 +02:00
|
|
|
import LoadingScreen from "./loading-screen";
|
2017-06-07 07:36:12 +02:00
|
|
|
|
2017-06-09 01:41:44 +02:00
|
|
|
class VideoPlayer extends React.PureComponent {
|
2017-06-10 19:07:28 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
hasMetadata: false,
|
|
|
|
startedPlaying: false,
|
|
|
|
unplayable: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-07 07:36:12 +02:00
|
|
|
componentDidMount() {
|
2017-06-10 19:07:28 +02:00
|
|
|
const container = this.refs.media;
|
|
|
|
const { mediaType } = this.props;
|
|
|
|
const loadedMetadata = e => {
|
|
|
|
this.setState({ hasMetadata: true, startedPlaying: true });
|
|
|
|
this.refs.media.children[0].play();
|
|
|
|
};
|
|
|
|
const renderMediaCallback = err => {
|
|
|
|
if (err) this.setState({ unplayable: true });
|
|
|
|
};
|
2017-07-02 10:00:22 +02:00
|
|
|
// Handle fullscreen change for the Windows platform
|
|
|
|
const win32FullScreenChange = e => {
|
|
|
|
const win = remote.BrowserWindow.getFocusedWindow();
|
|
|
|
if ("win32" === process.platform) {
|
|
|
|
win.setMenu(
|
|
|
|
document.webkitIsFullScreen ? null : remote.Menu.getApplicationMenu()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2017-06-10 19:07:28 +02:00
|
|
|
|
|
|
|
player.append(
|
|
|
|
this.file(),
|
|
|
|
container,
|
|
|
|
{ autoplay: false, controls: true },
|
|
|
|
renderMediaCallback.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
const mediaElement = this.refs.media.children[0];
|
|
|
|
if (mediaElement) {
|
|
|
|
mediaElement.addEventListener(
|
|
|
|
"loadedmetadata",
|
|
|
|
loadedMetadata.bind(this),
|
|
|
|
{
|
|
|
|
once: true,
|
|
|
|
}
|
|
|
|
);
|
2017-07-01 23:38:09 +02:00
|
|
|
|
2017-07-02 10:00:22 +02:00
|
|
|
mediaElement.addEventListener(
|
|
|
|
"webkitfullscreenchange",
|
|
|
|
win32FullScreenChange.bind(this)
|
|
|
|
);
|
2017-07-01 23:38:09 +02:00
|
|
|
mediaElement.addEventListener("volumechange", () => {
|
2017-07-02 17:35:30 +02:00
|
|
|
setSession("prefs_volume", mediaElement.volume);
|
2017-07-01 23:38:09 +02:00
|
|
|
});
|
|
|
|
mediaElement.volume = this.getPreferredVolume();
|
2017-06-10 19:07:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 23:38:09 +02:00
|
|
|
getPreferredVolume() {
|
2017-07-02 17:35:30 +02:00
|
|
|
const volumePreference = parseFloat(getSession("prefs_volume"));
|
2017-07-01 23:38:09 +02:00
|
|
|
return isNaN(volumePreference) ? 1 : volumePreference;
|
|
|
|
}
|
|
|
|
|
2017-06-10 19:07:28 +02:00
|
|
|
componentDidUpdate() {
|
|
|
|
const { mediaType, downloadCompleted } = this.props;
|
|
|
|
const { startedPlaying } = this.state;
|
|
|
|
|
|
|
|
if (this.playableType() && !startedPlaying && downloadCompleted) {
|
|
|
|
const container = this.refs.media.children[0];
|
|
|
|
|
|
|
|
player.render(this.file(), container, { autoplay: true, controls: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file() {
|
2017-06-07 07:36:12 +02:00
|
|
|
const { downloadPath, filename } = this.props;
|
2017-06-10 19:07:28 +02:00
|
|
|
|
|
|
|
return {
|
2017-06-07 07:36:12 +02:00
|
|
|
name: filename,
|
|
|
|
createReadStream: opts => {
|
|
|
|
return fs.createReadStream(downloadPath, opts);
|
|
|
|
},
|
|
|
|
};
|
2017-06-10 19:07:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
playableType() {
|
|
|
|
const { mediaType } = this.props;
|
|
|
|
|
|
|
|
return ["audio", "video"].indexOf(mediaType) !== -1;
|
2017-06-07 07:36:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-06-10 19:07:28 +02:00
|
|
|
const { mediaType, poster } = this.props;
|
|
|
|
const { hasMetadata, unplayable } = this.state;
|
|
|
|
const noMetadataMessage = "Waiting for metadata.";
|
|
|
|
const unplayableMessage = "Sorry, looks like we can't play this file.";
|
|
|
|
|
|
|
|
const needsMetadata = this.playableType();
|
2017-06-07 07:36:12 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{["audio", "application"].indexOf(mediaType) !== -1 &&
|
2017-06-10 19:07:28 +02:00
|
|
|
(!this.playableType() || hasMetadata) &&
|
|
|
|
!unplayable &&
|
2017-06-07 07:36:12 +02:00
|
|
|
<Thumbnail src={poster} className="video-embedded" />}
|
2017-06-10 19:07:28 +02:00
|
|
|
{this.playableType() &&
|
|
|
|
!hasMetadata &&
|
|
|
|
!unplayable &&
|
|
|
|
<LoadingScreen status={noMetadataMessage} />}
|
|
|
|
{unplayable &&
|
|
|
|
<LoadingScreen status={unplayableMessage} spinner={false} />}
|
2017-06-07 07:36:12 +02:00
|
|
|
<div ref="media" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default VideoPlayer;
|