且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

存储音频路径并在网页上播放

更新时间:2023-12-06 16:02:22

我将发布一个简单的示例,然后您可以将其扩展为所需的任何内容. 首先,您需要将音频文件复制到公共html目录中的某个文件夹,比方说音频. 之后,您需要创建一个数据库并打开与它的连接. 您可以在 PHP文档中对其进行阅读. 这是一个连接到数据库存储文件名然后获取它的php文件的示例. 数据库结构示例:

I will post a simple example and then you can extend it to whatever you need. First you need to copy the audio file to a folder in your public html directory, let's say audio. After that you need to create a database and open a connection to it. You can read about it in PHP Documentation. Here is an example of a php file that connects to a database store the filename and then gets it. Example of database structure:

id | audio_name 1 audio1.mp3

id | audio_name 1 audio1.mp3

<?php
$con = new mysqli("localhost", "user", "password", "database");
if ($con->connect_errno) {
    echo "Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error;
}

//We define a constant with the path to audio 
define('PATH','audio/');

//We store the audio file name in database
$result = $con->query("INSERT INTO table(audio_name) VALUES('audio1.mp3')");

//To get values from database you can do this
$audiomp3 = $con->query("SELECT audio_name FROM table WHERE audio_name = 'audio1.mp3'")->fetch_object()->audio_name;

//Full path to audio file
$audiomp3 = PATH . $audiomp3;

//And we create a audio element
$element = "";
$element .= "<audio controls>";
$element .= "<source src='$audiomp3' type='audio/ogg'>";
$element .= "Your browser does not support the audio element.";
$element .= "</audio>";

//Displaying audio element
echo $element;

请记住,这是一个基本示例!

Remember this is a base example!