我将如何在pygame中平移声音? [英] How would I be able to pan a sound in pygame?

查看:54
本文介绍了我将如何在pygame中平移声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想在这里完成的是一种在游戏运行时平移声音的方法.我想根据播放器的位置改变音量(左右).现在,我有一个简单的代码,我认为这将是一个很好的测试:

Basically, what I want to accomplish here, is a way to pan the sound while the game is running. I'd like to make the volumes (left and right) change depending on the position of the player. For now I've got a simple code that I thought that would be a good test:

pygame.mixer.init()

self.sound = pygame.mixer.Sound("System Of A Down - DDevil #06.mp3")
print("I could get here")
self.music = self.sound.play()

self.music.set_volume(1.0, 0)

首先,我尝试了类似的操作,但是使用了 pygame.mixer.music ,但是我意识到无法以这种方式分别更改音量,或者我认为,然后我更改为这里显示的代码.现在看来该文件无法加载,我的猜测是该文件太大,无法在pygame中视为声音.对我将如何进行这项工作有任何想法吗?

First, I tried something similar but with pygame.mixer.music, but I came to realize that there's no way to change the volumes separately this way, or so I think, then I changed to the code presented here. Now it seems like the file is not able to be loaded, my guess is that the file is too big to be treated as a sound in pygame. Any idea of how I'd be able to make this work?

推荐答案

您可以像这样在频道上平移:

You can pan on the Channel like this:

from os import split, join
import pygame
import pygame.examples.aliens
pygame.init()
# get path to an example punch.wav file that comes with pygame.
sound_path = join(split(pygame.examples.aliens.__file__)[0], 'data', 'punch.wav')
sound = pygame.mixer.Sound(sound_path)
# mixer can mix several sounds together at once.
# Find a free channel to play the sound on.
channel = pygame.mixer.find_channel()
# pan volume full loudness on the left, and silent on right.
channel.set_volume(1.0, 0.0)
channel.play(sound)

https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Channel

这篇关于我将如何在pygame中平移声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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