~voluntatefaber/beat-box/bug952329

« back to all changes in this revision

Viewing changes to src/StreamPlayer.vala

  • Committer: Scott Ringwelski
  • Date: 2011-02-10 21:30:53 UTC
  • Revision ID: sgringwe@mtu.edu-20110210213053-d3c7mnexeref3cwj
sexy icons, sexy waf

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using Gst;
 
2
using Gtk;
 
3
using Gee;
 
4
 
 
5
public class BeatBox.StreamPlayer : GLib.Object {
 
6
        MainLoop loop;
 
7
    dynamic Element play;
 
8
    Gst.Bus bus;
 
9
    Song current;
 
10
    
 
11
    /** signals **/
 
12
    public signal void end_of_stream(Song s);
 
13
    public signal void current_position_update(int64 position);
 
14
    
 
15
    public StreamPlayer(string[] args) {
 
16
                Gst.init(ref args);
 
17
                play = ElementFactory.make ("playbin", "play");
 
18
                bus = play.get_bus();
 
19
                bus.add_watch(bus_callback);
 
20
                
 
21
                loop = new MainLoop();
 
22
                var time = new TimeoutSource(500);
 
23
 
 
24
                time.set_callback(() => {
 
25
                        int64 position = 0;
 
26
                        Gst.Format fmt = Gst.Format.TIME;
 
27
                        play.query_position(ref fmt, out position);
 
28
                        current_position_update(position);
 
29
                        return true;
 
30
                });
 
31
 
 
32
                time.attach(loop.get_context());
 
33
        }
 
34
 
 
35
    private bool bus_callback (Gst.Bus bus, Gst.Message message) {
 
36
                switch (message.type) {
 
37
        case Gst.MessageType.ERROR:
 
38
            GLib.Error err;
 
39
            string debug;
 
40
            message.parse_error (out err, out debug);
 
41
            stdout.printf ("Error: %s\n", err.message);
 
42
            break;
 
43
        case Gst.MessageType.EOS:
 
44
                        end_of_stream(current);
 
45
            break;
 
46
        default:
 
47
            break;
 
48
        }
 
49
 
 
50
        return true;
 
51
    }
 
52
        
 
53
    public void play_song (Song s) {
 
54
                if(s.file.length > 2) {// play a new file
 
55
                        current = s;
 
56
                        play.uri = "file://" + s.file;
 
57
                        play.set_state(State.READY);
 
58
                        play_stream();
 
59
                }
 
60
                
 
61
        play.set_state(State.PLAYING);
 
62
    }
 
63
    
 
64
    public void play_stream() {
 
65
                play.set_state(State.PLAYING);
 
66
        }
 
67
    
 
68
    public void pause_stream() {
 
69
                play.set_state(State.PAUSED);
 
70
        }
 
71
        
 
72
        public void seek_position(int64 position) {
 
73
                if(play.current_state != State.PLAYING)
 
74
                        play.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, position);
 
75
                else
 
76
                        play.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, position);
 
77
        }
 
78
}