0

I found the library for music in replit python -- from replit import audio. However, it only works by itself. That is, it cannot run code along with it.

It runs like this:

audio.play_file("music.mp3")
time.sleep(100)

However, the sleep command, which is required for it to run (otherwise it'll "skip" the command as it runs for 0 seconds), means that no code can be run during this time. I tried to use thread but it returns an error instead when I put it in a function and call it with thread.

Working code (music only):

from replit import audio
import time

audio.play_file("tlc.mp3")
time.sleep(100)

Error code:

from replit import audio
import time
from threading import Thread

def func1():
    audio.play_file("tlc.mp3")
    time.sleep(100)

def func2():
    print('working.')
    # do other things I want to do

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()

It simply prints "working." with half a page of error message and no music appears. Any help would be appreciated.

enter image description here

Some background info: I want to make a console-based game with music in the background but cannot get it to work.

CELLSecret
  • 139
  • 1
  • 9
  • [replit forum post with simliar error message](https://replit.com/talk/ask/Replit-audio-not-working/122298) possibly helpful or related – chickity china chinese chicken Oct 06 '21 at 03:18
  • Maybe have a look at [pygame](https://www.pygame.org/news) that is able to run audio and code in parallel. – mozway Oct 06 '21 at 03:27
  • @chickitychinachinesechicken The correct answer there is that the file is too short but my file is 234 seconds long and I'm only playing the first 100. – CELLSecret Oct 06 '21 at 05:12
  • good point @CELLSecret, other posts on that forum mention [the music files may be to large to play immediately, hence the time out.](https://replit.com/talk/ask/Audio-was-not-created-within-2-seconds/116016) and other issues. It may be helpful to search that forum. or possibly it will be answered here. – chickity china chinese chicken Oct 06 '21 at 05:55
  • @mozway I tried, but it worked for me (without errors) just *there's no music* either :( – CELLSecret Oct 06 '21 at 06:30

0 Answers0