如何让PHP输出声音(嘟嘟声)?

发布于 2024-08-14 15:25:49 字数 98 浏览 11 评论 0原文

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

import winsound
winsound.Beep(537, 2000)

What's the PHP verson of this python code?

import winsound
winsound.Beep(537, 2000)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小矜持 2024-08-21 15:25:49

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

如果您想进行 Win32 调用,请查看:How do我从 PHP 进行 Win32 API 调用?
还有 Win32 蜂鸣功能

但是如果您希望在用户浏览器上更好地将音频嵌入 HTML 本身。

编辑: 另一种仅发出蜂鸣声的方法

<?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;
  }
?>

运行时不会执行任何操作通过浏览器,如果通过 shell 运行,它将产生 $int_beeps 次的蜂鸣声。这应该适用于 Windows、Unix 等。

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.

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

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

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.

血之狂魔 2024-08-21 15:25:49

这适用于标准/内置蜂鸣声。 (更多的是“doink”声音)

也适用于任何平台。

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

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

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

Also works on any platform.

Super simple, copy-paste code.

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

转身泪倾城 2024-08-21 15:25:49

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

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

system('cmd /k go.bat')

如果我使用,我什么也得不到:

exec('cmd /k go.bat')

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

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

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

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

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

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.

天生の放荡 2024-08-21 15:25:49

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

老帖子,没有回答问题:

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

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

这看起来像:

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

现在您只需通过 exec() 或 system() 或其他方式从 PHP 调用 go.bat 即可。不过,您需要通过 cmd 创建 go.bat,以使 Ctrl+G 字符正确。

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

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.

悟红尘 2024-08-21 15:25:49

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

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

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

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

Install mpg321 - a tiny sound app:

exec("mpg321 --quiet --gain 10 /path/to/beep.mp3");
祁梦 2024-08-21 15:25:49

EZ way

EZ way????:

you can get help from html :

echo "<audio src='http://freesoundeffect.net/sites/default/files/incorrect-answer--6-sound-effect-99679566.mp3' autoplay ></audio>"; 

this code play beep sound in background and you can change .mp3 file

江城子 2024-08-21 15:25:49

这会产生蜂鸣声,但与winsound不同的是,它不允许您指定频率、持续时间..

function cli_beep()
{
    if (is_callable('ncurses_beep')) {
        ncurses_beep();
        ncurses_flash();
    } else {
        fprintf(STDOUT, "%s", "\x07");
    }
}

this generates a beep, but unlike winsound, does not allow you to specify frequency, duration..

function cli_beep()
{
    if (is_callable('ncurses_beep')) {
        ncurses_beep();
        ncurses_flash();
    } else {
        fprintf(STDOUT, "%s", "\x07");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文