Search code examples
pythonmultithreadingaudiogoogle-colaboratorypython-multithreading

Python - Playing audio using multithreading in google colab


I have been trying to play audio in different threads but it is not happening on google Colab. Please help me out. I am not getting any errors in the code.

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()

Solution

  • Colaboratoty was only meant for console purposes, and if there are speakers or other peripherals connected to the servers, then your audio would have been played there... Not on your browser! But there are 2 ways.

    • You need to edit the javascript file of Colaboratoty in inspect element panel or
    • Use python codes to play sound..

    For now I will give you the second solution because as you are a python developer and if not proficient in javascript, might face some problem. You can find your solution here : https://colab.research.google.com/drive/1--xY78_ZTFwpI7F2ZfaeyFKiAOG2nkwd

    Good luck!