File: audio-player.vala

package info (click to toggle)
gnome-devel-docs 3.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 46,300 kB
  • ctags: 630
  • sloc: xml: 2,321; ansic: 2,040; python: 1,807; makefile: 747; sh: 504; cpp: 131
file content (92 lines) | stat: -rw-r--r-- 2,246 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Compile using:
 * valac --pkg gtk+-2.0 --pkg gstreamer-0.10 audio-player.vala
 *
 */

using Gtk;
using Gst;

public class AudioPlayer : Gtk.Window
{
  private Gst.Element playbin;
  private string[] files;
  private Gtk.Label label;
  private int song = 0;
  private bool playing;
  private Gtk.Button play_pause_button;

  public AudioPlayer ()
  {
    this.set_size_request (600, 200);
    this.destroy.connect (Gtk.main_quit);
    var vbox = new Gtk.VBox (false, 0);
    this.label = new Gtk.Label ("Audio Player");
    vbox.pack_start (this.label, true, true, 0);

    this.play_pause_button = new Gtk.Button.from_stock (Gtk.STOCK_MEDIA_PLAY);
    this.play_pause_button.clicked.connect (on_play_pause);
    var next_button = new Gtk.Button.from_stock (Gtk.STOCK_MEDIA_NEXT);
    next_button.clicked.connect (on_next);

    var button_box = new Gtk.HButtonBox ();
    button_box.add (this.play_pause_button);
    button_box.add (next_button);
    vbox.pack_start (button_box, false, true, 0);

    this.add (vbox);

    this.playbin = Gst.ElementFactory.make ("playbin", "play");
  }

  private void on_play_pause ()
  {
    if (this.playing)
    {
      this.playbin.set_state (Gst.State.PAUSED);
      this.play_pause_button.set_label (Gtk.STOCK_MEDIA_PAUSE);
    }
    else
    {
      this.playbin.set_state (Gst.State.PLAYING);
      this.play_pause_button.set_label (Gtk.STOCK_MEDIA_PLAY);
    }

    this.playing = !this.playing;
  }

  private void on_next ()
  {
    this.playbin.set_state (Gst.State.NULL);
    this.playbin.set ("uri", "file://" + this.files[song]);
    this.label.set_text (this.files[song]);
    this.song++;
    if (this.files.length <= this.song)
    {
      this.song = 0;
    }
    this.playbin.set_state (Gst.State.PLAYING);
    this.play_pause_button.set_label (Gtk.STOCK_MEDIA_PLAY);
    this.playing = true;
  }

  public static int main (string[] args)
  {
    Gst.init (ref args);
    Gtk.init (ref args);

    if (args.length < 2)
    {
      print ("usage: audio-player [files...]\n");
      return 1;
    }

    var audio_player = new AudioPlayer ();
    audio_player.files = args[1:args.length];
    audio_player.show_all ();
    audio_player.on_next ();
    Gtk.main ();

    return 0;
  }
}