python123
python123

Reputation: 37

Stop music playing with vlc module?

I wrote a code that plays song, but i don't know how to stop the song while its playing (i run it with cmd)

This is the code:

import vlc

my_song = vlc.MediaPlayer("path to song")

my_song.play()

while True:
    pass

I thought that if i will run another code while the music is playing (in another cmd window) it will stop the song but it is not working. This was the idea:

import vlc

my_song = vlc.MediaPlayer("path to song")

my_song.stop()

while True:
    pass

The goal is to play and stop music, i am building a music program. Tnx for help

Upvotes: 1

Views: 5375

Answers (1)

byumark
byumark

Reputation: 298

Seems to me like this would all have to be part of the same process and not broken up into two processes (two command windows). Depending on how you are running it, the one process (window) likely doesn't know about the other process (window).

You'll probably want to accomplish this in the same code, similar to the code mentioned in @martineau's comment above. You'll need an event to stop the playing. Here is an example of a timer event.

import vlc
import time

my_song = vlc.MediaPlayer("path to song")

my_song.play()

timeout = time.time() + 3   # 3 seconds from now
while True:
    if time.time() > timeout:
        my_song.stop()

You may want another event like a button push, etc.

Upvotes: 2

Related Questions