so://phie

verbal

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

blob: 382dc02fb1f78d88101bfb3bd4f283eb5a75de24 (7327 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 };

16 },

17 components: {

18 VButton,

19 VIcon

20 },

21 watch: {

22 station: function(station) {

23 this.playing = false;

24 this.error = false;

25 if(station !== null) {

26 this.loading = true;

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

28 this.updateMediaSession();

29 }

30 }

31 },

32 methods: {

33 $lang,

34 async setLike(is_favorite) {

35 try {

36 await http`${is_favorite ? 'post' : 'delete'}::/api/like`(this.station.uuid);

37 this.station.is_favorite = is_favorite;

38 state.app.$refs.favorites.doFetch();

39 }

40 catch(error) {

41 console.log(error);

42 }

43 },

44 setPlaying(playing, event = false) {

45 if(this.error) {

46 return;

47 }

48 this.playing = playing;

49 this.$refs.player[this.playing ? 'play' : 'pause']();

50 if(this.playing) {

51 this.error = false;

52 if(!event) {

53 this.updateSong();

54 }

55 }

56 },

57 onLoad() {

58 this.loading = false;

59 this.playing = true;

60 this.updateSong();

61 },

62 onError(event) {

63 this.error = true;

64 this.loading = false;

65 this.playing = false;

66 },

67 updateMediaSession() {

68 if('mediaSession' in navigator) {

69 navigator.mediaSession.metadata = new MediaMetadata({

70 title: this.title,

71 artist: this.station.name,

72 artwork: [

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

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

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

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

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

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

79 ]

80 });

81 }

82 },

83 async fetchSong() {

84 try {

85 const song = await http`get::/api/song`({ url: this.station.stream_url });

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

87 }

88 catch(err) {

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

90 }

91 this.updateMediaSession();

92 },

93 updateSong() {

94 this.fetchSong();

95 clearInterval(this.interval);

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

97 if(!this.playing) {

98 clearInterval(this.interval);

99 return;

100 }

101 await this.fetchSong();

102 }, 10000);

103 }

104 },

105 template: `

106 <div class="sticky top-[98px] z-30">

107 <div

108 v-if="station"

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

110 >

111 <audio

112 ref="player"

113 controls autoplay

114 :src="station.stream_url"

115 @loadeddata="onLoad"

116 @play="setPlaying(true, true)"

117 @pause="setPlaying(false, true)"

118 @error="onError"

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

120 >

121 <source :src="station.stream_url" type="audio/mpeg">

122 <source :src="station.stream_url" type="audio/ogg">

123 <source :src="station.stream_url" type="audio/aac">

124 </audio>

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

126 <img

127 v-if="station.favicon"

128 :src="station.favicon"

129 :alt="station.name"

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

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

132 >

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

134 <img

135 v-if="station.favicon"

136 :src="station.favicon"

137 :alt="station.name"

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

139 >

140 <v-icon

141 v-else

142 id="access-point"

143 size="65%"

144 class="bg-gray-400"

145 ></v-icon>

146 </div>

147 <div

148 class="flex flex-col flex-1 z-10 min-w-0 cursor-pointer"

149 >

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

151 {{station.name}}

152 </p>

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

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

155 </p>

156 </div>

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

158 <v-button

159 :icon="[ station.is_favorite ? 'heart-solid' : 'heart' ]"

160 :title="$lang('global.favorites')"

161 :active="station.is_favorite"

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

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

164 ></v-button>

165 <v-button

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

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

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

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

170 @click="setPlaying(!playing)"

171 ></v-button>

172 </div>

173 </div>

174 </div>

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

176 </div>

177 `

178}