python - 如何使用 python 3 在 OSX 中发出声音

标签 python macos

所以我试图在我的代码完成后播放一个简单的哔哔声,因为处理大量数据需要一段时间。我在网上尝试了其他示例,但似乎没有任何效果。我试过了:

import sys
sys.stdout.write('\a')
sys.stdout.flush()

它在 IDE shell 中输出单词 bel,但没有别的

我也试过:
import os
print('\a')

输出完全相同的东西。

我试过了
import os
os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % (   4, 4))

它只是输出:
sh: play: command not found

有人有任何想法吗?

这里还有一个包含 bell 一词的 jpg。看起来很有趣...
Image of 'Bel'

我还确保我的音量调高了

更新:它可能只是我的 IDE。我尝试直接从终端启动 python 并使用 write('\a') 并且它起作用了。它只是在 101 翼不起作用

最佳答案

实际上,sys.stdout.write('\a') 对我有用,但在 IDE 中无效,尝试在 终端 中运行此代码。您将听到系统声音。

你也可以试试这两个命令:

  • say
  • afplay


  • 例如
    import os
    os.system('say "Beer time."')
    
    import os
    os.system('afplay /System/Library/Sounds/Sosumi.aiff')
    

    运行 man say 以查看更多详细信息。

    say Hello, World.
    say -v Alex -o hi -f hello_world.txt.
    say --interactive=/green spending each day the color of the leaves.
    say -o hi.aac 'Hello, [[slnc 200]] World'.
    say -o hi.m4a --data-format=alac Hello, World.



    希望这可以帮助。

    关于python - 如何使用 python 3 在 OSX 中发出声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42150309/

    相关文章:

    macos - 在 OS X 中如何等待应用程序关闭?

    c# - 在锁定的 Windows 10 机器上的后台进程中将文本放入剪贴板

    python - 在Python中,是否有一个字符串s使得float(s)为None?

    python - 安装 PlaidML Keras

    c++ - 两级嵌套的 C++ 类适用于 GCC,但不适用于 Clang

    objective-c - cocoa WebView : can I load web and local resources together in the same page?

    python - 在公共(public) git repo 上对 python 应用程序进行版本控制时保护私有(private) API key 的正确方法

    python - 如何让 Django 应用程序导入另一个 Django 应用程序或模块?

    PHP 和 "turning on"mysqli

    Imagemagick 在 mac os x 山狮上找不到 .tiff 格式的代表库