so://phie

verbal

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

blob: 1c8983cb429b036e20cfea2b9bd73a021bd3a127 (7847 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: '- No song title available -',

14 interval: null,

15 resume: false

16 };

17 },

18 components: {

19 VButton,

20 VIcon

21 },

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/favorite`(this.station.id);

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 setVolume(event) {

58 this.$refs.player.volume = event.target.value;

59 },

60 onPlay() {

61 if(this.resume) {

62 this.playing = false;

63 this.loading = true;

64 this.$refs.player.load();

65 }

66 },

67 onPause() {

68 this.playing = false;

69 this.resume = true;

70 },

71 onLoad() {

72 this.loading = false;

73 this.playing = true;

74 this.resume = false;

75 this.updateSong();

76 },

77 onError(event) {

78 this.error = true;

79 this.loading = false;

80 this.playing = false;

81 },

82 updateMediaSession() {

83 if('mediaSession' in navigator) {

84 navigator.mediaSession.metadata = new MediaMetadata({

85 title: this.title,

86 artist: this.station.name,

87 artwork: [

88 { src: this.station.icon, sizes: '96x96', type: 'image/jpg' },

89 { src: this.station.icon, sizes: '128x128', type: 'image/jpg' },

90 { src: this.station.icon, sizes: '192x192', type: 'image/jpg' },

91 { src: this.station.icon, sizes: '256x256', type: 'image/jpg' },

92 { src: this.station.icon, sizes: '384x384', type: 'image/jpg' },

93 { src: this.station.icon, sizes: '512x512', type: 'image/jpg' }

94 ]

95 });

96 }

97 },

98 async fetchSong() {

99 try {

100 const song = await http`get::/api/song`(this.station);

101 this.title = song === '' ? '- No song title available -' : song;

102 }

103 catch(err) {

104 console.error(err);

105 this.title = '- No song title available -';

106 }

107 this.updateMediaSession();

108 },

109 updateSong() {

110 this.fetchSong();

111 clearInterval(this.interval);

112 this.interval = setInterval(async () => {

113 if(!this.playing) {

114 clearInterval(this.interval);

115 return;

116 }

117 await this.fetchSong();

118 }, 10000);

119 }

120 },

121 template: `

122 <div class="sticky top-[98px] z-30 -mb-2">

123 <div

124 v-if="station"

125 class="flex flex-col bg-black pb-6"

126 >

127 <audio

128 ref="player"

129 controls autoplay

130 :src="station.url"

131 @loadeddata="onLoad"

132 @play="onPlay"

133 @pause="onPause"

134 @error="onError"

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

136 >

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

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

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

140 </audio>

141 <div class="p-5 bg-zinc-800 rounded-xl flex gap-5 items-center relative overflow-hidden shadow-2xl z-30 flex-col sm:flex-row">

142 <img

143 v-if="station.icon"

144 :src="station.icon"

145 :alt="station.name"

146 :class="{ 'animate-pulse': playing }"

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

148 >

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

150 <img

151 v-if="station.icon"

152 :src="station.icon"

153 :alt="station.name"

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

155 >

156 <v-icon

157 v-else

158 id="access-point"

159 size="38px"

160 class="text-gray-400"

161 ></v-icon>

162 </div>

163 <div

164 class="flex flex-col flex-1 z-10 min-w-0"

165 >

166 <p class="text-xl font-semibold text-white pb-1 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none">

167 {{station.name}}

168 </p>

169 <p class="text-md text-gray-400 overflow-hidden text-ellipsis whitespace-nowrap select-none pointer-events-none">

170 {{title ?? $lang('player.loading')}}

171 </p>

172 </div>

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

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

175 <v-button

176 :icon="[ station.is_favorite ? 'bookmark-off' : 'bookmark' ]"

177 :title="[ $lang(station.is_favorite ? 'global.unlike' : 'global.like') ]"

178 :active="station.is_favorite"

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

180 @click="setLike(!station.is_favorite)"

181 ></v-button>

182 <v-button

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

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

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

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

187 @click="setPlaying(!playing)"

188 ></v-button>

189 </div>

190 </div>

191 </div>

192 <div class="bg-gradient-to-b from-black h-2 sm:h-4"></div>

193 </div>

194 `

195}