2 Copyright 2007-2008 Vincent Carmona
4 This file is part of ZiK.
6 ZiK is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 ZiK is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with ZiK; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 class GstError < Exception
24 attr_accessor :current
27 def initialize(list,preference)
29 @current=preference['current']
32 @shuffle=preference['shuffle']
36 @repeat=preference['repeat']
38 for i in 0..@playlist.length-1
42 @order=@order.sort_by{rand}
45 @playbin=Gst::ElementFactory.make('playbin')
48 if !@playlist[@current].nil?
49 @playbin.uri='file://'+@playlist[@current]
51 @playbin.bus.add_watch { | message | got_bus_message(message) }
62 def got_bus_message(message)
64 when Gst::Message::MessageType::ERROR
65 $stderr.puts 'Gst error'
67 raise GstError.new if @radio
68 when Gst::Message::MessageType::EOS
71 true #Without this line, only the first message is processed
75 played=@order[0..@current]
77 not_played=@order[@current+1..length-1]
81 for i in length..@playlist.length-1
85 not_played=not_played.sort_by{rand}
87 @order=played+not_played
103 case sel <=> @order[@current]
109 when nil then#@order[@current] returns nil if playing the last song.
111 next_s if sel==@order[@current]+1#if deleting the last song
122 def toggle_shuffle#Jouer ou pas les chansons déjà jouées?
125 @order=@order.sort_by{rand}
126 temp=@order.index(@current)
128 @current=0 if @current.nil?
130 @current=@order[@current]
131 @current=0 if @current.nil?
144 @current=@order.index(n)
147 @playbin.uri='file://'+name
149 puts "Playing... #{name}"
151 $stderr.puts "Error playing file://#{name}"
155 def play_radio(radio)
157 @radio=true;@playing=true
161 puts "Playing... #{radio}"
162 #puts @playbin.stream_info
164 $stderr.puts "Cannot play #{radio}"
180 if @current < @playlist.length-1
190 puts 'End of playlist.'
204 @current=@order.length-1
207 puts 'Begin of playlist.'
219 if @playbin.volume>0.9
226 if @playbin.volume<0.1
234 @playbin.query_position[1]/1000000000
238 @playbin.seek(1.0, Gst::Format::Type::TIME,Gst::Seek::FLAG_FLUSH.to_i | Gst::Seek::FLAG_KEY_UNIT.to_i,Gst::Seek::TYPE_SET,
239 pos*1000000000,Gst::Seek::TYPE_NONE,-1)#Code from the Gst binding examples (cf:video-player)