Skip to content

Commit c2a8d6b

Browse files
author
Aaron Gotwalt
committed
Playback reset sorta working
1 parent d6984fe commit c2a8d6b

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

lib/ghosty/cli.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,18 @@ def start
1010

1111
port = 3100
1212
assets_directory = 'assets'
13-
13+
base_uri = "http://#{IPSocket.getaddress(Socket.gethostname)}:#{port}"
1414

1515
web = Ghosty::Web.new(assets_directory, port)
1616
web.async.start
1717

18-
19-
base_uri = "http://#{IPSocket.getaddress(Socket.gethostname)}:#{port}"
20-
21-
22-
18+
# Wait for the web to start
2319
sleep 2
2420

25-
2621
player = Ghosty::Player.new(base_uri, assets_directory)
22+
player.async.perform
2723

28-
puts 'Playing'
29-
player.perform
30-
24+
# Final sleep is to keep things alive
3125
sleep
3226
end
3327

lib/ghosty/player.rb

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'sonos'
22
require 'celluloid'
33
require 'addressable/uri'
4+
require 'cgi'
45

56
module Ghosty
67
class Player
@@ -12,14 +13,40 @@ def initialize(base_uri, file_path)
1213
@system = Sonos::System.new
1314
end
1415

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
1643
@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'
1845
end.compact.sample
1946
end
2047

2148
# Finds a file to play and returns it as a URI
22-
def select_file
49+
def ghost_url
2350
file = Dir.glob(File.join(@path, '*.mp3')).sample
2451

2552
if file
@@ -29,13 +56,5 @@ def select_file
2956
end
3057
end
3158

32-
def perform
33-
speaker = select_speaker
34-
file = select_file
35-
36-
speaker.play(file)
37-
speaker.play
38-
end
39-
4059
end
4160
end

0 commit comments

Comments
 (0)