Music Player - problem with skipping songs + pause etc - python

I've been making a music player program to practice using Tkinter for my NEA (Alevel computer science project) I have a shuffle function in the program but can't seem to find a way of pausing/skipping songs.
Example of code: (Obviously I have imported tkinter, random and winsound, also I have more songs)
def song1():
print('song name - song artist')
winsound.PlaySound(r"song address",winsound.SND_FILENAME)
def shuffle():
ShuffleButton.grid_remove()
playlist=list(range(1,NumberOfSongs+1))
random.shuffle(playlist)
print(playlist)
i=int(0)
while i<=NumberOfSongs:
if playlist[i]==1:
song1()
i=i+1
Btw I know there is probably a much simpler way to make a music player but I can actually understand this way.

Replace SND_FILENAME with SND_ASYNC this will allow you to change the song whilst one is playing.
When using SND_ASYNC the shuffle function needs to be amended so the song will play - this can be done by using time.sleep(LengthOfSongInSeconds) underneath winsound.PlaySound(r"SongAddress",SND_ASYNC) this will allow the song to play before the next (el)if playlist[i]==x: will play the next song in the list. I am aware that there will be a more efficient way of allowing the song to be played in full and I am working on the solution.

Related

How can I play song repeatedly using pygame.mixer.music.play()?

I have tried to repeat a song(mp3) using pygame module. The code is as follows from the site https://www.studytonight.com/tkinter/music-player-application-using-tkinter
When calling this function, it comes out only one time, of course it's natural.
def playsong(self):
# Displaying Selected Song title
self.track.set(self.playlist.get(ACTIVE))
# Displaying Status
self.status.set("-Playing")
# Loading Selected Song
pygame.mixer.music.load(self.playlist.get(ACTIVE))
# Playing Selected Song
pygame.mixer.music.play()
To repeat this sound file I added loop like this, but it plays just one time.
How can I repeat many times over and over this? Should I call the function playsound(self) many times I want to repeat?
def playsong(self):
for i in range(3):
# Displaying Selected Song title
self.track.set(self.playlist.get(ACTIVE))
# Displaying Status
self.status.set("-Playing")
# Loading Selected Song
pygame.mixer.music.load(self.playlist.get(ACTIVE))
# Playing Selected Song
pygame.mixer.music.play()
The first optional argument (loops) to pygame.mixer.music.play() tells the function how often to repeate the music.
For instance pass 1 to play the music twice (repeat once):
pygame.mixer.music.play(1)
Pass -1 to play the music infinitely:
pygame.mixer.music.play(-1)
See pygame.mixer.music.play():
play(loops=0, start=0.0, fade_ms = 0) -> None
This will play the loaded music stream. If the music is already playing it will be restarted.
loops is an optional integer argument, which is 0 by default, it tells how many times to repeat the music. The music repeats indefinately if this argument is set to -1.

Python Keyboard Module, wait for user

I'm currently building a python code to play Blackjack where I need to be able to call certain functions uppon keypress without necessarily being in the terminal. I found this keyboard module which helped but I came across this issue.
def hit_or_stand(deck,hand):
global playing
print ("\nWould you like to Hit or Stand? Enter 'h' or 's'")
if keyboard.is_pressed('h'):
hit(deck,hand)
if keyboard.is_pressed('s'):
print("Player stands. Dealer is playing.")
playing = False
Let me explain what this function does as I don't want to overload everyone with the entire code. Essentially this function is called in a loop until the player decides to hit s which declares playing as False and therefore stopping the loop. If the player hits h then the hit function is called which draws a card. The player can draw as many cards.
My problem right now is that this code doesn't wait for user keypress. It loops extremely fast repeating it. I only want it to loop after either h or s is pressed.
I tried using keyboard.wait('h') on the line above the first if but then this doesn't allow the user to press s and therefore declare playing as False.
What I'm looking for is something along the lines of keyboard.wait('h'or's') but I know this does not work.
Thank you for your help. I am using python 3.7.9
EDIT: Keyboard Module I am referring too:
https://pypi.org/project/keyboard/
Use keyboard.read_key() to wait for any input.
If it's not h or s ,read_key again.
import keyboard
def mywait():
keyboard.read_key()
def my_function():
print("Hello")
def my_exit():
quit()
keyboard.add_hotkey("p", my_function)
keyboard.add_hotkey("esc", my_exit)
while True:
mywait()

How to use multiple instance of the same import module

I want to use the import module in 2 separate instances
[python]
import pygame
[...]
player1 = pygame.mixer
player1.init()
player1.music.load('file1.mp3')
player1.music.play()
[...]
player2 = pygame.mixer
player2.init()
player2.music.load('file2.mp3')
player2.music.play()
i want to play file1.mp3 and file2.mp3 at the same time but this way of coding starts to play file1.mp3 then start to play file2.mp3 (file1.mp3 is stopped)
i want to play them both simultaneously not one then the other
It's a kind of limitation of pygame with .mp3 format files to reproduce sounds in parallel. I found this information here: Pygame - Play sounds simultaneously
To achieve your results, it's necessary change the format from .mp3 to .wav or .ogg
See the example below:
import pygame
pygame.mixer.init()
sounds = [pygame.mixer.Sound('file1.wav'),
pygame.mixer.Sound('file2.wav')]
channels = [sound.play() for sound in sounds]
while any(channel.get_busy() for channel in channels):
print('Playing ...')
pygame.time.wait(100)
print('Finished')

on_player_eos event not working when trying to play through a list of mp3 files with Pyglet

I'm trying to shuffle a list of mp3 files and then play through them with pyglet. Once the player runs out of sources I want to re-shuffle the list, and then play through them again. This process would happen infinitely. I have been able to get pyglet to play through the sources once, but the on_player_eos event is never fired when I run out of sources. I'm not sure exactly what I am doing wrong, or if I am approaching it in the wrong way. I've tried a couple ways of doing it, but have had no luck getting it to work. Here is the code
import os, sys, random, pyglet
from random import shuffle
song1 = pyglet.media.load('beats/breathe.mp3', streaming=False)
song2 = pyglet.media.load('beats/everything.mp3', streaming=False)
song3 = pyglet.media.load('beats/jazz.mp3', streaming=False)
player = pyglet.media.Player()
# Play beatz
def beatz():
playlist = [song1, song2, song3]
shuffle(playlist)
player.queue(playlist[0])
player.queue(playlist[1])
player.queue(playlist[2])
player.play()
def on_player_eos():
print('repeating')
beatz()
player.on_player_eos = on_player_eos
pyglet.app.run()
I have also tried replacing the 'player.on_player_eos = on_player_eos' with
player.push_handlers(on_player_eos)
But it doesnt seem to do anything either. Any help appreciated.
UPDATE:
I was able to get this to work by simply adding 'streaming=False' to each song variable. Streaming sources in pyglet can only be queued once on a player object, while static sources can be queued as many times as you want.
With recognizing the event 'on_player_eos', I also wasn't aware of the pyglet event loop. In order for pyglet to pick up the events I wanted it to, the code below had to be run.
pyglet.app.run()
This fixed my problems.

How to know if the PyMedia module is busy

So I'm making a music player with the PyMedia module and the Tkinter module. I've been Googling for an answer on to to "check" if PyMedia.startPlayback is busy playing a song, but so far I've been unsuccessful. My goal is to be able to play the next song in the queue when the current song stops playing.
for example:
def Listen(songs):
global player
while True:
songs+=1
def play(number):
player.startPlayback(number)
def pause():
player.pausePlayback()
#init pymedia player
player= pymedia.Player()
player.start()
play(songs)
#wait till song is done playing...
#After finished continue on to the next one
play(songs)
This isn't the exact code but it is the gist of it.
I have python 2.7 on Windows 7. Thanks for you input.
http://pymedia.org/docs/pymedia.player.html
The docs list an isPlaying function for the player. They even use it in the example for the while loop.

Categories