本文介绍了在函数中使用IPython.display.audio在jupyter笔记本中播放音频无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码时,声音会播放:

When using the code below the sound plays:

import IPython.display as ipd
import numpy

sr = 22050 # sample rate
T = 0.5    # seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t)              # pure sine wave at 440 Hz
ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array

但是当我在函数中使用它时,它将停止工作:

But when I use it inside a function it stops working:

import IPython.display as ipd
import numpy

def SoundNotification():
    sr = 22050 # sample rate
    T = 0.5    # seconds
    t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
    x = 0.5*numpy.sin(2*numpy.pi*440*t)              # pure sine wave at 440 Hz
    ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array

SoundNotification()

我尝试将音频分配给一个变量,然后将其返回,该变量可以正常工作:

I've tried to assign the audio to a variable and return it which works:

import IPython.display as ipd
import numpy

def SoundNotification():
    sr = 22050 # sample rate
    T = 0.5    # seconds
    t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
    x = 0.5*numpy.sin(2*numpy.pi*440*t)              # pure sine wave at 440 Hz
    sound = ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
    return sound
sound = SoundNotification()
sound

但是我想在其他功能中使用声音:

But I want to use the sound in a different function:

import IPython.display as ipd
import numpy

def SoundNotification():
    sr = 22050 # sample rate
    T = 0.5    # seconds
    t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
    x = 0.5*numpy.sin(2*numpy.pi*440*t)              # pure sine wave at 440 Hz
    sound = ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
    return sound

def WhereIWantToUseTheSound():    
    sound = SoundNotification()
    sound

WhereIWantToUseTheSound()

我该如何进行这项工作以及导致这种现象的原因是什么?笔记本的内核是Python 3.

How do I make this work and what causes this behavior?The kernel for the notebook is Python 3.

我想在预定的事件中播放声音:

I want to play the sound in a scheduled event:

import IPython.display as ipd
import numpy
import sched, time

sound = []
def SoundNotification():
    sr = 22050 # sample rate
    T = 0.5    # seconds
    t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
    x = 0.5*numpy.sin(2*numpy.pi*440*t)              # pure sine wave at 440 Hz
    sound = ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
    return sound


def do_something(sc): 
    print("Doing stuff...")
    # do your stuff
    sound_ = SoundNotification()
    s.enter(interval, 1, do_something, (sc,))
    return sound_


s = sched.scheduler(time.time, time.sleep)
interval = int(input("Interval between captures in seconds: "))
s.enter(0, 1, do_something, (s,))
s.run()

我不知道如何在相同的函数中返回声音并安排下一个事件.

I don't know how to return the sound and schedule the next event within the same function.

推荐答案

我遇到了同样的问题,当我打电话时播放了声音:

I was having this same problem, the sound was played when I called:

from IPython.display import Audio 
Audio('/path/beep.mp3', autoplay=True)

但是当它在函数内部时,它不起作用.问题在于该函数调用并没有真正播放声音,它实际上是由返回到Jupyter输出的结果HTML播放的.

But it didn't work when it was inside a function. The problem is that the function call doesn't really play the sound, it's actually played by the resulting HTML that is returned to Jupyter output.

因此,为了克服这个问题,您可以使用IPython中的display()函数强制该函数呈现HTML.这将起作用:

So to overcome this, you can force the function to render the HTML using display( ) function from IPython. This will work:

from IPython.display import Audio 
from IPython.core.display import display
def beep():
    display(Audio('/path/beep.mp3', autoplay=True))
beep();

这篇关于在函数中使用IPython.display.audio在jupyter笔记本中播放音频无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 07:10