Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to play Sound with php

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to play sound in php". In daily operation, I believe many people have doubts about how to play sound in php. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to play sound in php". Next, please follow the editor to study!

Php to achieve the method of playing sound: 1, create a PHP example file; 2, read the database through PHP; 3, create a HTML sample file and use H5 audio () to play the prompt tone.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

How does php play sound?

PHP uses H5 audio to achieve a sound prompt similar to Meituan's order, and the web page plays music effects.

We often do the function of internal message and message reminder, so how to add a cue tone on this basis? Today, I would like to share with you the whole process of function implementation.

First of all, we need to lay out the good news prompt style, here you can layout according to your own needs.

Message tip bell style .png

Once we have done the style of the message prompt, we need to start doing timers and asynchronously querying data.

$(function () {var set = setInterval (function () {$.post ("{: url ('prompt')}",'', function (data) {if (data.status) {$('# num') .html (data.num); / / add the number of messages to var audio = new Audio (". / song.ogg") / / to obtain local audio files, ogg,mp3 can audio.play (); / / perform playback operation}}, 'json');}, 1000) / / Click to view carefully and empty the number of messages $('# num' Linklink') .click (function () {$('# link) .html (';})})

Here we use the audio () of H5 to play the tone. My code here is very rough, so you can optimize it according to your own needs.

Next, we use PHP language to read new messages in the database, start pushing if there is, and ignore it if not. I'm based on TP5.1.

Public function prompt () {/ / whether there is a new order here. If so, ignore $num = Db::name ('hexiao')-> where (' add_time','>', date ('Y-m-d Hlav, time ()-10))-> count (); if ($num > 0) {return json ([' status' = > 1, 'num' = > $num]) } else {return json (['status' = > 0,' num' = > $num]);}}

The following describes the audio tag of H5, which currently supports MP3, Ogg and Wav.

Your browser does not support audio elements.

Execution effect

Execution effect. PNG

Playback and pause and replay can also be controlled.

Replay

You can also control audio playback and pause through js.

Function repeat () {var audio = document.getElementById ('music'); audio.currentTime = 0 lead / replay} function control () {var audio = document.getElementById (' music'); if {if (audio.paused) {audio.play (); / / play} else {audio.pause () / / pause} at this point, the study on "how to play sound by php" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report