【问题标题】:How can I make my audio loop with pyaudio?如何使用 pyaudio 制作音频循环?
【发布时间】:2021-04-11 14:11:21
【问题描述】:

首先,我对这个库还很陌生,而且我并不是很了解所有内容。在互联网的帮助下,我设法让这个代码 sn-p 工作。此代码基本上播放音频文件(具体为.wav)。问题是它只播放一次。我希望音频文件循环播放,直到我将 is_looping 变量设置为 False。

import pyaudio
import wave


class AudioFile:
    chunk = 1024

    def __init__(self, file_dir):
        """ Init audio stream """
        self.wf = wave.open(file_dir, 'rb')
        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(
            format=self.p.get_format_from_width(self.wf.getsampwidth()),
            channels=self.wf.getnchannels(),
            rate=self.wf.getframerate(),
            output=True
        )

    def play(self):
        """ Play entire file """
        data = self.wf.readframes(self.chunk)
        while data != '':
            self.stream.write(data)
            data = self.wf.readframes(self.chunk)

    def close(self):
        """ Graceful shutdown """
        self.stream.close()
        self.p.terminate()

is_looping = True
audio = AudioFile("___.wav")
audio.play()
audio.close()

我试过做这样的事情,但还是不行:

is_looping = True
audio = AudioFile("___.wav")
while is_looping:
    audio.play()
audio.close()

【问题讨论】:

标签: python python-3.x pyaudio


【解决方案1】:

我找不到使用我的代码循环播放音频的方法,但我在互联网上找到了一个完全符合我要求的代码。这是链接:https://gist.github.com/THeK3nger/3624478

这是该链接中的代码:

import os
import wave
import threading
import sys

# PyAudio Library
import pyaudio

class WavePlayerLoop(threading.Thread):
    CHUNK = 1024

    def __init__(self, filepath, loop=True):
        """
        Initialize `WavePlayerLoop` class.
        PARAM:
            -- filepath (String) : File Path to wave file.
            -- loop (boolean)    : True if you want loop playback.
                                   False otherwise.
        """
        super(WavePlayerLoop, self).__init__()
        self.filepath = os.path.abspath(filepath)
        self.loop = loop

    def run(self):
        # Open Wave File and start play!
        wf = wave.open(self.filepath, 'rb')
        player = pyaudio.PyAudio()

        # Open Output Stream (based on PyAudio tutorial)
        stream = player.open(format=player.get_format_from_width(wf.getsampwidth()),
                             channels=wf.getnchannels(),
                             rate=wf.getframerate(),
                             output=True)

        # PLAYBACK LOOP
        data = wf.readframes(self.CHUNK)
        while self.loop:
            stream.write(data)
            data = wf.readframes(self.CHUNK)
            if data == b'':  # If file is over then rewind.
                wf.rewind()
                data = wf.readframes(self.CHUNK)

        stream.close()
        player.terminate()

    def play(self):
        """
        Just another name for self.start()
        """
        self.start()

    def stop(self):
        """
        Stop playback.
        """
        self.loop = False

你只需要在类之外添加这样的东西,它应该可以工作:

player = WavePlayerLoop("sounds/1.wav")
player.play()

【讨论】:

  • 如果您阅读我对原始问题的评论,您会看到我提到了rewind 方法,这是此代码与您的代码之间的重要区别。
  • 哦,好吧,我明白你的意思了。是的,你是对的。
猜你喜欢
  • 2018-05-10
  • 1970-01-01
  • 2012-10-11
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多