Using Gstreamer with Python2.6 in Windows XP – Pt. 4 Incorporating it all into wxPython

(Original post from 01. Nov. 2011)

Hi folks, back again for the next round. This time all I will do, is just putting Gstreamer and wxPython together. Nothing fancy at all. This post is based on the GStreamer Tutorial with the difference that I will only use the playbin instead of the pipeline, simply because I could not find a suitable mp3 decoder available for Windows at the time of writing this. I am sure there are suitable ones out there, but my measly skills couldn’t make them work. I could have tried the python ctypes library to get the winLame DLLs to work, but it just didn’t seem worth the hassle with mp3play available…

Anyhow, so essentially I just inserted the code from the previous post into a simply GUI with wxPython. Anyone who knows wxPython and read the previous post, will not see anything new. So here is the code and we’ll talk about it afterwards.

NOTE: I used an XRC file to represent the GUI. This is personal preference and the files can be downloaded here.

#!usr/bin/python

import os
import urllib

import pygst
pygst.require("0.10")
import gst

import wx
from wx import xrc

class SimpleAudio(wx.App):
    """
    Class definition of the GUI front end and its functionality
    """

    def OnInit(self):
       
        # Load GUI info frm xrc file
        self.res = xrc.XmlResource('simpleAudio.xrc')
        self.init_frame()
        return True

    def init_frame(self):
       
        self.frame = self.res.LoadFrame(None, 'simple_audio')
        self.panel = xrc.XRCCTRL(self.frame, "main_panel")
       
        self.init_panel()
       
        self.frame.Show()
       
    def init_panel(self):
       
        # Gstreamer player
        self.player = gst.element_factory_make("playbin2", "player")
        fakesink = gst.element_factory_make("fakesink", "fakesink")
        self.player.set_property("video-sink", fakesink)
       
        self.path_txt = xrc.XRCCTRL(self.panel, "path_txt")
        self.play_btn = xrc.XRCCTRL(self.panel, "play_btn")
        self.open_btn = xrc.XRCCTRL(self.panel, "open_btn")
       
        self.panel.Bind(wx.EVT_BUTTON, self.on_play, id=xrc.XRCID('play_btn'))
        self.panel.Bind(wx.EVT_BUTTON, self.on_open, id=xrc.XRCID('open_btn'))
   
    def on_play(self, event):
        if self.play_btn.GetLabel() == "Play":
            filepath = self.path_txt.GetValue()
            path = urllib.pathname2url(filepath)
           
            if os.path.isfile(filepath):
                self.play_btn.SetLabel("Stop")
                self.player.set_property("uri", "file:" + path)
                self.player.set_state(gst.STATE_PLAYING)
        else:
            self.player.set_state(gst.STATE_NULL)
            self.play_btn.SetLabel("Play")
           
    def on_open(self, event):
       
        dlg = wx.FileDialog(self.frame, message="Open an ogg or wav file",
                    wildcard = "OGG, WAV Files (.ogg;.wav;)|*.ogg;*.wav;",
                    defaultDir=os.getcwd(),
                    defaultFile="", style=wx.OPEN)
       
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.path_txt.SetValue(path)
                         
        dlg.Destroy()

if __name__ == '__main__':
    app = SimpleAudio(False)
    app.MainLoop()

So, as you can see, we’ve essentially transferred parts of the GStreamer syntax into the frame initialisation, so that it is ready.

        ...
        # Gstreamer player
        self.player = gst.element_factory_make("playbin2", "player")
        fakesink = gst.element_factory_make("fakesink", "fakesink")
        self.player.set_property("video-sink", fakesink)
        ...

This is a direct copy and paste job. The only thing that is a little step further is in the function “on_play”:

    ...
    def on_play(self, event):
        if self.play_btn.GetLabel() == "Play":
            filepath = self.path_txt.GetValue()
            path = urllib.pathname2url(filepath)
           
            if os.path.isfile(filepath):
                self.play_btn.SetLabel("Stop")
                self.player.set_property("uri", "file:" + path)
                self.player.set_state(gst.STATE_PLAYING)
        else:
            self.player.set_state(gst.STATE_NULL)
            self.play_btn.SetLabel("Play")
    ... 

here you will find the second part of the code from the last post with the difference that we now also give the option of “stopping” the song. Yes it may seem outlandish to want to stop a song, but I incorporated it anyway for all those non-conventional types πŸ˜‰
Summing the code up: We get the path from the text control, check if the path is valid, convert to a url format and play it. That’s it really. No big deal.
Well, if I feel like it, I will try and expand on this player and include a song progress bar and a playlist, just to show you more of it, but we’ll see. Not sure yet, to be honest.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s