When running a pygame script as root, no sound is output? - python

I have written a very simple script for my raspberry pi that loads an uncompressed WAV and plays it - however when I run the script as root (to be able to use GPIO and ServoBlaster), there is no sound output.
I have set the default audio device to a USB sound card, and this works - I have tested this using aplay fx.wav.
Running the pygame script without sudo, the sound plays fine.
What is going on here?

The issue was the sudo command changing the directory in which the script was being run - so running python with sudo -s or simply using an absolute path for the sound fixed it.

Related

Sound not playing in python via cmd but works through idle

I tried different ways of playing sound through cmd or any other terminal like that of vscode but python using any module (pygame,winsound,playsound,pyglet,etc.) doesn't play sound. I tried and found that idle plays all the sounds. Is there any way by which even other terminals and ides can play sound?
If you need more info please inform
I even tried using ctrl+g in cmd. it produced sound but when I did the save by using .bat file.It doesn't produce any Sound
Edit:-I work in windows and none other than any terminal like powershell works.It works only through Idle
Edit2:-I used many different codes but the basic code used was
import winsound
# for playing note.mp3 file
winsound.PlaySound('click2.wav',winsound.SND_ASYNC)
print('playing sound using playsound')

Raspberry Pi: Enable or disable touch screen from Python script

I am working with Raspberry Pi 4 B, 8 gb RAM and Raspbian OS. I am having 7" touch screen attached to PI.
In my project, I want to make touch screen sleep if there is no activity detected for 5 mins from the Python script (just to save some power). Is there any shell command I can use in the Python script?
There is the option in config.txt of "disable_touchscreen=1" which will stop the Pi polling the controller board for touch input. It also doesn't start the firmware side of the touchscreen input driver, which should stop the Linux side loading either. The display side will work exactly as before.
This file is normally accessible as /boot/config.txt from raspbian OS.
You can read config.txt and modify using following python code
import ConfigParser
config_parser = ConfigParser.ConfigParser()
config_parser.read('config.txt')

No sound on Raspbian when using root

I'm having a really weird issue on my Raspberry. I need to play sounds with it and I need to do it inside a script which requires sudo to work. However, for some reason, when I try to play sounds with sudo, it doesn't work.
No errors, it just doesn't make any sound (I tried with several libraries, none worked). It does work if I'm using omxplayer.
If I don't use sudo, everything works fine.
Anybody had a similar issue?
Edit: I added one of the code I used (this works just fine on another Raspberry)
import pygame
import time
pygame.mixer.init()
pygame.mixer.music.stop()
pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play()
print("Going forward")
while True:
time.sleep(1)
Are you using the 3.5mm jack or HDMI?
One thing I have noticed in the past is that if I run alsamixer as user=pi the default device is the 3.5mm aux jack. However if I run sudo alsamixer the default device is now HDMI. Never looked in to why, just know that's a thing.
Spent nearly a day trying to enable the analog out when using the root user.
First make sure the default user (pi) has audio:
aplay sound.wav
If that works then copy the audio configuration to the root user home, try this:
sudo su
cd
cp /home/pi/.asoundrc .asoundrc
reboot

How to use cd terminal command in Python

Question
How can I change the active directory on the raspberry pi using cd and the subprocess module?
Background
Since I absolutely hate to use the command line, I am trying to create a basic GUI text-editor which can also compile my programs. For now, I am just trying to change the directory to Desktop. To do this, I am using the subprocess module. Here is my current code:
from subprocess import *
call(["cd","Desktop"])
In the terminal, this line (cd Desktop) would change the active directory to Desktop. Oddly, when I run it through subprocess, I am given this error:
OSError: [Errno 2] No such file or directory
Tech Specs
Raspberry Pi Model B
Raspbian "Wheezy" OS
I would try os.chdir
import os
os.chdir("/path/to/dir")
i don't mean to derail the original question, but if you're trying to automate a lot of tasks, you can use the fabric module.
it has a rather simple syntax like this:
with cd('/path/to/app'):
with prefix('workon myvenv'):
run('./manage.py syncdb')
http://docs.fabfile.org/en/1.6/api/core/context_managers.html
it's designed for remote usage over ssh, but many people use it for a lot of local management & deployment
the lcd command works on your local machine:
with lcd('/path/to/app'):
with prefix('workon myvenv'):
run('./manage.py syncdb')

pygame.mixer sound not playing when script run from command line

I'm working on a Raspberry Pi project and I have a python script that accepts some serial input and plays sounds depending on the input. I have the script set up and it works just fine when I run it from within the GUI (i.e. startx). If I log out of the GUI and try to run the script from the command line the script executes just fine but my sounds don't play. I just get a momentary static click. I can tell the script is running because I have it printing debug code and the print's work just fine. Is there a way to get the sounds to work from the command line?
I want this script to execute when the Raspberry Pi is turned on without user input which I believe means it will be running from the command line. If there is some reason the sounds simply won't play until the GUI starts up how would I set it up to load the GUI and then execute the script on startup without any user input?
This will be embedded in a prop and will play sounds when some buttons (connected through arduino i.e. serial input) are pressed. So I need a solution that will have it from power on automatically run the script and be able to play the sounds with no keyboard, mouse, or monitor attached.
Turns out it was file path naming. If I have the command line test to the root directory it doesn't work but if I "cd Desktop/containingFolder" then the sounds play. I'll play with how I have the files set up in the python script so it will work.
Updating the path names fixed the issue. I just needed them to be full paths instead of relative ones.

Categories

Resources