0

Controlling the VLC media player using python-vlc module

I have tried the code below but am getting the error:

Traceback (most recent call last): File "", line 1, in

import vlc   File "vlc.py", line 2, in <module>
# -*- coding: utf-8 -*-

AttributeError: 'module' object has no attribute 'MediaPlayer'

Using code:

import vlc
    media_player = vlc.MediaPlayer("path_to_your_song.mp3")
    media_player.play()

I want the script to run and play the file

Jonny
  • 3,807
  • 8
  • 31
  • 48
anita
  • 21
  • 1
  • 2

2 Answers2

2

Just change the path,your good to go..

from vlc import Instance
import time
import os

class VLC:
    def __init__(self):
        self.Player = Instance('--loop')

    def addPlaylist(self):
        self.mediaList = self.Player.media_list_new()
        path = r"C:\Users\dell5567\Desktop\engsong"
        songs = os.listdir(path)
        for s in songs:
            self.mediaList.add_media(self.Player.media_new(os.path.join(path,s)))
        self.listPlayer = self.Player.media_list_player_new()
        self.listPlayer.set_media_list(self.mediaList)
    def play(self):
        self.listPlayer.play()
    def next(self):
        self.listPlayer.next()
    def pause(self):
        self.listPlayer.pause()
    def previous(self):
        self.listPlayer.previous()
    def stop(self):
        self.listPlayer.stop()

Create a object

player = VLC()

Add playlist

player.addPlaylist()

Play the song

player.play()
time.sleep(9)

Play the next song

player.next()
time.sleep(9)

Pause the song

player.pause()
time.sleep(9)

Resume the song

player.play()
time.sleep(9)

Previous song

player.previous()
time.sleep(9)

Stop the song

player.stop()
Pramod
  • 161
  • 2
  • 4
  • I tried this solution and worked perfectly for me. Now I was wondering, after using player.play() if I want to use player.pause() or player.stop() asynchronously, what will be the best approach? For example: I was thinking, if I create separate modules for play, pause, stop etc where I will import individual methods from this class. But the problem will be how to use the same player instance in different modules. so, I was looking for the best pythonic way to do that – zafi005 Sep 14 '20 at 16:02
  • I guess you can use inheritance concepts. Create a class with init method only and derive all other classes from main class. – Pramod Sep 15 '20 at 17:11
  • 1
    I've seen a couple ways to instantiate a new player including `mediaplayer = vlc.MediaPlayer()`. Where are the docs for these bindings? Super useful, but virtually undocumented. – Wes Modes May 11 '21 at 15:47
1

You need to create a vlc Instance.
The minimum required would be something like this, but there are many variations.

>>> import vlc
>>> i = vlc.Instance()
>>> media_player = i.media_player_new()
>>> media_player.set_mrl('./vp1.mp3')
>>> media_player.play()
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Sir I tried this code but I am getting following error using Python2.7 on Windows 7 ultimateTraceback (most recent call last): File "C:\Python27\vlc3.py", line 1, in import vlc File "C:\Python27\vlc.py", line 207, in dll, plugin_path = find_lib() File "C:\Python27\vlc.py", line 167, in find_lib dll = ctypes.CDLL(libname) File "C:\Python27\lib\ctypes\__init__.py", line 362, in __init__ self._handle = _dlopen(self._name, mode) WindowsError: [Error 126] The specified module could not be found – anita Aug 28 '19 at 06:16
  • @anita I don't use windows so I am unable to give you any real hint as to what the issue is. However, this vlc unrelated question may provide a clue https://stackoverflow.com/questions/23804438/find-library-in-ctypes On the downside I don't see how you code posted in the question would have worked either if the code I posted is in the same directory. Have you tried using just the 5 lines of code above in the directory that contains the `vlc.py` program. – Rolf of Saxony Aug 28 '19 at 08:49