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

Add Music Cache Support #1192

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,8 @@
"_PROTOCOL": "Protocol",
"_HOST": "Host",
"_PORT": "Port",
"ZOOM_IN_OUT": "Zoom In/Out"
"ZOOM_IN_OUT": "Zoom In/Out",
"_MUSIC_CACHE_DIR": "Music Cache Directory",
"_ENABLE_MUSIC_CACHE": "Enable Music Cache",
"_ENABLE_MUSIC_CACHE_ONLY_MODE": "Enable Music Cache Only Mode (Turning volume off recommended)"
}
5 changes: 4 additions & 1 deletion i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,8 @@
"_PROTOCOL": "代理协议",
"_HOST": "主机地址",
"_PORT": "端口",
"ZOOM_IN_OUT": "放大/缩小"
"ZOOM_IN_OUT": "放大/缩小",
"_MUSIC_CACHE_DIR": "音乐缓存目录",
"_ENABLE_MUSIC_CACHE": "是否启用音乐缓存",
"_ENABLE_MUSIC_CACHE_ONLY_MODE": "是否启用仅音乐缓存模式(建议关闭音量)"
}
4 changes: 2 additions & 2 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const main = () => {
link(scope, element, attrs) {
element.bind('click', (event) => {
if (isElectron()) {
const { shell } = require('electron');
const { shell } = require('electron'); // eslint-disable-line
shell.openExternal(scope.url);
} else {
$window.open(scope.url, '_blank');
Expand All @@ -236,7 +236,7 @@ const main = () => {
link(scope, element, attrs) {
element.bind('click', (event) => {
if (isElectron()) {
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
ipcRenderer.send('control', scope.action);
}
});
Expand Down
2 changes: 1 addition & 1 deletion js/controller/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ angular.module('listenone').controller('AuthController', [
$scope.openLogin = (source) => {
const url = $scope.getLoginUrl(source);
if (isElectron()) {
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
return ipcRenderer.send('openUrl', url);
}
return window.open(url, '_blank');
Expand Down
2 changes: 1 addition & 1 deletion js/controller/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ angular.module('listenone').controller('NavigationController', [

$scope.addLocalMusic = (list_id) => {
if (isElectron()) {
const { remote } = require('electron');
const { remote } = require('electron'); // eslint-disable-line
const remoteFunctions = remote.require('./functions.js');
remote.dialog
.showOpenDialog({
Expand Down
80 changes: 72 additions & 8 deletions js/controller/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,33 @@ angular.module('listenone').controller('PlayController', [
'enable_auto_choose_source',
true
);
$scope.enableMusicCache = getLocalStorageValue(
'enable_music_cache',
false
);
$scope.enableMusicCacheOnlyMode = getLocalStorageValue(
'enable_music_cache_only_mode',
false
);

if (isElectron()) {
// const { remote } = require('electron');
// const { join } = require("path");
const fs = require('fs');
$scope.musicCacheDir = getLocalStorageValue(
'music_cache_dir',
// join(remote.app.getPath('home'), ".listen1", "music_cache")
require('path').join(
require('os').homedir(),
'.listen1',
'music_cache'
)
);
if ($scope.enableMusicCache && !fs.existsSync($scope.musicCacheDir)) {
fs.mkdirSync($scope.musicCacheDir, { recursive: true });
}
}

$scope.autoChooseSourceList = getLocalStorageValue(
'auto_choose_source_list',
['kuwo', 'qq', 'migu']
Expand Down Expand Up @@ -214,7 +241,7 @@ angular.module('listenone').controller('PlayController', [
$scope.enableGlobalShortCut
);

const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
ipcRenderer.send('control', message);
};

Expand All @@ -235,7 +262,7 @@ angular.module('listenone').controller('PlayController', [
'enable_lyric_floating_window',
$scope.enableLyricFloatingWindow
);
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
ipcRenderer.send(
'control',
message,
Expand All @@ -244,7 +271,7 @@ angular.module('listenone').controller('PlayController', [
};

if (isElectron()) {
const { webFrame, ipcRenderer } = require('electron');
const { webFrame, ipcRenderer } = require('electron'); // eslint-disable-line
// webFrame.setVisualZoomLevelLimits(1, 3);
ipcRenderer.on('setZoomLevel', (event, level) => {
webFrame.setZoomLevel(level);
Expand Down Expand Up @@ -301,7 +328,7 @@ angular.module('listenone').controller('PlayController', [
'float_window_setting',
$scope.floatWindowSetting
);
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
const message = 'update_lyric_floating_window_css';
ipcRenderer.send(
'control',
Expand Down Expand Up @@ -559,7 +586,7 @@ angular.module('listenone').controller('PlayController', [
$scope.lyricLineNumberTrans = lastObjectTrans.lineNumber;
}
if (isElectron()) {
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
const currentLyric =
$scope.lyricArray[lastObject.lineNumber].content;
let currentLyricTrans = '';
Expand Down Expand Up @@ -674,7 +701,7 @@ angular.module('listenone').controller('PlayController', [
});
$scope.lastTrackId = msg.data.currentPlaying.id;
if (isElectron()) {
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
ipcRenderer.send('currentLyric', track.title);
ipcRenderer.send('trackPlayingNow', track);
}
Expand Down Expand Up @@ -728,7 +755,7 @@ angular.module('listenone').controller('PlayController', [

$rootScope.document_title = title;
if (isElectron()) {
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
if (msg.data.isPlaying) {
ipcRenderer.send('isPlaying', true);
} else {
Expand Down Expand Up @@ -832,7 +859,8 @@ angular.module('listenone').controller('PlayController', [
};

if (isElectron()) {
require('electron').ipcRenderer.on('globalShortcut', (event, message) => {
const { ipcRenderer } = require('electron'); // eslint-disable-line
ipcRenderer.on('globalShortcut', (event, message) => {
if (message === 'right') {
l1Player.next();
} else if (message === 'left') {
Expand All @@ -853,6 +881,42 @@ angular.module('listenone').controller('PlayController', [
);
};

$scope.setMusicCacheOnlyMode = (toggle) => {
if (toggle === true) {
$scope.enableMusicCacheOnlyMode = !$scope.enableMusicCacheOnlyMode;
}
localStorage.setObject(
'enable_music_cache_only_mode',
$scope.enableMusicCacheOnlyMode
);
};

$scope.setMusicCache = (toggle) => {
if (toggle === true) {
$scope.enableMusicCache = !$scope.enableMusicCache;
}
localStorage.setObject('enable_music_cache', $scope.enableMusicCache);
const fs = require('fs');
if (!fs.existsSync($scope.musicCacheDir)) {
fs.mkdirSync($scope.musicCacheDir, { recursive: true });
}
};

$scope.setMusicCacheDir = () => {
const { remote } = require('electron'); // eslint-disable-line
const dir = remote.dialog.showOpenDialogSync({
properties: ['openDirectory'],
defaultPath: $scope.musicCacheDir,
});

if (dir === null) {
notyf.warning('请选择音乐缓存目录');
return;
}
$scope.musicCacheDir = dir[0]; // eslint-disable-line
localStorage.setObject('music_cache_dir', $scope.musicCacheDir);
};

$scope.enableSource = (source) => {
if ($scope.autoChooseSourceList.indexOf(source) > -1) {
return;
Expand Down
6 changes: 3 additions & 3 deletions js/controller/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ angular.module('listenone').controller('ProfileController', [
$scope.proxyRules = `${$scope.proxyProtocol}://${host}:${port}`;
if (isElectron()) {
const message = 'update_proxy_config';
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
if (mode === 'system' || mode === 'direct') {
ipcRenderer.send('control', message, { mode });
} else {
Expand All @@ -60,7 +60,7 @@ angular.module('listenone').controller('ProfileController', [
if (isElectron()) {
// get proxy config from main process
const message = 'get_proxy_config';
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line
ipcRenderer.send('control', message);
}
};
Expand All @@ -75,7 +75,7 @@ angular.module('listenone').controller('ProfileController', [
};

if (isElectron()) {
const { ipcRenderer } = require('electron');
const { ipcRenderer } = require('electron'); // eslint-disable-line

ipcRenderer.on('proxyConfig', (event, config) => {
// parse config
Expand Down
7 changes: 5 additions & 2 deletions js/loweb.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,10 @@ const MediaService = {
},

bootstrapTrack(track, playerSuccessCallback, playerFailCallback) {
const successCallback = playerSuccessCallback;
const customCallback = (bootinfo) => {
playerSuccessCallback(bootinfo);
};
// const successCallback = playerSuccessCallback;
const sound = {};
function failureCallback() {
if (localStorage.getObject('enable_auto_choose_source') === false) {
Expand Down Expand Up @@ -399,7 +402,7 @@ const MediaService = {

const provider = getProviderByName(track.source);

provider.bootstrap_track(track, successCallback, failureCallback);
provider.bootstrap_track(track, customCallback, failureCallback);
},

login(source, options) {
Expand Down
31 changes: 29 additions & 2 deletions js/player_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
return this.currentAudio && this.currentAudio.howl;
}

set currentHowl(howl) {
if (this.currentAudio && this.currentAudio.howl) {
this.currentAudio.howl = howl;
}
}

get playing() {
return this.currentHowl ? this.currentHowl.playing() : false;
}
Expand Down Expand Up @@ -223,13 +229,35 @@
msg.type = 'BG_PLAYER:RETRIEVE_URL_SUCCESS';

msg.data = { ...msg.data, ...bootinfo };

this.playlist[index].bitrate = bootinfo.bitrate;
this.playlist[index].platform = bootinfo.platform;

this.setMediaURI(msg.data.url, msg.data.id);
this.setAudioDisabled(false, msg.data.index);
this.finishLoad(msg.data.index, playNow);

if (
// eslint-disable-next-line no-undef
process.platform === 'darwin' &&
localStorage.getObject('enable_music_cache', false)
) {
const { ipcRenderer } = require('electron'); // eslint-disable-line
ipcRenderer.send('downloadMusic', {
data: {
title: msg.data.title,
artist: msg.data.artist,
album: msg.data.album,
img_url: msg.data.img_url,
url: msg.data.url,
},
musicCacheDir: localStorage.getObject('music_cache_dir'),
cacheOnlyMode: localStorage.getObject(
'enable_music_cache_only_mode',
false
),
});
}

playerSendMessage(this.mode, msg);
},
() => {
Expand Down Expand Up @@ -265,7 +293,6 @@

finishLoad(index, playNow) {
const data = this.playlist[index];

// If we already loaded this track, use the current one.
// Otherwise, setup and load a new Howl.
const self = this;
Expand Down
59 changes: 59 additions & 0 deletions listen1.html
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,65 @@ <h2>{{ backup.id }} {{backup.description}}</h2>
{{_MODIFY}}
</button>
</div>
<div class="settings-title" ng-if="isMac">
<span>{{_MUSIC_CACHE_DIR}}</span>
</div>
<div class="settings-content" ng-if="isMac">
<div
class="shortcut"
class="btn btn-primary confirm-button"
>
<svg
class="feather"
ng-show="!enableMusicCache"
ng-click="setMusicCache(true)"
>
<use href="#square"></use>
</svg>
<svg
class="feather"
ng-show="enableMusicCache"
ng-click="setMusicCache(true)"
>
<use href="#check-square"></use>
</svg>
{{_ENABLE_MUSIC_CACHE}}
</div>
<div
ng-show="enableMusicCache"
>
<span></span>
<p>{{_MUSIC_CACHE_DIR}}: {{musicCacheDir}}</p>
<label class="upload-button"
for="music-cache-dir-selector"
ng-click="setMusicCacheDir()">
{{_MODIFY}}
</label>
<span></span>
<div
class="shortcut"
ng-if="isMac"
class="btn btn-primary confirm-button"
>
<svg
class="feather"
ng-show="!enableMusicCacheOnlyMode"
ng-click="setMusicCacheOnlyMode(true)"
>
<use href="#square"></use>
</svg>
<svg
class="feather"
ng-show="enableMusicCacheOnlyMode"
ng-click="setMusicCacheOnlyMode(true)"
>
<use href="#check-square"></use>
</svg>
{{_ENABLE_MUSIC_CACHE_ONLY_MODE}}
</div>
</div>

</div>
<div class="settings-title">
<span>{{_ABOUT}}</span>
</div>
Expand Down
Loading