A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: fb0418a3f1d36866167fba0ed7bccb82a3f161e1 (7836 bytes)[doc][blame][raw]
1
import {state} from '../main.js';2
import {http, $lang} from '../utils.js';3
import VButton from '../element/button.js';4
import VIcon from '../element/icon.js';6
export default {7
props: ['station'],8
data() {9
return {10
playing: false,11
loading: false,12
error: false,13
title: null,14
interval: null,15
resume: false,16
state17
};18
},19
components: {20
VButton,21
VIcon22
},23
watch: {24
station: function(station) {25
this.playing = false;26
this.error = false;27
if(station !== null) {28
this.loading = true;29
this.title = null;30
this.updateMediaSession();31
state.open = true;32
}33
}34
},35
methods: {36
$lang,37
setPlaying(playing) {38
if(this.error || this.loading) {39
return;40
}41
this.playing = playing;42
this.$refs.player[this.playing ? 'play' : 'pause']();43
if(this.playing) {44
this.playing = false;45
this.loading = true;46
this.$refs.player.load();47
}48
},49
setVolume(event) {50
this.$refs.player.volume = event.target.value;51
},52
onPlay() {53
if(this.resume) {54
this.playing = false;55
this.loading = true;56
this.$refs.player.load();57
}58
},59
onPause() {60
this.playing = false;61
this.resume = true;62
},63
onLoad() {64
this.loading = false;65
this.playing = true;66
this.resume = false;67
this.updateSong();68
},69
onError() {70
this.error = true;71
this.loading = false;72
this.playing = false;73
},74
updateMediaSession() {75
if('mediaSession' in navigator) {76
navigator.mediaSession.metadata = new MediaMetadata({77
title: this.title ?? $lang('player.no-song-title'),78
artist: this.station.name,79
artwork: [80
{ src: this.station.icon, sizes: '96x96', type: 'image/jpg' },81
{ src: this.station.icon, sizes: '128x128', type: 'image/jpg' },82
{ src: this.station.icon, sizes: '192x192', type: 'image/jpg' },83
{ src: this.station.icon, sizes: '256x256', type: 'image/jpg' },84
{ src: this.station.icon, sizes: '384x384', type: 'image/jpg' },85
{ src: this.station.icon, sizes: '512x512', type: 'image/jpg' }86
]87
});88
}89
},90
async fetchSong() {91
try {92
const song = await http`get::/api/song`(this.station);93
this.title = song === '' ? null : song;94
}95
catch(err) {96
console.error(err);97
clearInterval(this.interval);98
this.title = null;99
}100
this.updateMediaSession();101
},102
updateSong() {103
this.fetchSong();104
clearInterval(this.interval);105
this.interval = setInterval(async () => {106
if(!this.playing) {107
clearInterval(this.interval);108
return;109
}110
await this.fetchSong();111
}, 10000);112
}113
},114
computed: {115
transform() {116
return {117
'transform': `translate(0, ${state.open ? 0 : 100}vh)`118
};119
},120
opacity() {121
return {122
'opacity': `${state.open ? 1 : 0}`,123
'pointer-events': `${state.open ? 'all' : 'none'}`124
};125
}126
},127
template: `128
<div class="z-30">129
<div130
@click="state.open = false"131
class="fixed top-0 bottom-0 left-0 right-0 bg-black/60 transition-opacity duration-300 ease-in-out"132
:style="opacity"133
>134
</div>135
<div136
v-if="station"137
ref="button"138
class="fixed w-[64px] h-[64px] bg-white rounded-full bottom-4 right-4 z-40 cursor-pointer shadow-xl"139
@click="state.open = !state.open"140
>141
<v-icon142
id="playlist"143
class="pointer-events-none"144
>145
</v-icon>146
</div>147
<div148
class="w-full flex flex-col bg-zinc-800 pb-6 rounded-t-2xl fixed left-0 right-0 bottom-0 px-4 pt-4 h-[70vh] transition-transform duration-300 ease-in-out"149
:style="transform"150
>151
<div class="w-[40px] h-[8px] bg-zinc-500 rounded-[4px] mx-auto mb-12"></div>152
<audio153
v-if="station"154
ref="player"155
controls autoplay156
:src="station.url"157
@loadeddata="onLoad"158
@play="onPlay"159
@pause="onPause"160
@error="onError"161
class="select-none pointer-events-none w-0 h-0 opacity-0"162
>163
<source :src="station.url" type="audio/mpeg">164
<source :src="station.url" type="audio/ogg">165
<source :src="station.url" type="audio/aac">166
</audio>167
<div v-if="station" class="flex gap-5 items-center relative flex-col sm:flex-row">168
<div class="w-[140px] h-[140px] min-w-[140px] bg-zinc-900 rounded-lg overflow-hidden z-10">169
<img170
v-if="station.icon"171
:src="station.icon"172
:alt="station.name"173
class="w-full h-full object-contain select-none"174
>175
<v-icon176
v-else177
id="access-point"178
size="38px"179
class="text-gray-400"180
></v-icon>181
</div>182
<div183
class="flex flex-col flex-1 z-10 min-w-0 max-w-full"184
>185
<p class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center sm:text-left">186
{{station.name}}187
</p>188
<p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center sm:text-left">189
{{title ?? $lang('player.no-song-title')}}190
</p>191
</div>192
<div class="inline-flex gap-4 z-10 pr-2">193
<input type="range" min="0" max="1" step="0.05" @change="setVolume">194
<v-button195
:icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"196
:title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"197
:animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"198
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10"199
@click="setPlaying(!playing)"200
></v-button>201
</div>202
</div>203
</div>204
</div>205
`206
}