A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 9c8a224c5419dd873703cc9e6b292325bad2b24b (8691 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-[18vw] h-[18vw] max-w-[80px] max-h-[80px] bg-zinc-900 rounded-md bottom-5 right-4 z-40 cursor-pointer shadow-2xl overflow-hidden"139
@click="state.open = !state.open"140
>141
<img142
v-if="station.icon"143
:src="station.icon"144
:alt="station.name"145
class="w-full h-full object-contain select-none pointer-events-none"146
>147
<v-icon148
v-else149
id="access-point"150
size="38px"151
class="text-gray-400 pointer-events-none"152
></v-icon>153
</div>154
<div155
class="w-full flex flex-col bg-black fixed left-0 right-0 top-0 h-[100vh] px-4 transition-transform duration-300 ease-in-out"156
:style="transform"157
>158
<audio159
v-if="station"160
ref="player"161
controls autoplay162
:src="station.url"163
@loadeddata="onLoad"164
@play="onPlay"165
@pause="onPause"166
@error="onError"167
class="select-none pointer-events-none w-0 h-0 opacity-0"168
>169
<source :src="station.url" type="audio/mpeg">170
<source :src="station.url" type="audio/ogg">171
<source :src="station.url" type="audio/aac">172
</audio>173
<div class="w-full h-[98px] py-6 flex gap-4 justify-between">174
<v-button175
icon="chevron-down"176
@click="state.open = false"177
></v-button>178
<v-button179
icon="cast"180
></v-button>181
</div>182
<div v-if="station" class="flex gap-8 items-center relative flex-col sm:flex-row pt-12">183
<div class="w-[75vw] h-[75vw] min-w-[75vw] bg-zinc-900 rounded-lg overflow-hidden z-10">184
<img185
v-if="station.icon"186
:src="station.icon"187
:alt="station.name"188
class="w-full h-full object-contain select-none"189
>190
<v-icon191
v-else192
id="access-point"193
size="38px"194
class="text-gray-400"195
></v-icon>196
</div>197
<div198
class="flex flex-col flex-1 z-10 min-w-0 max-w-[75vw]"199
>200
<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">201
{{station.name}}202
</p>203
<p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center sm:text-left">204
{{title ?? $lang('player.no-song-title')}}205
</p>206
</div>207
<div class="inline-flex flex-col gap-6 z-10 pr-2 items-center">208
<v-button209
:icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"210
:title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"211
:animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"212
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10 !min-w-[64px] !min-h-[64px]"213
@click="setPlaying(!playing)"214
></v-button>215
<input216
type="range"217
min="0"218
max="1"219
step="0.05"220
@change="setVolume"221
class="w-[200px]"222
>223
</div>224
</div>225
</div>226
</div>227
`228
}