I wrote a small demo of a VLC mediaplayer with 2 buttons : play and stop
and 1 slider for the volume.
The slider works when the player is playing, i.e. the volume is changing depending on the slider setting.
when it is stopped and the slider position is changed I need to click on play 2 times to make the volume changing according to the new setting of the slider. clicking 1 time only keeps the previous setting of the volume.
Here is the code :
import vlc
from tkinter import *
from tkinter import ttk
volume_ini=50
url='http://direct.fipradio.fr/live/fip-midfi.aac'
instance=vlc.Instance('--input-repeat=-1','--fullscreen')
player=instance.media_player_new()
media=instance.media_new(url)
player.set_media(media)
root=Tk()
window_height=240
window_width=320
screen_width=root.winfo_screenwidth()
screen_height=root.winfo_screenheight()
x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2) - (window_height/2))
root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
v1=DoubleVar()
def st():
player.stop()
def pl():
global player
global volume_ini
player.audio_set_volume(volume_ini)
player.play()
class BarVolume(ttk.Scale):
def __init__(self, master):
global v1
global volume_ini
ttk.Scale.__init__(self,master,
variable=v1,from_=0,to=100,orient=HORIZONTAL,length=80)
self.volume=volume_ini
self.bind("<ButtonRelease-1>", self.updateValue)
self.set(volume_ini)
def updateValue(self, event):
global volume_ini
global v1
global player
volume_ini=int(v1.get())
self.volume=volume_ini
labelText.set(str(volume_ini))
player.audio_set_volume(self.volume)
#self.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
content1 = ttk.Frame(root)
content1.grid(column=0, row=0)
led_1=ttk.Button(content1, text='play',command=lambda:pl(),width=20)
led_1.grid(column=0, row=0)
led_2=ttk.Button(content1, text='stop',command=lambda:st(),width=10)
led_2.grid(column=0, row=1)
labelText = StringVar()
lbl=ttk.Label(content1,textvariable=labelText,width=20)
lbl.grid(column=0, row=3)
echelle=BarVolume(content1)
echelle.grid(column=0, row=4)
root.mainloop()
player.stop()
I tried to play with global and local variables
I also wrote the same code with a class embedding the whole window
bbd 666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.