在 MouseOver 上的右扬声器或左扬声器上播放声音
Playing sound on right or left Speaker on MouseOver
我正在尝试使用 PyQt5 在 python 中制作小程序。
该程序将有两个按钮,中间有一个标签。当鼠标经过标签时,我想调用 def,以更改按钮的颜色并从特定扬声器(左或右)播放声音
我尝试了 pygame,关注了一些帖子,但一无所获。双声道都在播放声音。
import time
import pygame
pygame.mixer.init(44100, -16,2,2048)
channel1 = pygame.mixer.Channel(0) # argument must be int
channel2 = pygame.mixer.Channel(1)
print('OkkK')
soundObj = pygame.mixer.Sound('Aloe Blacc - Wake Me Up.wav')
channel2.play(soundObj)
soundObj.set_volume(0.2)
time.sleep(6) # wait and let the sound play for 6 second
soundObj.stop()
有没有办法解决这个问题并选择左右扬声器?
另外,有没有办法在鼠标悬停在标签上时调用 def?
一般情况下使用 pygame
时,最好调用 pygame.init()
来初始化所有 pygame
模块,而不是单独键入 pygame.
module
.init()
。它将节省您的时间和代码行数。
然后,播放pygame
中的声音文件,我一般使用pygame.mixer.Sound
获取文件,然后调用声音对象的play()
函数。
所以下面导入一个声音文件,然后根据鼠标X位置
播放和平移
import pygame
from pygame.locals import *
pygame.init() # init all the modules
sound = pygame.sound.Sound('Aloe Blacc - Wake Me Up.wav')) # import the sound file
sound_played = False
# sound has not been played, so calling set_volume() will return an error
screen = pygame.display.set_mode((640, 480)) # make a screen
running = True
while running: # main loop
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN: # play the sound file
channel = sound.play()
sound_played = True
# start setting the volume now, from this moment where channel is defined
# calculate the pan
pan = pygame.mouse.get_pos()[0] / pygame.display.get_surface().get_size()[0]
left = pan
right = 1 - pan
# pan the sound if the sound has been started to play
if sound_played:
channel.set_volume(left, right)
pygame.display.flip()
我正在尝试使用 PyQt5 在 python 中制作小程序。 该程序将有两个按钮,中间有一个标签。当鼠标经过标签时,我想调用 def,以更改按钮的颜色并从特定扬声器(左或右)播放声音
我尝试了 pygame,关注了一些帖子,但一无所获。双声道都在播放声音。
import time
import pygame
pygame.mixer.init(44100, -16,2,2048)
channel1 = pygame.mixer.Channel(0) # argument must be int
channel2 = pygame.mixer.Channel(1)
print('OkkK')
soundObj = pygame.mixer.Sound('Aloe Blacc - Wake Me Up.wav')
channel2.play(soundObj)
soundObj.set_volume(0.2)
time.sleep(6) # wait and let the sound play for 6 second
soundObj.stop()
有没有办法解决这个问题并选择左右扬声器?
另外,有没有办法在鼠标悬停在标签上时调用 def?
一般情况下使用 pygame
时,最好调用 pygame.init()
来初始化所有 pygame
模块,而不是单独键入 pygame.
module
.init()
。它将节省您的时间和代码行数。
然后,播放pygame
中的声音文件,我一般使用pygame.mixer.Sound
获取文件,然后调用声音对象的play()
函数。
所以下面导入一个声音文件,然后根据鼠标X位置
播放和平移import pygame
from pygame.locals import *
pygame.init() # init all the modules
sound = pygame.sound.Sound('Aloe Blacc - Wake Me Up.wav')) # import the sound file
sound_played = False
# sound has not been played, so calling set_volume() will return an error
screen = pygame.display.set_mode((640, 480)) # make a screen
running = True
while running: # main loop
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN: # play the sound file
channel = sound.play()
sound_played = True
# start setting the volume now, from this moment where channel is defined
# calculate the pan
pan = pygame.mouse.get_pos()[0] / pygame.display.get_surface().get_size()[0]
left = pan
right = 1 - pan
# pan the sound if the sound has been started to play
if sound_played:
channel.set_volume(left, right)
pygame.display.flip()