vlcwidget.py 2.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#! /usr/bin/python

"""VLC Widget classes.

This module provides two helper classes, to ease the embedding of a
VLC component inside a pygtk application.

VLCWidget is a simple VLC widget.

DecoratedVLCWidget provides simple player controls.
11 12

$Id$
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
"""

import gtk
import sys
import vlc

from gettext import gettext as _

class VLCWidget(gtk.DrawingArea):
    """Simple VLC widget.

    Its player can be controlled through the 'player' attribute, which
    is a MediaControl instance.
    """
    def __init__(self, *p):
        gtk.DrawingArea.__init__(self)
        self.player=vlc.MediaControl(*p)
        def handle_embed(*p):
            if sys.platform == 'win32':
                xidattr='handle'
            else:
                xidattr='xid'
            self.player.set_visual(getattr(self.window, xidattr))
            return True
        self.connect("map-event", handle_embed)
        self.set_size_request(320, 200)


class DecoratedVLCWidget(gtk.VBox):
    """Decorated VLC widget.

    VLC widget decorated with a player control toolbar.

    Its player can be controlled through the 'player' attribute, which
    is a MediaControl instance.    
    """
    def __init__(self, *p):
        gtk.VBox.__init__(self)
        self._vlc_widget=VLCWidget(*p)
        self.player=self._vlc_widget.player
        self.pack_start(self._vlc_widget, expand=True)
        self._toolbar = self.get_player_control_toolbar()
        self.pack_start(self._toolbar, expand=False)

    def get_player_control_toolbar(self):
        """Return a player control toolbar
        """
        tb=gtk.Toolbar()
        tb.set_style(gtk.TOOLBAR_ICONS) 

        def on_play(b):
            self.player.start(0)
            return True

        def on_stop(b):
            self.player.stop(0)
            return True

        def on_pause(b):
            self.player.pause(0)
            return True

        tb_list = (
            (_("Play"), _("Play"), gtk.STOCK_MEDIA_PLAY,
             on_play),
            (_("Pause"), _("Pause"), gtk.STOCK_MEDIA_PAUSE,
             on_pause),
            (_("Stop"), _("Stop"), gtk.STOCK_MEDIA_STOP, 
             on_stop),
            )

        for text, tooltip, stock, callback in tb_list:
            b=gtk.ToolButton(stock)
            b.connect("clicked", callback)
            tb.insert(b, -1)
        tb.show_all()
        return tb

class VideoPlayer:
    """Example video player.
    """
    def __init__(self):
        self.vlc = DecoratedVLCWidget()
        
    def main(self, fname):
        self.vlc.player.playlist_add_item(fname)
        self.popup()
        gtk.main()

    def popup(self):
        w=gtk.Window()
        w.add(self.vlc)
        w.show_all()
        w.connect("destroy", gtk.main_quit)
        return w

if __name__ == '__main__':
    if not sys.argv[1:]:
       print "You must provide a movie filename"
       sys.exit(1)
    p=VideoPlayer()
    p.main(sys.argv[1])