如何在 Python 脚本的背景中播放音频(播放声音)?
How can I play audio (playsound) in the background of a Python script?
我只是为了好玩而写一个小 Python 游戏,我有一个函数可以进行开头叙述。
我正在尝试让音频在后台播放,但不幸的是,MP3 文件在功能继续之前先播放了。
如何在后台将其发送到 运行?
import playsound
def displayIntro():
playsound.playsound('storm.mp3',True)
print('')
print('')
print_slow('The year is 1845, you have just arrived home...')
另外,有没有办法控制 playsound 模块的音量?
我正在使用 Mac,而且我并不拘泥于使用 playsound。它似乎是我唯一可以工作的模块。
在Windows中:
使用winsound.SND_ASYNC异步播放:
import winsound
winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )
停止播放
winsound.PlaySound(None, winsound.SND_ASYNC)
在 Mac 或其他平台上:
你可以试试这个Pygame/SDL
pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()
嗯,你可以使用 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
in Pygame 中有一个名为 mixer 的库,您可以使用 Python 脚本里面,把这样的代码放在里面:
from pygame import mixer
mixer.init()
mixer.music.load("mysong.mp3")
mixer.music.play()
只需将 True
更改为 False
(我使用 Python 3.7.1)
import playsound
playsound.playsound('storm.mp3', False)
print ('...')
from pygame import mixer
mixer.music.init()
mixer.music.load("audio.mp3") # Paste The audio file location
mixer.play()
您可以试试just_playback. It's a wrapper I wrote around miniaudio,它在后台播放音频,同时提供暂停、恢复、搜索和设置播放音量等播放控制功能。
使用vlc
import vlc
player = None
def play(sound_file):
global player
if player is not None:
player.stop()
player = vlc.MediaPlayer("file://" + sound_file)
player.play()
一个很好的方法是使用 vlc。非常常见且受支持的库。
pip install python-vlc
然后编写代码
import vlc
#Then instantiate a MediaPlayer object
player = vlc.MediaPlayer("/path/to/song.mp3")
#And then use the play method
player.play()
#You can also use the methods pause() and stop if you need.
player.pause()
player.stop
#And for the super fancy thing you can even use a playlist :)
playlist = ['/path/to/song1.mp3', '/path/to/song2.mp3', '/path/to/song3.mp3']
for song in playlist:
player = vlc.MediaPlayer(song)
player.play()
我在不阻塞主线程的情况下使用了一个单独的线程来播放声音。它也适用于 Ubuntu。
from threading import Thread
from playsound import playsound
def play(path):
"""
Play sound file in a separate thread
(don't block current thread)
"""
def play_thread_function:
playsound(path)
play_thread = Thread(target=play_thread_function)
play_thread.start()
这是一个重现相似长度声音的解决方案。这个解决方案对我来说解决了错误:
The specified device is not open or recognized by MCI.
此处遵循 class 方法的语法,但即使没有 class,您也可以更改它以工作。
导入这些包:
import multiprocessing
import threading
import time
定义这个函数:
def _reproduce_sound_nb(self, str_, time_s):
def reproduce_and_kill(str_, time_sec=time_s):
p = multiprocessing.Process(target=playsound, args=(str_,))
p.start()
time.sleep(time_sec)
p.terminate()
threading.Thread(target=reproduce_and_kill, args=(str_, time_s), daemon=True).start()
主要program/method:
self._reproduce_sound_nb(sound_path, 3) # path to the sound, seconds after sound stop -> to manage the process end
我只是为了好玩而写一个小 Python 游戏,我有一个函数可以进行开头叙述。
我正在尝试让音频在后台播放,但不幸的是,MP3 文件在功能继续之前先播放了。
如何在后台将其发送到 运行?
import playsound
def displayIntro():
playsound.playsound('storm.mp3',True)
print('')
print('')
print_slow('The year is 1845, you have just arrived home...')
另外,有没有办法控制 playsound 模块的音量?
我正在使用 Mac,而且我并不拘泥于使用 playsound。它似乎是我唯一可以工作的模块。
在Windows中:
使用winsound.SND_ASYNC异步播放:
import winsound
winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )
停止播放
winsound.PlaySound(None, winsound.SND_ASYNC)
在 Mac 或其他平台上:
你可以试试这个Pygame/SDL
pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()
嗯,你可以使用 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
in Pygame 中有一个名为 mixer 的库,您可以使用 Python 脚本里面,把这样的代码放在里面:
from pygame import mixer
mixer.init()
mixer.music.load("mysong.mp3")
mixer.music.play()
只需将 True
更改为 False
(我使用 Python 3.7.1)
import playsound
playsound.playsound('storm.mp3', False)
print ('...')
from pygame import mixer
mixer.music.init()
mixer.music.load("audio.mp3") # Paste The audio file location
mixer.play()
您可以试试just_playback. It's a wrapper I wrote around miniaudio,它在后台播放音频,同时提供暂停、恢复、搜索和设置播放音量等播放控制功能。
使用vlc
import vlc
player = None
def play(sound_file):
global player
if player is not None:
player.stop()
player = vlc.MediaPlayer("file://" + sound_file)
player.play()
一个很好的方法是使用 vlc。非常常见且受支持的库。
pip install python-vlc
然后编写代码
import vlc
#Then instantiate a MediaPlayer object
player = vlc.MediaPlayer("/path/to/song.mp3")
#And then use the play method
player.play()
#You can also use the methods pause() and stop if you need.
player.pause()
player.stop
#And for the super fancy thing you can even use a playlist :)
playlist = ['/path/to/song1.mp3', '/path/to/song2.mp3', '/path/to/song3.mp3']
for song in playlist:
player = vlc.MediaPlayer(song)
player.play()
我在不阻塞主线程的情况下使用了一个单独的线程来播放声音。它也适用于 Ubuntu。
from threading import Thread
from playsound import playsound
def play(path):
"""
Play sound file in a separate thread
(don't block current thread)
"""
def play_thread_function:
playsound(path)
play_thread = Thread(target=play_thread_function)
play_thread.start()
这是一个重现相似长度声音的解决方案。这个解决方案对我来说解决了错误:
The specified device is not open or recognized by MCI.
此处遵循 class 方法的语法,但即使没有 class,您也可以更改它以工作。
导入这些包:
import multiprocessing
import threading
import time
定义这个函数:
def _reproduce_sound_nb(self, str_, time_s):
def reproduce_and_kill(str_, time_sec=time_s):
p = multiprocessing.Process(target=playsound, args=(str_,))
p.start()
time.sleep(time_sec)
p.terminate()
threading.Thread(target=reproduce_and_kill, args=(str_, time_s), daemon=True).start()
主要program/method:
self._reproduce_sound_nb(sound_path, 3) # path to the sound, seconds after sound stop -> to manage the process end