Автообновление данных о треке в режиме трансляции

This commit is contained in:
Norbaev 2024-07-01 11:52:42 +05:00
parent 3f3086c141
commit 338a68d792
3 changed files with 40 additions and 25 deletions

View File

@ -64,10 +64,11 @@ export default {
}, },
watch: {}, watch: {},
created() { created() {
this.$store.dispatch('setCurrentPlay', { // Нахуя это здесь?
...this.currentPlay, // this.$store.dispatch('setCurrentPlay', {
isPlay: false, // ...this.currentPlay,
}); // isPlay: false,
// });
}, },
mounted() {}, mounted() {},
methods: {}, methods: {},

View File

@ -174,7 +174,9 @@ export default {
this.connectionPlayer(); this.connectionPlayer();
}, },
mounted() { mounted() {
// Создание <audio>
this.$store.dispatch('initPlayer', { volume: this.songVolume }); this.$store.dispatch('initPlayer', { volume: this.songVolume });
if (this.user?.id) { if (this.user?.id) {
this.checkSongIsFavorite(); this.checkSongIsFavorite();
this.getSongList(); this.getSongList();
@ -210,7 +212,7 @@ export default {
this.connection = new Player(); this.connection = new Player();
this.connection.init(); this.connection.init();
this.connection.onHandler(this.handlePlayer); this.connection.onHandler(this.handleConnection);
}, },
checkSongIsFavorite() { checkSongIsFavorite() {
app app
@ -233,7 +235,7 @@ export default {
console.error(err); console.error(err);
}); });
}, },
handlePlayer(e) { handleConnection(e) {
// console.log('getPlaying', e); // console.log('getPlaying', e);
const jsonData = JSON.parse(e.data); const jsonData = JSON.parse(e.data);
// console.log('jsonData', jsonData); // console.log('jsonData', jsonData);
@ -243,30 +245,42 @@ export default {
// Первый ответ // Первый ответ
jsonData?.connect?.subs?.['station:it-radio']?.publications?.[0]?.data; jsonData?.connect?.subs?.['station:it-radio']?.publications?.[0]?.data;
// console.log('getPlaying', data); // console.log('getPlaying', data);
console.log(data);
if (!data) return; if (!data) return;
if (this.currentPlay.live) { if (this.currentPlay.live) {
// Ссылка на трансляцию
const dataUrl = data.np.station.listen_url; const dataUrl = data.np.station.listen_url;
// Объект с данными музыки
const dataSong = data.np.now_playing.song;
console.log(dataSong);
const playerUrl = this.player.target.src; const playerUrl = this.player.target.src;
if (dataUrl !== playerUrl) { if (dataUrl !== playerUrl) {
// console.log('data.np.station.listen_url', dataUrl); // console.log('data.np.station.listen_url', dataUrl);
this.$store.dispatch('changePlayer', dataUrl); this.$store.dispatch('changePlayer', dataUrl);
// console.log(this.player); // console.log(this.player);
const currentPlay = { this.actionCurrentPlay(dataSong);
...this.currentPlay, }
// Инфа про текущий трек if (dataSong.id !== this.currentPlay.id) {
...data.np.now_playing.song, this.actionCurrentPlay(dataSong);
azura_id: data.np.now_playing.song.id,
isLoader: false,
live: true,
currentIndex: null,
};
// console.log('params in getPlaying ', params);
// delete params.unique_id;
this.$store.dispatch('setCurrentPlay', currentPlay);
} }
} }
}, },
actionCurrentPlay(song) {
// console.log('actionCurrentPlay', data);
const currentPlay = {
...this.currentPlay,
// Инфа про текущий трек
...song,
// azura_id: song.id,
isLoader: false,
live: true,
currentIndex: null,
};
// console.log('params in getPlaying ', params);
// delete params.unique_id;
this.$store.dispatch('setCurrentPlay', currentPlay);
},
updateProgress(e) { updateProgress(e) {
this.playerInfo = { this.playerInfo = {
...this.playerInfo, ...this.playerInfo,
@ -290,8 +304,8 @@ export default {
} }
}, },
handlerPlay() { handlerPlay() {
console.log(this.currentPlay); // console.log(this.currentPlay);
console.log(this.player); // console.log(this.player);
this.$store.dispatch('handlerPlayer', { play: true }); this.$store.dispatch('handlerPlayer', { play: true });
}, },
handlerPause() { handlerPause() {
@ -356,12 +370,12 @@ export default {
toggleVolume() { toggleVolume() {
if (this.songVolume === 0) { if (this.songVolume === 0) {
this.songVolume = this.preToggleVol; this.songVolume = this.preToggleVol;
console.log(this.songVolume); // console.log(this.songVolume);
this.$store.dispatch('handlerPlayer', { volume: this.songVolume }); this.$store.dispatch('handlerPlayer', { volume: this.songVolume });
} else { } else {
this.preToggleVol = this.songVolume; this.preToggleVol = this.songVolume;
this.songVolume = 0.0; this.songVolume = 0.0;
console.log(this.songVolume); // console.log(this.songVolume);
this.$store.dispatch('handlerPlayer', { volume: this.songVolume }); this.$store.dispatch('handlerPlayer', { volume: this.songVolume });
} }
}, },

View File

@ -64,7 +64,7 @@ export default createStore({
initPlayer(state, { volume }) { initPlayer(state, { volume }) {
// console.log(params); // console.log(params);
state.player.target = document.createElement('audio'); state.player.target = document.createElement('audio');
console.log(state.player.target); // console.log(state.player.target);
state.player.target.src = ''; state.player.target.src = '';
state.player.target.preload = 'auto'; state.player.target.preload = 'auto';
state.player.target.controls = true; state.player.target.controls = true;
@ -90,13 +90,13 @@ export default createStore({
state.player.target.addEventListener('canplaythrough', awaitPlay); state.player.target.addEventListener('canplaythrough', awaitPlay);
}, },
handlerPlayer(state, params) { handlerPlayer(state, params) {
console.log('hanlerPlayer', params); // console.log('hanlerPlayer', params);
if (params.pause) { if (params.pause) {
state.currentPlay.isPlay = false; state.currentPlay.isPlay = false;
state.player.target.pause(); state.player.target.pause();
} }
if (params.play) { if (params.play) {
console.log(state); // console.log(state);
if (state.player.target.readyState >= 3) { if (state.player.target.readyState >= 3) {
state.currentPlay.isPlay = true; state.currentPlay.isPlay = true;
state.player.target.play(); state.player.target.play();