Skip to content
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

feat(utils/player): 播放、暫停時淡入淡出 #1414

Merged
merged 2 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions src/utils/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { isAccountLoggedIn } from '@/utils/auth';
import { trackUpdateNowPlaying, trackScrobble } from '@/api/lastfm';
import { isCreateMpris, isCreateTray } from '@/utils/platform';

const PLAY_PAUSE_FADE_DURATION = 200;

const electron =
process.env.IS_ELECTRON === true ? window.require('electron') : null;
const ipcRenderer =
Expand Down Expand Up @@ -649,28 +651,38 @@ export default class {
}

pause() {
this._howler?.pause();
this._setPlaying(false);
setTitle(null);
this._pauseDiscordPresence(this._currentTrack);
this._howler?.fade(this.volume, 0, PLAY_PAUSE_FADE_DURATION);

this._howler?.once('fade', () => {
this._howler?.pause();
this._setPlaying(false);
setTitle(null);
this._pauseDiscordPresence(this._currentTrack);
});
}
play() {
if (this._howler?.playing()) return;

this._howler?.play();
this._setPlaying(true);
if (this._currentTrack.name) {
setTitle(this._currentTrack);
}
this._playDiscordPresence(this._currentTrack, this.seek());
if (store.state.lastfm.key !== undefined) {
trackUpdateNowPlaying({
artist: this.currentTrack.ar[0].name,
track: this.currentTrack.name,
album: this.currentTrack.al.name,
trackNumber: this.currentTrack.no,
duration: ~~(this.currentTrack.dt / 1000),
});
}

this._howler?.once('play', () => {
this._howler?.fade(0, this.volume, PLAY_PAUSE_FADE_DURATION);

this._setPlaying(true);
if (this._currentTrack.name) {
setTitle(this._currentTrack);
}
this._playDiscordPresence(this._currentTrack, this.seek());
if (store.state.lastfm.key !== undefined) {
trackUpdateNowPlaying({
artist: this.currentTrack.ar[0].name,
track: this.currentTrack.name,
album: this.currentTrack.al.name,
trackNumber: this.currentTrack.no,
duration: ~~(this.currentTrack.dt / 1000),
});
}
});
}
playOrPause() {
if (this._howler?.playing()) {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export const isMac = process.platform === 'darwin';
export const isLinux = process.platform === 'linux';
export const isDevelopment = process.env.NODE_ENV === 'development';

export const isCreateTray = isWindows || isLinux || isDevelopment;
export const isCreateTray =
process.env.IS_ELECTRON && (isWindows || isLinux || isDevelopment);
export const isCreateMpris = isLinux;