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

房东的猫 提交于 2019-12-02 11:55:01

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/34878391/python-script-executes-but-unable-to-hear-sound-which-it-is-playing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!