Page 1 of 1

[SOLVED] Python VLC; Why cant it open and play this file?

Posted: 13 May 2011 14:25
by Kanonskall
Hi.

I'm trying to get a simple mp3 to play using vlc.py

I've tried copying the procedure from "examples_wxvlc.py" (which works and plays the mp3).

here is my program

Code: Select all

# -*- coding: utf-8 -*- import vlc , os class newPlayer(object): def __init__(self): self.Instance = vlc.Instance() self.player = self.Instance.media_player_new() def Open(self): dirname = "D:\\Music\\Div\\Test" filename = "music1.mp3" self.Media = self.Instance.media_new(unicode(os.path.join(dirname, filename))) self.player.set_media(self.Media) title = self.player.get_title() print title def Play(self): self.player.get_media() self.player.play() Spelar = newPlayer() Spelar.Open() Spelar.Play()

Code: Select all

Warning: input_item_SetURI("D:\Music\Div\Test\music1.mp3"): file path instead of URL. input_SplitMRL("D:\Music\Div\Test\music1.mp3"): not a valid URI!
What am i missing? :roll:

Re: Python VLC; Why cant it open and play this file?

Posted: 13 May 2011 14:27
by Jean-Baptiste Kempf
\\ ?

Re: Python VLC; Why cant it open and play this file?

Posted: 13 May 2011 14:37
by Kanonskall
\\ ?
Yes, escaping the \'s. But i've tried

Code: Select all

dirname = r"<the directory>"
also, but i get the same message. And i just can't find what "examples_wxvlc.py" is doing differently, since it's able to play it.

As far as i can see, all i'm skipping is the creation of the WX GUI.

Here is the example program i got:

Code: Select all

#! /usr/bin/python # -*- coding: utf-8 -*- # # WX example for VLC Python bindings # Copyright (C) 2009-2010 the VideoLAN team # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. # """ A simple example for VLC python bindings using wxPython. Author: Michele Orrù Date: 23-11-2010 """ # import external libraries import wx # 2.8 import vlc # import standard libraries import os import user class Player(wx.Frame): """The main window has to deal with events. """ def __init__(self, title): wx.Frame.__init__(self, None, -1, title, pos=wx.DefaultPosition, size=(550, 500)) # Menu Bar # File Menu self.frame_menubar = wx.MenuBar() self.file_menu = wx.Menu() self.file_menu.Append(1, "&Open", "Open from file..") self.file_menu.AppendSeparator() self.file_menu.Append(2, "&Close", "Quit") self.Bind(wx.EVT_MENU, self.OnOpen, id=1) self.Bind(wx.EVT_MENU, self.OnExit, id=2) self.frame_menubar.Append(self.file_menu, "File") self.SetMenuBar(self.frame_menubar) # Panels # The first panel holds the video and it's all black self.videopanel = wx.Panel(self, -1) self.videopanel.SetBackgroundColour(wx.BLACK) # The second panel holds controls ctrlpanel = wx.Panel(self, -1 ) self.timeslider = wx.Slider(ctrlpanel, -1, 0, 0, 1000) self.timeslider.SetRange(0, 1000) pause = wx.Button(ctrlpanel, label="Pause") play = wx.Button(ctrlpanel, label="Play") stop = wx.Button(ctrlpanel, label="Stop") volume = wx.Button(ctrlpanel, label="Volume") self.volslider = wx.Slider(ctrlpanel, -1, 0, 0, 100, size=(100, -1)) # Bind controls to events self.Bind(wx.EVT_BUTTON, self.OnPlay, play) self.Bind(wx.EVT_BUTTON, self.OnPause, pause) self.Bind(wx.EVT_BUTTON, self.OnStop, stop) self.Bind(wx.EVT_BUTTON, self.OnToggleVolume, volume) self.Bind(wx.EVT_SLIDER, self.OnSetVolume, self.volslider) # Give a pretty layout to the controls ctrlbox = wx.BoxSizer(wx.VERTICAL) box1 = wx.BoxSizer(wx.HORIZONTAL) box2 = wx.BoxSizer(wx.HORIZONTAL) # box1 contains the timeslider box1.Add(self.timeslider, 1) # box2 contains some buttons and the volume controls box2.Add(play, flag=wx.RIGHT, border=5) box2.Add(pause) box2.Add(stop) box2.Add((-1, -1), 1) box2.Add(volume) box2.Add(self.volslider, flag=wx.TOP | wx.LEFT, border=5) # Merge box1 and box2 to the ctrlsizer ctrlbox.Add(box1, flag=wx.EXPAND | wx.BOTTOM, border=10) ctrlbox.Add(box2, 1, wx.EXPAND) ctrlpanel.SetSizer(ctrlbox) # Put everything togheter sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.videopanel, 1, flag=wx.EXPAND) sizer.Add(ctrlpanel, flag=wx.EXPAND | wx.BOTTOM | wx.TOP, border=10) self.SetSizer(sizer) self.SetMinSize((350, 300)) # finally create the timer, which updates the timeslider self.timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) # VLC player controls self.Instance = vlc.Instance() self.player = self.Instance.media_player_new() def OnExit(self, evt): """Closes the window. """ self.Close() def OnOpen(self, evt): """Pop up a new dialow window to choose a file, then play the selected file. """ # if a file is already running, then stop it. self.OnStop(None) # Create a file dialog opened in the current home directory, where # you can display all kind of files, having as title "Choose a file". dlg = wx.FileDialog(self, "Choose a file", user.home, "", "*.*", wx.OPEN) if dlg.ShowModal() == wx.ID_OK: dirname = dlg.GetDirectory() print dirname filename = dlg.GetFilename() print filename # Creation self.Media = self.Instance.media_new(unicode(os.path.join(dirname, filename))) self.player.set_media(self.Media) # Report the title of the file chosen title = self.player.get_title() # if an error was encountred while retriving the title, then use # filename if title == -1: title = filename self.SetTitle("%s - wxVLCplayer" % title) # set the window id where to render VLC's video output self.player.set_xwindow(self.videopanel.GetHandle()) # FIXME: this should be made cross-platform self.OnPlay(None) # set the volume slider to the current volume self.volslider.SetValue(self.player.audio_get_volume() / 2) # finally destroy the dialog dlg.Destroy() def OnPlay(self, evt): """Toggle the status to Play/Pause. If no file is loaded, open the dialog window. """ # check if there is a file to play, otherwise open a # wx.FileDialog to select a file if not self.player.get_media(): self.OnOpen(None) else: # Try to launch the media, if this fails display an error message if self.player.play() == -1: self.errorDialog("Unable to play.") else: self.timer.Start() def OnPause(self, evt): """Pause the player. """ self.player.pause() def OnStop(self, evt): """Stop the player. """ self.player.stop() # reset the time slider self.timeslider.SetValue(0) self.timer.Stop() def OnTimer(self, evt): """Update the time slider according to the current movie time. """ # since the self.player.get_length can change while playing, # re-set the timeslider to the correct range. length = self.player.get_length() self.timeslider.SetRange(-1, length) # update the time on the slider time = self.player.get_time() self.timeslider.SetValue(time) def OnToggleVolume(self, evt): """Mute/Unmute according to the audio button. """ is_mute = self.player.audio_get_mute() self.player.audio_set_mute(not is_mute) # update the volume slider; # since vlc volume range is in [0, 200], # and our volume slider has range [0, 100], just divide by 2. self.volslider.SetValue(self.player.audio_get_volume() / 2) def OnSetVolume(self, evt): """Set the volume according to the volume sider. """ volume = self.volslider.GetValue() * 2 # vlc.MediaPlayer.audio_set_volume returns 0 if success, -1 otherwise if self.player.audio_set_volume(volume) == -1: self.errorDialog("Failed to set volume") def errorDialog(self, errormessage): """Display a simple error dialog. """ edialog = wx.MessageDialog(self, errormessage, 'Error', wx.OK| wx.ICON_ERROR) edialog.ShowModal() if __name__ == "__main__": # Create a wx.App(), which handles the windowing system event loop app = wx.PySimpleApp() # Create the window containing our small media player player = Player("Simple PyVLC Player") # show the player window centred and run the application player.Centre() player.Show() app.MainLoop()

Re: Python VLC; Why cant it open and play this file?

Posted: 13 May 2011 14:58
by Rémi Denis-Courmont
You are using the function to open an URL... and pass a file name. There should be a different function for file paths. I don't know the Python bindings though.

Re: Python VLC; Why cant it open and play this file?

Posted: 13 May 2011 15:15
by Kanonskall
Ok, i'll check the docs and see if i can find it. Thanks! :)

EDIT:
Got it working! :)
should have used .media_new_path()

Code: Select all

self.Media = self.Instance.media_new_path("./music1.mp3")
At first i could not get sound, but turns out it was playing but there was nothing that prevented my program from stopping yet. So when i added a

Code: Select all

while True: pass
at the end for testing, the song was playing :)

Thanks guys! :mrgreen: