diff --git a/lib/soundcli/player.rb b/lib/soundcli/player.rb index 7b7429c..d83759f 100644 --- a/lib/soundcli/player.rb +++ b/lib/soundcli/player.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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? @@ -139,11 +123,11 @@ 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 @@ -151,14 +135,14 @@ def handle_bus_message(msg) 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