A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 934816fcb54a607fcf6061626e2aee7bbca891cf (7678 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: '- No song title available -',14
interval: null,15
resume: false16
};17
},18
components: {19
VButton,20
VIcon21
},22
watch: {23
station: function(station) {24
this.playing = false;25
this.error = false;26
if(station !== null) {27
this.loading = true;28
this.title = '- No song title available -';29
this.updateMediaSession();30
}31
}32
},33
methods: {34
$lang,35
async setLike(is_favorite) {36
try {37
await http`${is_favorite ? 'post' : 'delete'}::/api/like`(this.station.uuid);38
this.station.is_favorite = is_favorite;39
state.app.$refs.favorites.doFetch();40
}41
catch(error) {42
console.log(error);43
}44
},45
setPlaying(playing) {46
if(this.error || this.loading) {47
return;48
}49
this.playing = playing;50
this.$refs.player[this.playing ? 'play' : 'pause']();51
if(this.playing) {52
this.playing = false;53
this.loading = true;54
this.$refs.player.load();55
}56
},57
onPlay() {58
if(this.resume) {59
this.playing = false;60
this.loading = true;61
this.$refs.player.load();62
}63
},64
onPause() {65
this.playing = false;66
this.resume = true;67
},68
onLoad() {69
this.loading = false;70
this.playing = true;71
this.resume = false;72
this.updateSong();73
},74
onError(event) {75
this.error = true;76
this.loading = false;77
this.playing = false;78
},79
updateMediaSession() {80
if('mediaSession' in navigator) {81
navigator.mediaSession.metadata = new MediaMetadata({82
title: this.title,83
artist: this.station.name,84
artwork: [85
{ src: this.station.favicon, sizes: '96x96', type: 'image/jpg' },86
{ src: this.station.favicon, sizes: '128x128', type: 'image/jpg' },87
{ src: this.station.favicon, sizes: '192x192', type: 'image/jpg' },88
{ src: this.station.favicon, sizes: '256x256', type: 'image/jpg' },89
{ src: this.station.favicon, sizes: '384x384', type: 'image/jpg' },90
{ src: this.station.favicon, sizes: '512x512', type: 'image/jpg' }91
]92
});93
}94
},95
async fetchSong() {96
try {97
const song = await http`get::/api/song`({ url: this.station.stream_url });98
this.title = song === '' ? '- No song title available -' : song;99
}100
catch(err) {101
this.title = '- No song title available -';102
}103
this.updateMediaSession();104
},105
updateSong() {106
this.fetchSong();107
clearInterval(this.interval);108
this.interval = setInterval(async () => {109
if(!this.playing) {110
clearInterval(this.interval);111
return;112
}113
await this.fetchSong();114
}, 10000);115
}116
},117
template: `118
<div class="sticky top-[98px] z-30">119
<div120
v-if="station"121
class="flex flex-col bg-black pb-6"122
>123
<audio124
ref="player"125
controls autoplay126
:src="station.stream_url"127
@loadeddata="onLoad"128
@play="onPlay"129
@pause="onPause"130
@error="onError"131
class="select-none pointer-events-none w-0 h-0 opacity-0"132
>133
<source :src="station.stream_url" type="audio/mpeg">134
<source :src="station.stream_url" type="audio/ogg">135
<source :src="station.stream_url" type="audio/aac">136
</audio>137
<div class="p-5 bg-zinc-800 rounded-xl flex gap-5 items-center relative overflow-hidden shadow-2xl z-30">138
<img139
v-if="station.favicon"140
:src="station.favicon"141
:alt="station.name"142
:class="{ 'animate-pulse': playing }"143
class="w-full h-full object-cover absolute top-0 left-0 right-0 bottom-0 blur-2xl opacity-30 z-0 pointer-events-none select-none"144
>145
<div class="w-[64px] h-[64px] min-w-[64px] bg-zinc-900 rounded-lg overflow-hidden z-10">146
<img147
v-if="station.favicon"148
:src="station.favicon"149
:alt="station.name"150
class="w-full h-full object-contain select-none"151
>152
<v-icon153
v-else154
id="access-point"155
size="65%"156
class="bg-gray-400"157
></v-icon>158
</div>159
<div160
class="flex flex-col flex-1 z-10 min-w-0 cursor-pointer"161
>162
<p class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none">163
{{station.name}}164
</p>165
<p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none">166
{{title ?? $lang('player.loading')}}167
</p>168
</div>169
<div class="inline-flex gap-4 z-10 pr-2">170
<v-button171
:icon="[ station.is_favorite ? 'heart-solid' : 'heart' ]"172
:title="[ $lang(station.is_favorite ? 'global.unlike' : 'global.like') ]"173
:active="station.is_favorite"174
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10"175
@click="setLike(!station.is_favorite)"176
></v-button>177
<v-button178
:icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"179
:title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"180
:animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"181
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10"182
@click="setPlaying(!playing)"183
></v-button>184
</div>185
</div>186
</div>187
<div class="bg-gradient-to-b from-black h-2 sm:h-4"></div>188
</div>189
`190
}