I am trying to make my own music player with Python, and after looking at alternatives, I've settled on using pygame's mixer.music to actually play the audio. (I've used pygame before, just usually for actual games) I was looking at playsound instead until I realized I needed a way to play the next song once one is done, as well as the ability to play and pause the audio. I also need to play mp3 files instead of the wavs that most alternatives require.
I actually got it working perfectly originally, until I tried adding other unrelated features, and now it's saying:
File "main.py", line 66, in playCurrentSong
mixer.load(path.join(museDir, currentSong))
pygame.error: Error reading the stream. (code 18)
(museDir is my variable for the directory that music files are in, and mixer is my variable for pygame.mixer.music as a shorthand)
I cannot figure out for the life of me why it's giving me this error now, as it played the audio perfectly fine before. My code is here: https://pastebin.com/V7nAfmK6
If a solution only works on a certain operating system, my final OS will be Linux, on a Rasperry Pi, but I'm trying to write and test the code on Windows. However, if that's not possible, I understand.
Thank you beforehand for any and all help; this is giving me a headache.
I just found the source of the problem.
Before the error popped up, I had been trying to mess around with the metadata of the mp3 files in order to incorporate a genre system into the player, but nothing was working. I eventually decided to use csv files for that instead.
I must have done something wrong however when I was messing around with that metadata because I looked at the mp3s in File Explorer, and they were all 0 bytes. That's why pygame couldn't read the stream: there wasn't one! I plugged the pygame stuff back in, replaced the mp3s with new ones, and it works just fine now.
Thanks for the help anyway though, Torxed!
Related
Does anybody know of an easy module to interact with MPRIS and write metadata to it?
I'm trying to get a music player working with the OS, and I'm using VLC [python-vlc] as it's backend. If you can help me out find any module that allows me to easily write metadata, thanks.
Modules I've tried:
playerdo - Only reads metadata
playerctl - Only reads metadata
modipy-mpris - Doesn't work
mpris-server - Breaks constantly and generally a pain to set up
vidify - Not what I've wanted
VLC docs: https://www.olivieraubert.net/vlc/python-ctypes/doc/index.html
NOTE: This is not what my player looks like, this is KDE's Applet allowing me to see what is playing. I'm sure windows has something similar, but the most important thing is I need to be able to play and pause music from anywhere in the OS with the play/pause button on my keyboard or bluetooth device
Example of output: KDE Plasma Applet
Use MPRIS-SERVER but read the issues.
Still a pain to set up but at least it works properly.
I am trying to make a simple program that plays some narration, and has a sound effect on a trigger. Being new to python, I am just building up functions in layers. Getting music to work was easy, as it should be, but I cannot get sound to work.
I have read of a few people with similar issues, but either they are also unsolved, or the symptoms are slightly different, and the solutions don't work for me.
I have tried this on a pi3 (which is the end target), and windows 7, both running latest python 3, and pygame.
import pygame
import time
import os
pygame.mixer.init()
#pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
pygame.mixer.music.load("English.mp3")
pygame.mixer.music.play()
print (os.getcwd())
shot = pygame.mixer.Sound("gun-gunshot-02.wav")
shot.play()
while True:
Time.sleep(1) # for testing and irritation prevention if sound ever plays
shot.play()
I should also note that the sound effect does work if I play it using music, but of course it replaces the narration.
Error is as follows same on both machines, same with both mp3 and wav:
C:\Users\me\Documents\Interrupter
Traceback (most recent call last):
File "C:/Users/me/Documents/Interrupter/simpletest.py", line 11, in
shot = pygame.mixer.Sound("gun-gunshot-02.wav")
pygame.error: Unable to open file 'gun-gunshot-02.wav'
Thanks in advance
So I have solved the issue, which is of file compatibility.
I don't know what the right kind of wav file is (other than uncompressed). I had tried using a wav but it did not work.
However I finally tried .ogg since that was the only thing listed as working in the online manual for pygame. As soon as I used .ogg everything worked as planned.
For anyone else with this issue I used Audacity to export my .mp3 and .wav files to .ogg using defaults.
Pygame is supporting only 2 type of files.
OGG and WAV
Sound can be loaded from an OGG audio file
or
from an uncompressed WAV.
for more information please visit to
https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound
I wrote a music player in Python using Pygame and Pydub(I used Pygame for actually playing the music, while Pydub is probably unrelated to the issue).
The music works fine even when windows are switched unless I switch to another Pygame window. I thought this effect would go away if I compiled it(cx_freeze), but that didn't work.
So I was wondering if there is any way to let the music keep playing when the window is switched to another Pygame window.
I used pygame.mixer.music instead of Sound objects if that might somehow be related.
Thanks in advance!
Not exactly a fix, but it seems that if I disable the mixer(pygame.mixer.quit()) it doesn't cancel out the audio. Unfortunately, I haven't found a way to use audio in two pygame windows at once.
Feel free to post another answer if anyone figures out a full fix.
Recently I have been trying to make a game in python with pygame and have so far succeeded. My endeavors came to a sudden halt when I tested the game after implementing sound code, and found that no sound played. I have looked around the forums and most people say to convert the .mp3 to a .wav or .ogg, and convert I did, but to no avail. I was thinking if it isn't the file, then maybe there must be something off with my code. Can you guys tell me what I can do to fix it?
The code is as follows
pygame.mixer.init()
pygame.mixer.music.load('BennyHill.wav')
pygame.mixer.music.play(-1, 0.0)
I am just a beginner at the moment, so it might look completely wrong to you experts out there
Do pygame.music.play(-1). In my programs that always works.
The docs are sometimes incorrect with needed/optional arguments.
For playing my sounds I have this working code:
import pygame
pygame.init()
pygame.mixer.pre_init(44100, 16, 2)
screen = pygame.display.set_mode([100,100])
print(pygame.mixer.get_num_channels(),"cannels")
sounda = pygame.mixer.Sound("track.mp3")
print("Length",sounda.get_length())
print("Volume",sounda.get_volume())
channela = sounda.play()
while channela.get_busy():
pygame.time.delay(100)
pygame.quit()
You can see I am mostly using .mp3 files.
But my problem is that it can play some sounds but others result the error which says that the sound can not be loaded. Are there any differences between mp3 files and do you know how to solve this problem?
Thanks in advance
So I have found out there can be differences in one sound format so that pygame has problems with it. With the program ocenaudio I can manipulate and save my sounds in multiple types of the .wav format. Pygame can deal with one of them.
But loading music has never been a problem...
If Pygame is not loading your audio, there are 3 possibilities.
Possibility 1:
Pygame cannot find your file. When saying the name of a file, make sure either the sound file is in the same directory as your program file, or you can give the entire file path instead (like '/home/person/sound.mp3', instead of just 'sound.mp3'.)
Possibility 2:
Your file is the wrong format. Though it is called .mp3, it may be a different type. Try using an audio conversion program.
Possibility 3:
Pygame just plain cannot read the file. Pygame works best with .ogg files, but sometimes has issues with other types. Just use an audio conversion program to convert it to .ogg, and it might work.
If none of these work, giving an exact error message would be appreciated.