Skip to content
This repository was archived by the owner on Mar 17, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions lib/soundcli/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

class Player
def initialize
# create the playbin
@playbin = Gst::ElementFactory.make("playbin2")
# create the bin
@playbin = Gst.parse_launch("playbin")
#watch the bus for messages
bus = @playbin.bus
bus.add_watch do |bus, message|
@playbin.bus.add_watch do |bus, message|
handle_bus_message(message)
end
end
Expand All @@ -17,8 +16,7 @@ def set(uri, comments)
@comments = comments
@comment_ptr = 0

@playbin.set_property("buffer-size", Settings::all['buffer-size'])
@playbin.set_property("uri",uri)
@playbin.uri = uri
end

protected
Expand All @@ -38,26 +36,14 @@ def ns_to_str(ns)

# get position of the playbin
def position
begin
@query_position = Gst::QueryPosition.new(Gst::Format::TIME)
@playbin.query(@query_position)
pos = @query_position
rescue
pos = 0
end
result, pos = @playbin.query_position(Gst::Format::TIME)
return pos
end

# get song duration
def duration
begin
@query_duration = Gst::QueryDuration.new(Gst::Format::TIME)
@playbin.query(@query_duration)
pos = @query_duration
rescue
pos = 0
end
return pos
result, dur = @playbin.query_duration(Gst::Format::TIME)
return dur
end

public
Expand All @@ -76,12 +62,12 @@ def play
@playbin.play

GLib::Timeout.add(100) do
@duration = self.ns_to_str(self.duration.parse[1])
@position = self.ns_to_str(self.position.parse[1])
timestamp = self.position.parse[1]/1000000
duration = self.ns_to_str(self.duration)
position = self.ns_to_str(self.position)
timestamp = self.position/1000000

if self.playing?
Helpers::say("#{@position}/#{@duration} \r", :normal)
Helpers::say("#{position}/#{duration} \r", :normal)
$stdout.flush
end

Expand Down Expand Up @@ -116,20 +102,18 @@ def play
end

def resume
@playbin.set_state(Gst::State::PLAYING)
@playbin.play
end

def pause
@playbin.set_state(Gst::State::PAUSED)
@playbin.pause
Helpers::say("--- PAUSED ---\r", :normal)
end

def handle_bus_message(msg)
case msg.type
when Gst::Message::Type::BUFFERING
buffer = msg.parse
when Gst::MessageType::BUFFERING
buffer = msg.parse_buffering
if buffer < 100
Helpers::say("Buffering: #{buffer}% \r", :normal)
self.pause if self.playing?
Expand All @@ -139,26 +123,26 @@ def handle_bus_message(msg)
end

$stdout.flush
when Gst::Message::Type::ERROR
when Gst::MessageType::ERROR
@playbin.set_state(Gst::State::NULL)
$stderr.puts msg.parse
$stderr.puts msg.parse_error
self.quit
when Gst::Message::Type::EOS
when Gst::MessageType::EOS
@playbin.set_state(Gst::State::NULL)
self.quit
end
true
end

def done?
return (@playbin.get_state[1] == Gst::State::NULL)
return (@playbin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::NULL)
end

def playing?
return (@playbin.get_state[1] == Gst::State::PLAYING)
return (@playbin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::PLAYING)
end

def paused?
return (@playbin.get_state[1] == Gst::State::PAUSED)
return (@playbin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::PAUSED)
end
end