I was making an music app player and was trying to implement a function where the user can seek forward or backward using the seekbar. Whenever I tried to click anywhere on the seekbar, my music position would change according to the value of the seekbar where I clicked. But the seekbar starts back from 0 whenever I try to skip in the song. How to fix this? The below one is my code.(Ignore the format, I am not much experienced python developer)
import io from pygame import mixer from mutagen.mp3 import MP3 # Variables current_song = "songs/middle_of_night.mp3" has_song_started = False is_song_playing = False is_slider_dragging = False # Get the current song length audio = MP3(current_song) total_duration = audio.info.length # Programming Code mixer.init() mixer.music.load(current_song) def play_song(): global has_song_started global is_song_playing mixer.music.play() has_song_started = True is_song_playing = True def pause(): global is_song_playing mixer.music.pause() is_song_playing = False def resume(): global is_song_playing mixer.music.unpause() is_song_playing = True def playback_slider_event(value): global total_duration music_pos = (float(value)/100)*total_duration music_pos = music_pos * 1000 print(f"value = {value}") print(f"music_pos = {music_pos}") # mixer.music.stop() # mixer.music.play(start=music_pos/1000) mixer.music.set_pos(music_pos/1000) # print(f"percentage = {percentage}") slider.set(value) # GUI slider = ctk.CTkSlider(home, from_=0, to=100, command=playback_slider_event, width=600) slider.pack(pady=30) # Add padding around the slider # Programming Code def set_playback_slider(): global is_slider_dragging if not is_slider_dragging: current_position = mixer.music.get_pos()/1000 percentage = (current_position/total_duration)*100 print(percentage) slider.set(percentage) app.after(1000, set_playback_slider) def slider_drag_start(event): global is_slider_dragging is_slider_dragging = True def slider_drag_end(event): global is_slider_dragging is_slider_dragging = False # Bind the slider drag events slider.bind("<ButtonPress-1>", slider_drag_start) # When slider dragging starts slider.bind("<ButtonRelease-1>", slider_drag_end) # When slider dragging ends change_play_button_command() set_playback_slider() app.mainloop()
submitted by /u/PFG_Gamer
[link] [comments]
r/learnpython I was making an music app player and was trying to implement a function where the user can seek forward or backward using the seekbar. Whenever I tried to click anywhere on the seekbar, my music position would change according to the value of the seekbar where I clicked. But the seekbar starts back from 0 whenever I try to skip in the song. How to fix this? The below one is my code.(Ignore the format, I am not much experienced python developer) import io from pygame import mixer from mutagen.mp3 import MP3 # Variables current_song = “songs/middle_of_night.mp3″ has_song_started = False is_song_playing = False is_slider_dragging = False # Get the current song length audio = MP3(current_song) total_duration = audio.info.length # Programming Code mixer.init() mixer.music.load(current_song) def play_song(): global has_song_started global is_song_playing mixer.music.play() has_song_started = True is_song_playing = True def pause(): global is_song_playing mixer.music.pause() is_song_playing = False def resume(): global is_song_playing mixer.music.unpause() is_song_playing = True def playback_slider_event(value): global total_duration music_pos = (float(value)/100)*total_duration music_pos = music_pos * 1000 print(f”value = {value}”) print(f”music_pos = {music_pos}”) # mixer.music.stop() # mixer.music.play(start=music_pos/1000) mixer.music.set_pos(music_pos/1000) # print(f”percentage = {percentage}”) slider.set(value) # GUI slider = ctk.CTkSlider(home, from_=0, to=100, command=playback_slider_event, width=600) slider.pack(pady=30) # Add padding around the slider # Programming Code def set_playback_slider(): global is_slider_dragging if not is_slider_dragging: current_position = mixer.music.get_pos()/1000 percentage = (current_position/total_duration)*100 print(percentage) slider.set(percentage) app.after(1000, set_playback_slider) def slider_drag_start(event): global is_slider_dragging is_slider_dragging = True def slider_drag_end(event): global is_slider_dragging is_slider_dragging = False # Bind the slider drag events slider.bind(“<ButtonPress-1>”, slider_drag_start) # When slider dragging starts slider.bind(“<ButtonRelease-1>”, slider_drag_end) # When slider dragging ends change_play_button_command() set_playback_slider() app.mainloop() submitted by /u/PFG_Gamer [link] [comments]
I was making an music app player and was trying to implement a function where the user can seek forward or backward using the seekbar. Whenever I tried to click anywhere on the seekbar, my music position would change according to the value of the seekbar where I clicked. But the seekbar starts back from 0 whenever I try to skip in the song. How to fix this? The below one is my code.(Ignore the format, I am not much experienced python developer)
import io from pygame import mixer from mutagen.mp3 import MP3 # Variables current_song = "songs/middle_of_night.mp3" has_song_started = False is_song_playing = False is_slider_dragging = False # Get the current song length audio = MP3(current_song) total_duration = audio.info.length # Programming Code mixer.init() mixer.music.load(current_song) def play_song(): global has_song_started global is_song_playing mixer.music.play() has_song_started = True is_song_playing = True def pause(): global is_song_playing mixer.music.pause() is_song_playing = False def resume(): global is_song_playing mixer.music.unpause() is_song_playing = True def playback_slider_event(value): global total_duration music_pos = (float(value)/100)*total_duration music_pos = music_pos * 1000 print(f"value = {value}") print(f"music_pos = {music_pos}") # mixer.music.stop() # mixer.music.play(start=music_pos/1000) mixer.music.set_pos(music_pos/1000) # print(f"percentage = {percentage}") slider.set(value) # GUI slider = ctk.CTkSlider(home, from_=0, to=100, command=playback_slider_event, width=600) slider.pack(pady=30) # Add padding around the slider # Programming Code def set_playback_slider(): global is_slider_dragging if not is_slider_dragging: current_position = mixer.music.get_pos()/1000 percentage = (current_position/total_duration)*100 print(percentage) slider.set(percentage) app.after(1000, set_playback_slider) def slider_drag_start(event): global is_slider_dragging is_slider_dragging = True def slider_drag_end(event): global is_slider_dragging is_slider_dragging = False # Bind the slider drag events slider.bind("<ButtonPress-1>", slider_drag_start) # When slider dragging starts slider.bind("<ButtonRelease-1>", slider_drag_end) # When slider dragging ends change_play_button_command() set_playback_slider() app.mainloop()
submitted by /u/PFG_Gamer
[link] [comments]