发生异常:错误mpg123\U seek:RVA模式无效。(代码12)

2024-05-17 02:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用sounddevice录制音频,我想通过pygame通过虚拟音频电缆播放,我一直收到此错误Exception has occurred: error mpg123_seek: Invalid RVA mode. (code 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()

感谢您的帮助


Tags: importnumberoutputtimeinitasrandomsd
1条回答
网友
1楼 · 发布于 2024-05-17 02:35:01

有几个问题

第一个是scipi.io.wavefile.write()只写入未压缩的WAV文件(ref:https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html)。您可以将其命名为.mp3,但它不是那样压缩的

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

一种解决方法是使用base pygame.mixer,它很乐意加载未压缩的WAV。虽然我没有“CABLE Input(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()

相关问题 更多 >