存储音频路径并在网页上播放 [英] Storing audio paths and playing it on web page

查看:265
本文介绍了存储音频路径并在网页上播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究的内容涉及要在网页上播放的许多简短音频片段. 我知道播放音频文件的最佳方法是使用音频标签,并将音频文件存储在本地存储中并在数据库中调用路径.

What I'm working on involves many short audio clips to be played on the webpage. I know that the best way to play an audio file is to use an audio tag, and also storing the audio files in a local storage and calling the path in the database.

我的主要问题是,如何将音频文件路径存储在数据库列中,我必须怎么做才能播放文件?

My main question is that how do I store the audio file path in the database column and what do I have to do to be able to play the file?

此外,我不确定与数据库的文件路径交互如何工作,有人能启发我吗?

Also, I am not sure how does that file path interaction with database works, can anyone enlighten me?

这也是因为我正在做类似拼写蜂的操作,因此会出现一个播放按钮或用户可以单击的文本,并且在问题旁边会听到声音,然后他们必须键入答案.

This is also because I am doing something like a spelling bee whereby there will be a play button or a text that the user can click and a sound will be heard which is next to the question and then they will have to type their answer.

现在对我来说重要的是使音频功能正常工作.

What matters for me now is in making the audio feature work.

任何输入将不胜感激.

Any input will be GREATLY appreciated.

推荐答案

我将发布一个简单的示例,然后您可以将其扩展为所需的任何内容. 首先,您需要将音频文件复制到公共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!

这篇关于存储音频路径并在网页上播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆