如何在 Python 脚本的后台播放音频(播放声音)? [英] How can I play audio (playsound) in the background of a Python script?

查看:46
本文介绍了如何在 Python 脚本的后台播放音频(播放声音)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是为了好玩而编写一个小型 Python 游戏,我有一个函数可以进行开头的叙述.

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

我试图让音频在后台播放,但不幸的是 MP3 文件在功能继续之前先播放.

I am trying to get the audio to play in the background, but unfortunately the MP3 file plays first before the function continues.

如何让它在后台运行?

import playsound

def displayIntro():
    playsound.playsound('storm.mp3',True)
    print('')
    print('')
    print_slow('The year is 1845, you have just arrived home...')

另外,有没有办法控制playsound模块的音量?

Also, is there a way of controlling the volume of the playsound module?

我使用的是 Mac,并且我不习惯使用 PlaySound.它似乎是我唯一可以工作的模块.

I am using a Mac, and I am not wedded to using playsound. It just seems to be the only module that I can get working.

推荐答案

在 Windows 中:

In Windows:

使用 winsound.SND_ASYNC 异步播放它们:

Use winsound.SND_ASYNC to play them asynchronously:

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

停止播放

winsound.PlaySound(None, winsound.SND_ASYNC)

在 Mac 或其他平台上:

On Mac or other platforms:

你可以试试这个Pygame/SDL

pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()

这篇关于如何在 Python 脚本的后台播放音频(播放声音)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆