Search code examples
pythonlibvlc

Python script executes but unable to hear sound which it is playing


I am using libvlc python binding to play a music file.my code is as follows:

import vlc
instance = vlc.Instance()

#Create a MediaPlayer with the default instance
player = instance.media_player_new()

#Load the media file
media = instance.media_new('01.DONT CARE.mp3')

#Add the media to the player
player.set_media(media)

try:
    player.play()
except Exception, e:
    raise e

The script executes successfully but I am unable to hear anything. If the code is executed line by line then it works correctly and I am able to hear the sound.Any idea on what might be wrong?


Solution

  • I think the reason it is not being played is because player.play() is asynchronous. So when the script exits, it kills the process and stops the media straight away. Try add a time.sleep(10) and see does it play.

    Note: Don't forget to import time at the top.

    The reason that it would be working as you type it in line by line is because it isn't exiting the python program.