When I try to forward a song with .wav extension in pygame , I get an error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\default\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
File "C:\Users\default\PycharmProjects\pythonProject1\main.py", line 14, in forward
pygame.mixer.music.play(start = forwarded_pos)
pygame.error: Position not implemented for music type
Here's the code:
from tkinter import *
import pygame
root = Tk()
pygame.init()
def play():
pygame.mixer.music.load("test.wav")
pygame.mixer.music.play()
def forward():
forwarded_pos = pygame.mixer.music.get_pos() + 10
pygame.mixer.music.load("test.wav")
pygame.mixer.music.play(start = forwarded_pos)
play_button = Button(root , text = "Play song" , command = play)
play_button.grid(row = 0 , column = 0)
forward_button = Button(root , text = "Forward song" , command = forward)
forward_button.grid(row = 1 , column = 0 , pady = 10)
mainloop()
As I was continuously getting that error , I tried this:
def forward():
forwarded_pos = pygame.mixer.music.get_pos() + 10
pygame.mixer.music.set_pos(forwarded_pos)
But when I did that, I keep getting another error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\default\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
File "C:\Users\default\PycharmProjects\pythonProject1\main.py", line 13, in forward
pygame.mixer.music.set_pos(forwarded_pos)
pygame.error: set_pos unsupported for this codec
Is there any way to fix this problem?
I think forwarding is not supported in .wav format , so fixing this problem with any other format(except .mp3) will also be ok.
Get_pos and set_pos are triggering the same errors for me. You actually don't need them as you can assign the forwarded_pos when you start playing the music as follows. This doesn't seem to work for .wav but it does for .ogg and .mp3 formats.
import pygame
from os import path
pygame.init()
pygame.mixer.init()
snd_dir = path.join(path.dirname(__file__), "snd")
song = path.join(snd_dir, 'song_name.ogg')
forwarded_pos = 22
mixer = pygame.mixer.music
mixer.load(song)
mixer.play(-1, forwarded_pos)
# doesn't work for wav sounds types
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
Related
I'm making a Python program on a MacBook using the curses.textpad module. Here's my complete code:
import curses
from curses.textpad import Textbox, rectangle
def editor(stdscr):
stdscr.addstr(0, 0, "Type something: (hit Ctrl-G to exit)")
editor = curses.newwin(5, 30, 2, 1)
rectangle(stdscr, 1, 0, 1 + 5 + 1, 1 + 30 + 1)
stdscr.refresh()
box = Textbox(editor)
box.edit()
message = box.gather()
print(message)
text = curses.wrapper(editor)
print("Here's what you typed:")
print(text)
It keeps giving me this error whenever I try to run it:
Traceback (most recent call last):
File "/Users/Donoru/Desktop/editor/main.py", line 17, in <module>
text = curses.wrapper(editor)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/curses/__init__.py", line 84, in wrapper
stdscr = initscr()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/curses/__init__.py", line 29, in initscr
setupterm(term=_os.environ.get("TERM", "unknown"),
_curses.error: setupterm: could not find terminal
How can I fix this?
Im currently in an intro coding class and for my final project, i am trying to learn the pyglet module to create a game with a picture in the background, and have a character on the left that a user can make jump, and then have jumps come from the right at a set speed that the user will jump over. i need to use classes for the assignment, and im really having a hard time using creating a sprite class. heres my current code:
import pyglet
window = pyglet.window.Window(700,700)
image = pyglet.image.load('IMG_3315.jpg')#use 10x10 in. image
#image_2 = pyglet.image.load('IMG_3559.jpg')
main_batch = pyglet.graphics.Batch()
score_label = pyglet.text.Label(text="Score: 0", x=570, y=650, batch=main_batch)
the_jump = pyglet.image.load("jumpsi.png")
#horse = pyglet.sprite.Sprite(image_2, x = 50, y = 50)
# background_sound = pyglet.media.load(
# 'Documents/Leave The Night On.mp3',
# streaming=False)
class Jump(pyglet.sprite.Sprite):
def __init__(self, img, x=0, y=0, blend_src=770, blend_dest=771, batch=None, group=None, usage='dynamic', subpixel=False):
self.img = the_jump
self.x = 50
self.y = 50
def draw(self):
self.draw()
# verticle = Jump('verticle')
#window.event
def on_draw():
window.clear()
image.blit(0, 0)
main_batch.draw()
window = Jump()
#horse.draw()
#background_sound.play()
if __name__ == "__main__":
sprite = Jump()
pyglet.app.run()
i know its probably wrong but everything else i have tried (using preexisting games as examples) hasn't worked either.
my current error message is:
Traceback (most recent call last):
File "jumper.py", line 39, in <module>
sprite = Jump()
TypeError: __init__() takes at least 2 arguments (1 given)
im just really stuck and have been trying to figure this out for hours and not made any leeway. any help you can offer would be greatly appreciated. Thanks so much!
UPDATE: i recently changed the code, noticing the problem that Gustav pointed out, and change the end call to
if __name__ == "__main__":
sprite = Jump(the_jump)
pyglet.app.run()
but now i get the error
Traceback (most recent call last):
File "jumper.py", line 39, in <module>
sprite = Jump(the_jump)
File "jumper.py", line 21, in __init__
self.x = 50
File "/Library/Python/2.7/site-packages/pyglet/sprite.py", line 459, in _set_x
self._update_position()
File "/Library/Python/2.7/site-packages/pyglet/sprite.py", line 393, in _update_position
img = self._texture
AttributeError: 'Jump' object has no attribute '_texture'
The error message is telling you exactly which line the error is in and exactly what is wrong. You are initializing the Jump class without passing in an argument for the required parameter img.
You can fix this by either changing the initialization method or your call to Jump().
Here's an example for how to read Python's traceback.
Getting started with python-sfml. This code
import sfml
window = sfml.RenderWindow(sfml.VideoMode(640, 480), "FooBar")
try:
texture = sfml.Texture.from_file("brick_002.jpg")
sprite = sfml.Sprite(texture)
font = sfml.Font.from_file("arial.ttf")
text = sfml.Text("Math Rocks!")
except IOError:
exit(1)
while window.is_open:
for event in window.events:
if type(event) is sfml.CloseEvent:
window.close()
window.clear()
window.draw(sprite)
window.draw(text)
window.display()
and getting this mistake:
Traceback (most recent call last):
File "sfml.py", line 1, in <module>
import sfml
File "/home/montreal/projects/foo/sfml/sfml.py", line 4, in <module>
window = sfml.RenderWindow(sfml.VideoMode(640, 480), "FooBar")
AttributeError: 'module' object has no attribute 'RenderWindow'
But in interpreter everyting works fine (I can create window, add texture and close it). What can be wrong?
Hello all I am working with ncurses (my first time working with a cli) and I keep getting this error
Traceback (most recent call last):
File "cursesDemo1.py", line 5, in <module>
screen.start_color()
AttributeError: start_color
Here is my code:
import curses
import time
screen = curses.initscr()
screen.start_color()
def maketextbox(h,w,y,x,value="",deco=None,underlineChr=curses.ACS_HLINE,textColorpair=0,decoColorpair=0):
nw = curses.newwin(h,w,y,x)
txtbox = curses.textpad.Textbox(nw)
if deco=="frame":
screen.attron(decoColorpair)
curses.textpad.rectangle(screen,y-1,x-1,y+h,x+w)
screen.attroff(decoColorpair)
elif deco=="underline":
screen.hline(y+1,x,underlineChr,w,decoColorpair)
nw.addstr(0,0,value,textColorpair)
nw.attron(textColorpair)
screen.refresh()
return txtbox
try:
screen.border(0)
box1 = curses.newwin(22, 50, 3, 5)
box1.box()
box2 = curses.newwin(22, 50, 3, 65)
box2.box()
box3 = maketextbox(1,40, 10,20,"foo",deco="underline",textColorpair=curses.color_pair (0),decoColorpair=curses.color_pair(1))
textInput = box3.edit()
It has more errors when I take start_color() out can anyone please advise as to a better course of action? Thanks!!!!
Check manual on curses and be sure to call right methods on right objects.
curses.initscr() Initialize the library. Return a WindowObject which
represents the whole screen.
So, curses.initscr() returns WindowObject but start_color() is in curses module itself. You ought to init colors this way
curses.start_color()
I have installed gstreamer, gst-plugins-bad and its python bindings.
The following code selects a song from a given directory and plays it.
import pygst
pygst.require("0.10")
import gst
import pygtk
import gtk
import os
class Main:
def __init__(self):
self.pipeline = gst.Pipeline("mypipeline")
self.musicFiles = os.listdir("musicFiles")
self.currentSong = self.musicFiles[0]
self.findFile = gst.element_factory_make("filesrc", "file")
self.findFile.set_property("location", "musicFiles/" + self.currentSong)
self.pipeline.add(self.findFile)
self.mad = gst.element_factory_make("mad", "mad")
self.pipeline.add(self.mad)
self.findFile.link(self.mad)
self.audioconvert = gst.element_factory_make("audioconvert", "convert")
self.pipeline.add(self.audioconvert)
self.mad.link(self.audioconvert)
self.audioresample = gst.element_factory_make("audioresample", "resample")
self.pipeline.add(self.audioresample)
self.audioconvert.link(self.audioresample)
self.getbpm = gst.element_factory_make("bpmdetect", "bpm")
self.pipeline.add(self.getbpm)
self.audioresample.link(self.getbpm)
self.sink = gst.element_factory_make("playsink", "sink")
self.pipeline.add(self.sink)
self.audioresample.link(self.sink)
self.pipeline.set_state(gst.STATE_PLAYING)
start=Main()
gtk.main()
However, when I try to create the bpmdetect element, it gives the following error:
Traceback (most recent call last):
File "/Users/shubhitsingh/Desktop/Stuff/COLLEGE/US/Carnegie Mellon/Fall '12/15-112/Term Project/Practice/gstreamertutorial-1.py", line 49, in <module>
start=Main()
File "/Users/shubhitsingh/Desktop/Stuff/COLLEGE/US/Carnegie Mellon/Fall '12/15-112/Term Project/Practice/gstreamertutorial-1.py", line 37, in __init__
self.getbpm = gst.element_factory_make("bpmdetect", "bpm")
ElementNotFoundError: bpmdetect
Other elements from gst-plugins-bad can be created without problems. Its just the two elements under soundtouch (bpmdetect and pitch) that cannot be created. Help?