简体   繁体   English

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

[英]How to make PHP output a sound (beep)?

What's the PHP verson of this python code?这个python代码的PHP版本是什么?

import winsound
winsound.Beep(537, 2000)

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?如果您想进行 Win32 调用,请查看: 如何从 PHP 进行 Win32 API 调用? also the Win32 Beep Function还有Win32 Beep 功能

But if you want to have beep sound on user browser better embed audio into the HTML itself.但是,如果您想在用户浏览器上发出哔哔声,最好将音频嵌入 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.这在通过浏览器运行时不会做任何事情,如果通过外壳运行,它将产生可听见的哔哔声 $int_beeps 次。 This should work on Windows, Unix, etc.这应该适用于 Windows、Unix 等。

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 确保它只闪烁到任务栏。

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.现在你只需通过 exec() 或 system() 或其他东西从 PHP 调用 go.bat 。 You need to make go.bat through cmd though, in order for the Ctrl+G character to be correct.不过,您需要通过 cmd 生成 go.bat,以便 Ctrl+G 字符正确。

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" );
}

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");

EZ way😊: EZ方式😊:

you can get help form html :您可以获得帮助表单 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此代码在后台播放哔声,您可以更改 .mp3 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM