Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中存储声音?_Python_Python 3.x_Audio_Pygame_Pyaudio - Fatal编程技术网

如何在Python中存储声音?

如何在Python中存储声音?,python,python-3.x,audio,pygame,pyaudio,Python,Python 3.x,Audio,Pygame,Pyaudio,我在试着做一个拍子。我用这样一种方式来编程,一个踢在a的压力下,一个圈套在s的压力下。如果我按a-s-a-s来做一个基本的节拍,有没有办法保存发出的声音 import pygame pygame.init() screen = pygame.display.set_mode((800,800)) while True: screen.fill((255,255,255)) for event in pygame.event.get(): if event.t

我在试着做一个拍子。我用这样一种方式来编程,一个踢在a的压力下,一个圈套在s的压力下。如果我按a-s-a-s来做一个基本的节拍,有没有办法保存发出的声音

import pygame

pygame.init()


screen = pygame.display.set_mode((800,800))
while True:
    screen.fill((255,255,255))
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pygame.mixer.music.load('Kick1.mp3')
                pygame.mixer.music.play()
            if event.key == pygame.K_s:
                pygame.mixer.music.load('Snare1.mp3')
                pygame.mixer.music.play()
            if event.key == pygame.K_w:
                pygame.quit()
                quit()
    pygame.display.update()


pygame.display.set_caption('Beat pad')

这是我用来读写声音的代码:

f = open ('Kick1.mp3', 'rb')
file = f.read()
f.close()

f = open ('Snare1.mp3', 'rb')
file1 = f.read()


x = open ('Kick3.mp3', 'ab')
x.write(file)
x.write(file1)
因为它是mp3文件,所以不能像文本文件那样读取,也不能像文本文件那样附加/写入。应该使用'rb'将其读取为二进制文件,并使用'ab'将其附加为二进制文件


在这里,两个不同文件的声音,即Kick1.mp3和Snare1.mp3,将作为一个声音存储在一个文件“Kick3.mp3”中(因为我正在附加而不是写入它)。Kick3.mp3不会退出,因此它将由python创建,声音将存储在其中。

您想合并声音并将其保存为单个声音文件吗?是的,我刚刚找到了。。我现在发布答案