问题描述
我一直试图在不同线程中播放音频,但Google Colab上没有发生这种情况。 请帮帮我。我的代码没有任何错误。
import os
import threading
from threading import *
import time
from google.colab import drive
from playsound import playsound
def play1():
playsound("/content/drive/My Drive/colabTestData/SoundHelixBackground.mp3")
time.sleep(2)
def play2():
playsound("/content/drive/My Drive/colabTestData/SoundHelixBackground.mp3")
t1=Thread(target=play1)
t2=Thread(target=play2)
t1.start()
t2.start()
解决方法
Colaboratoty仅用于控制台,如果服务器上连接了扬声器或其他外围设备,则您的音频将在那里播放...不在浏览器上!但是有两种方法。
- 您需要在检查元素面板中编辑Colaboratoty的javascript文件,或者
- 使用python代码播放声音。
现在,我将为您提供第二种解决方案,因为您是python开发人员,并且如果不精通javascript,可能会遇到一些问题。 您可以在这里找到解决方案:https://colab.research.google.com/drive/1--xY78_ZTFwpI7F2ZfaeyFKiAOG2nkwd
祝你好运!