Is it possible to go to a specific time in a sound file using Python?

Hi,

So like the title asks, using Python, would I be able to use a property to go to a specific time in a sound file? If it is possible, would it work in seconds/minutes or frames? As far as I know, there isn’t much that can be done with sound in Blender so I’m not sure if this can be done.

sure:


limit(start, end)
Limits a factory within a specific start and end time.

https://docs.blender.org/api/2.78a/aud.html

2 Likes

I’m not sure how you’d put that into a script.

Also I didn’t know about the docs for the audio api, it seems there is a fair amount for audio in BGE. Thanks for that.

import bge
import aud
from bge.logic import expandPath

# Basic references
cont = bge.logic.getCurrentController()
own = cont.owner
device = aud.device()

# Sensors
forward = cont.sensors['forward'] # Right key
rewind = cont.sensors['rewind'] # Left key
toggle_pause = cont.sensors['toggle_pause'] # Spacebar key

# Initialize sound
if not 'handle' in own:
	
	# Loads the factory into memory
	factory = aud.Factory(expandPath('//Lalalalalala.mp3'))
	
	# Plays the factory and stores its handle reference for later
	# keep=True keeps the handle in memory even if sound ended playing
	own['handle'] = device.play(factory, keep=True)
	
	# A negative loop_count makes the handle repeat infinitely
	own['handle'].loop_count = -1
	
	# Value to keep track of handle status
	own['handle_status'] = 'playing'

# When spacebar is pressed
if toggle_pause.positive:
	
	# Pause handle if status is playing
	if own['handle_status'] == 'playing':
		own['handle'].pause()
		own['handle_status'] = 'paused'
	
	# Resume handle if status is paused
	else:
		own['handle'].resume()
		own['handle_status'] = 'playing'
		
# Fast forward by 2 seconds
if forward.positive:
	own['handle'].position += 2
	
# Rewind by 2 seconds
if rewind.positive:
	own['handle'].position -= 2

Example file attached below.
ex_sound_seek.zip (172.6 KB)

2 Likes

I’ve tried using this in UPBGE but it doesn’t play the audio file. It works fine in normal BGE so something must be wrong.

edit: Nevermind, I didn’t change the names of the logic bricks. It works on UPBGE.