File tree Expand file tree Collapse file tree 2 files changed +34
-21
lines changed Expand file tree Collapse file tree 2 files changed +34
-21
lines changed Original file line number Diff line number Diff line change @@ -10,24 +10,18 @@ def start
10
10
11
11
port = 3100
12
12
assets_directory = 'assets'
13
-
13
+ base_uri = "http:// #{ IPSocket . getaddress ( Socket . gethostname ) } : #{ port } "
14
14
15
15
web = Ghosty ::Web . new ( assets_directory , port )
16
16
web . async . start
17
17
18
-
19
- base_uri = "http://#{ IPSocket . getaddress ( Socket . gethostname ) } :#{ port } "
20
-
21
-
22
-
18
+ # Wait for the web to start
23
19
sleep 2
24
20
25
-
26
21
player = Ghosty ::Player . new ( base_uri , assets_directory )
22
+ player . async . perform
27
23
28
- puts 'Playing'
29
- player . perform
30
-
24
+ # Final sleep is to keep things alive
31
25
sleep
32
26
end
33
27
Original file line number Diff line number Diff line change 1
1
require 'sonos'
2
2
require 'celluloid'
3
3
require 'addressable/uri'
4
+ require 'cgi'
4
5
5
6
module Ghosty
6
7
class Player
@@ -12,14 +13,40 @@ def initialize(base_uri, file_path)
12
13
@system = Sonos ::System . new
13
14
end
14
15
15
- def select_speaker
16
+ def perform
17
+ speaker = random_speaker
18
+ file = ghost_url
19
+
20
+ previous = speaker . now_playing
21
+ previous_uri = Addressable ::URI . parse ( previous [ :uri ] )
22
+ previous_uri . query = nil
23
+
24
+ puts "Playing #{ file } on #{ speaker . name } "
25
+ speaker . play ( file )
26
+ track_info = speaker . now_playing
27
+ speaker . play
28
+
29
+ # Determine the track length, add a few extra seconds for padding
30
+ duration = Time . parse ( track_info [ :track_duration ] ) . sec + 2
31
+ puts "Sleeping for #{ duration } s"
32
+ sleep 0 # duration
33
+
34
+ puts "Resetting speaker source to #{ previous_uri } "
35
+ # reset the current track
36
+ speaker . play ( previous_uri . to_s )
37
+ speaker . play
38
+ end
39
+
40
+ private
41
+
42
+ def random_speaker
16
43
@system . speakers . map do |speaker |
17
- speaker if speaker . name == 'Office' # get_player_state[:state] == 'PAUSED_PLAYBACK'
44
+ speaker # speaker.get_player_state[:state] == 'PAUSED_PLAYBACK'
18
45
end . compact . sample
19
46
end
20
47
21
48
# Finds a file to play and returns it as a URI
22
- def select_file
49
+ def ghost_url
23
50
file = Dir . glob ( File . join ( @path , '*.mp3' ) ) . sample
24
51
25
52
if file
@@ -29,13 +56,5 @@ def select_file
29
56
end
30
57
end
31
58
32
- def perform
33
- speaker = select_speaker
34
- file = select_file
35
-
36
- speaker . play ( file )
37
- speaker . play
38
- end
39
-
40
59
end
41
60
end
You can’t perform that action at this time.
0 commit comments