Re: Audio wrapper for Python

Oops! ambro86 is right, the [mixer.music] functions do support position control. But theres a few caveats, music played like this is streamed only from the mixer itself, and the mixer can only stream one track at a time. There also doesn't seem to be a function to tell how long the track actually is, short of playing it and tracking how long it plays for. In your case, this might not be much of an issue, so something like this might work:

import pygame
from pygame import mixer
import sys

def Example():
#initialize pygame
    pygame.init()
#initialize sound mixer
    mixer.init()
#create display
    window = pygame.display.set_mode([640,480])
#load sound
    mixer.music.load('track1.wav')

#main update loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
            #if space is pressed, play sound
                if event.key == pygame.K_SPACE:
                    mixer.music.play()
            #get playback position in milliseconds
                if event.key == pygame.K_LEFT:
                    if mixer.music.get_busy():
                        print("playing",mixer.music.get_pos()/1000.0)
                        mixer.music.set_pos(mixer.music.get_pos()-1000.0)
            #get playback position in milliseconds
                if event.key == pygame.K_LEFT:
                    if mixer.music.get_busy():
                        print("playing",mixer.music.get_pos()/1000.0)
                        mixer.music.set_pos(mixer.music.get_pos()+1000.0)
            #if escape is pressed, quit
                if event.key == pygame.K_ESCAPE:
                    mixer.music.stop()
                    mixer.music.unload()
                    pygame.quit()
                    sys.exit(0)

    #update window
        pygame.display.update()

Example()
-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Turkce_Rap via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Turkce_Rap via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ambro86 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ambro86 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Turkce_Rap via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ashleygrobler04 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Turkce_Rap via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector

Reply via email to