How do play audio (playsound) in background of Python script?

前端 未结 6 789
慢半拍i
慢半拍i 2020-12-03 09:36

I am just writing a small python game for fun and I have a function that does the beginning narrative.

I am trying to get the audio to play in the background but unf

相关标签:
6条回答
  • 2020-12-03 09:49
    from pygame import mixer
    
    mixer.music.init()
    mixer.music.load("audio.mp3") # Paste The audio file location 
    mixer.play() 
    
    0 讨论(0)
  • 2020-12-03 10:00

    Playsound has an option for running in the background -> additional argument to be set to 0:

    playsound("xxxxx.mp3", 0)
    
    0 讨论(0)
  • 2020-12-03 10:03

    In windows:

    Use winsound.SND_ASYNC to play them asynchronously

    import winsound
    winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )
    

    To stop playing

    winsound.PlaySound(None, winsound.SND_ASYNC)
    

    In mac or other platforms: You can try this Pygame/SDL

    pygame.mixer.init()
    pygame.mixer.music.load("file.mp3")
    pygame.mixer.music.play()
    
    0 讨论(0)
  • 2020-12-03 10:05

    Well you could just use pygame.mixer.music.play(x)

    #!/usr/bin/env python3
    # Any problems contact me on instagram @vulnerabilties
    import pygame
    
    pygame.mixer.init()
    pygame.mixer.music.load('filename.extention')
    pygame.mixer.music.play(999)
    # YOUR CODE HERE
    
    0 讨论(0)
  • 2020-12-03 10:12

    There is a library In pygame called mixer and you can add a mp3 file to the folder with the python script inside and put code like this inside:

    from pygame import mixer
    mixer.init()
    mixer.music.load("mysong.mp3")
    mixer.music.play()
    

    I hope you found my answer helpful

    0 讨论(0)
  • 2020-12-03 10:14

    Just change True to False (I use python 3.7.1)

    import playsound
    playsound.playsound('storm.mp3', False)
    print ('...')
    
    0 讨论(0)
提交回复
热议问题