so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: 7330fa041be852d836422a81363a8470e528a886 (8161 bytes)[doc][blame][raw]

1import {state} from '../main.js';

2import {http, $lang} from '../utils.js';

3import VButton from '../element/button.js';

4import VIcon from '../element/icon.js';

5

6export 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 state

17 };

18 },

19 components: {

20 VButton,

21 VIcon

22 },

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 <div

130 @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 <div

136 v-if="station"

137 ref="button"

138 class="fixed w-[64px] h-[64px] bg-zinc-900 rounded-md bottom-5 right-4 z-40 cursor-pointer shadow-xl overflow-hidden"

139 @click="state.open = !state.open"

140 >

141 <img

142 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-icon

148 v-else

149 id="access-point"

150 size="38px"

151 class="text-gray-400 pointer-events-none"

152 ></v-icon>

153 </div>

154 <div

155 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"

156 :style="transform"

157 >

158 <div class="w-[40px] h-[8px] bg-zinc-500 rounded-[4px] mx-auto mb-12"></div>

159 <audio

160 v-if="station"

161 ref="player"

162 controls autoplay

163 :src="station.url"

164 @loadeddata="onLoad"

165 @play="onPlay"

166 @pause="onPause"

167 @error="onError"

168 class="select-none pointer-events-none w-0 h-0 opacity-0"

169 >

170 <source :src="station.url" type="audio/mpeg">

171 <source :src="station.url" type="audio/ogg">

172 <source :src="station.url" type="audio/aac">

173 </audio>

174 <div v-if="station" class="flex gap-5 items-center relative flex-col sm:flex-row">

175 <div class="w-[140px] h-[140px] min-w-[140px] bg-zinc-900 rounded-lg overflow-hidden z-10">

176 <img

177 v-if="station.icon"

178 :src="station.icon"

179 :alt="station.name"

180 class="w-full h-full object-contain select-none"

181 >

182 <v-icon

183 v-else

184 id="access-point"

185 size="38px"

186 class="text-gray-400"

187 ></v-icon>

188 </div>

189 <div

190 class="flex flex-col flex-1 z-10 min-w-0 max-w-full"

191 >

192 <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">

193 {{station.name}}

194 </p>

195 <p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none text-center sm:text-left">

196 {{title ?? $lang('player.no-song-title')}}

197 </p>

198 </div>

199 <div class="inline-flex gap-4 z-10 pr-2">

200 <input type="range" min="0" max="1" step="0.05" @change="setVolume">

201 <v-button

202 :icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"

203 :title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"

204 :animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"

205 class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10"

206 @click="setPlaying(!playing)"

207 ></v-button>

208 </div>

209 </div>

210 </div>

211 </div>

212 `

213}