gpt4 book ai didi

python - 发生异常 : error mpg123_seek: Invalid RVA mode.(代码 12)

转载 作者:行者123 更新时间:2023-12-05 06:51:16 25 4
gpt4 key购买 nike

我正在使用 sounddevice 录制音频,我想通过 pygame 通过虚拟音频线播放它,我一直收到此错误 Exception has occurred: error mpg123_seek: Invalid RVA mode。 (代码 12)

我的代码如下:

import sounddevice as sd
from scipy.io.wavfile import write
import random
import pygame
import time

pygame.init()
pygame.mixer.init(devicename='CABLE Input (VB-Audio Virtual Cable)')

fs = 44100 # Sample rate
seconds = 00.1 # Duration of recording


def main():
for x in range(10000):
number = random.randint(1,9999999)


myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write(f'output/output{str(number)}.mp3', fs, myrecording) # Save as WAV file `

# PLAY MIC SOUND HERE
pygame.mixer.music.load(f'output/output{str(number)}.mp3') #Load the mp3
pygame.mixer.music.play() #Play it
time.sleep(00.1)

main()

感谢任何帮助。

最佳答案

有几个问题。

首先是 scipi.io.wavefile.write() 只写入未压缩的 WAV 文件(引用:https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html)。您可以将其命名为 .mp3,但它并不是以这种方式压缩的。

下一个问题是pygame.mixer.music 不会.load() 未压缩的WAV 文件。那么……怎么办……

一种解决方法是使用基础 pygame.mixer,它很乐意加载未压缩的 WAV。虽然我没有“电缆输入(VB-音频虚拟电缆)”设备,但我确实得到了一个很好的静音文件,我用声音编辑程序 Audacity 对其进行了验证,这似乎可以正常播放。

import sounddevice as sd
from scipy.io.wavfile import write
import pygame
import time
import random

pygame.init()
pygame.mixer.init(devicename='CABLE Input (VB-Audio Virtual Cable)')

fs = 44100 # Sample rate
seconds = 00.1 # Duration of recording


def main():
for x in range(10000):
number = random.randint(1,9999999)

myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished

filename = f'output/output{str(number)}.wav'
write(filename, fs, myrecording) # Save as uncompressed WAV file

# PLAY MIC SOUND HERE
print( "Playing [" + filename + "]" )

#pygame.mixer.music.load(filename) #Load the wav
#pygame.mixer.music.play() #Play it
#while ( pygame.mixer.music.get_busy() ): # wait for the sound to end
# time.sleep(00.1)

sound = pygame.mixer.Sound(filename) #Load the wav
sound.play() #Play it
while ( pygame.mixer.get_busy() ): # wait for the sound to end
time.sleep(00.1)

main()

关于python - 发生异常 : error mpg123_seek: Invalid RVA mode.(代码 12),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66201730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com