问题
I am trying to wait for a song to finish to play the next song.
I am running python 3.4 on windows but am trying to be able to use it on Linux (raspberry pi
).
I have used the command subprocess.call([PLAYER, SONG])
to open the mp3 file. It plays in vlc and then stops. I know that I have not incorporated anything for the program to tell python when its finished.
That's where you guys come in. I want to find a command that will wait until the song has finished and when the song is finished, the rest of the code will process.
I would like the command to be able to used on a Linux operating system and I am trying not to have to install any modules.
If modules are definitely needed, I will install them.
回答1:
You may use VLC python bindings to get the different information about the VLC player.
There are different methods that can provide your different information. You may achieve your solution by using the get_state(self) method, it provides the state information of the player. So, when the song is completed the VLC state would be stopped.
Sample code:
import vlc
def setup_player(filename):
vlc_instance = vlc.Instance()
media = vlc_instance.media_new(filename)
player = vlc_instance.media_player_new()
player.set_media(media)
player.play()# Start the player
print player.get_state()# Print player's state
player.stop()# Stop the player
print player.get_state()# Print player's state
setup_player('filename')#Put media file name/location here
I hope it was helpful.
来源:https://stackoverflow.com/questions/24403867/wait-for-song-played-outside-of-python-to-finish