python 如何让 PHP 输出声音(哔)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1919826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:21:12  来源:igfitidea点击:

How to make PHP output a sound (beep)?

phppythonbeepsynthesizer

提问by user198729

What's the PHP verson of this python code?

这个python代码的PHP版本是什么?

import winsound
winsound.Beep(537, 2000)

回答by Priyank Bolia

php is mostly used on webservers, so what the use beeping there, and you can't beep on user computer through php, as php is translated into HTML, which has no such method.

php主要是用在网络服务器上,所以在那里哔哔有什么用,而且你不能通过php在用户计算机上哔哔,因为php被翻译成HTML,没有这种方法。

If you want to have Win32 calls have a look at: How do I make Win32 API calls from PHP?also the Win32 Beep Function

如果您想进行 Win32 调用,请查看:如何从 PHP 进行 Win32 API 调用?还有Win32 Beep 功能

But if you want to have beep sound on user browser better embed audio into the HTMLitself.

但是,如果您想在用户浏览器上发出哔哔声,最好将音频嵌入 HTML本身。

Edit: Another method for just the beep:

编辑:另一种只发出哔声的方法

<?php
  function beep ($int_beeps = 1) {
    for ($i = 0; $i < $int_beeps; $i++): $string_beeps .= "\x07"; endfor;
    isset ($_SERVER['SERVER_PROTOCOL']) ? false : print $string_beeps;
  }
?>

This will not do anything when running through a browser, if running through a shell it will produce an audible beep $int_beeps times. This should work on Windows, Unix, etc.

这在通过浏览器运行时不会做任何事情,如果通过外壳运行,它将产生可听见的哔声 $int_beeps 次。这应该适用于 Windows、Unix 等。

回答by daveloop

I tried what Tor Valamo suggested, but I still couldn't get the sound to play.

我尝试了 Tor Valamo 的建议,但仍然无法播放声音。

I would simply get a representation of the chr(7) on my screen but no sound when I used:

我只会在屏幕上获得 chr(7) 的表示,但在使用时没有声音:

system('cmd /k go.bat')

And I would get nothing at all if I used:

如果我使用:

exec('cmd /k go.bat')

Instead i used either of:

相反,我使用了以下任一项:

exec('start /MIN go.bat')
exec('cmd.exe /k start /MIN go.bat')

the only side effect is that a cmd.exe does flash, so the /MIN ensures that it only flashes to the taskbar.

唯一的副作用是 cmd.exe 会闪烁,因此 /MIN 确保它只闪烁到任务栏。

回答by Tor Valamo

Update: Never mind, I thought you just wanted a 'beep', not a TONE.

更新:没关系,我以为你只是想要“哔”声,而不是音调。

Old post, not answering the question:

旧帖子,不回答问题:

You'd need to make a .bat file, so: Open cmd

您需要制作一个 .bat 文件,因此:打开 cmd

copy con go.bat [Enter]
@echo off [Enter]
echo [Ctrl+G] [Enter]
[Ctrl+Z] [Enter]

This looks like:

这看起来像:

C:\DEV\test>copy con go.bat
@echo off
echo ^G
^Z
    1 file(s) copied.

Now you just call go.bat from PHP through exec() or system() or something. You need to make go.bat through cmd though, in order for the Ctrl+G character to be correct.

现在你只需通过 exec() 或 system() 或其他东西从 PHP 调用 go.bat 。不过,您需要通过 cmd 生成 go.bat,以便 Ctrl+G 字符正确。

回答by Dave Kimble

Of course people write GUI apps in PHP - that's what wxPHP is for.

当然,人们用 PHP 编写 GUI 应用程序 - 这就是 wxPHP 的用途。

Install mpg321 - a tiny sound app:

安装 mpg321 - 一个微小的声音应用程序:

exec("mpg321 --quiet --gain 10 /path/to/beep.mp3");

回答by Mason

This one works with the standard/built-in beep sound. (more of a 'doink' sound)

这个与标准/内置蜂鸣声一起使用。(更像是“doink”的声音)

Also works on any platform.

也适用于任何平台。

Super simple, copy-paste code.

超级简单,复制粘贴代码。

function beep()
{
    fprintf ( STDOUT, "%s", "\x07" );
}