JS高手帮我写一个背景音乐播放代码
点击播放将会播放背景音乐,默认播放背景音乐播放一遍就停止播放途中点击暂停将暂停播放,点击开始继续播放也就是只有两个按钮“播放”、“暂停”...
点击播放将会播放背景音乐,默认播放
背景音乐播放一遍就停止
播放途中点击暂停将暂停播放,点击开始继续播放
也就是只有两个按钮“播放”、“暂停” 展开
背景音乐播放一遍就停止
播放途中点击暂停将暂停播放,点击开始继续播放
也就是只有两个按钮“播放”、“暂停” 展开
3个回答
展开全部
第一段:
<embed src="等一分钟.mp3" id="aa">
<input type=button value=暂停 onclick="aa.pause();">
<input type=button value=播放 onclick="aa.play();">
第二段:
<audio id="aaa" src="等一分钟.mp3" autoplay controls></audio>
<input type=button value=暂停 onclick="aaa.pause();">
<input type=button value=播放 onclick="aaa.play();">
IE:支持第一段代码,不支持第二段代码
Firefox:支持第二段代码,第一段代码的第一行需要装插件,不支持第一段的后两行
Chrom:支持第二段代码,不支持第一段代码中的后两行
Opera,Safari:第一段代码的第一行需要装插件,不支持第一段的后两行,不支持第二段代码
更多追问追答
追问
我用IE测试第一段。没效果呀?
追答
src="等一分钟.mp3" 这句 要求 在和你写的html同一目录下,有“等一分钟.mp3”这个文件。就是<embed>标签和<audio>标签中的src属性 是为标签指定了资源的路径,然后浏览器解析标签时会根据这个路径去读取指定的文件,还有测试第二段代码的时候,标签中的“autoplay controls”两个值不能丢,不然Chrom直接略过该标签,不会解析。
刚才我自己试了一下,的确没反应,原因是中文在页面显示乱码了,由于歌曲名是中文,乱码后就找不到目标文件了。给你弄成压缩包了,解压用IE打开html文件,仔细阅读一下就明白了。
博思aippt
2024-07-20 广告
2024-07-20 广告
**AI一键生成PPT免费版**为满足广大用户的需求,我们博思云创科技特推出AI一键生成PPT免费版。用户只需简单输入需求,AI技术便能智能分析并快速生成高质量PPT。此版本功能强大且易于操作,无需专业设计技能,即可轻松打造出令人满意的演示...
点击进入详情页
本回答由博思aippt提供
展开全部
你可以去酷狗、虾米这些地方获取播放器代码的,也不用音乐文件比较省事
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<!DOCTYPE html>
<html>
<head>
<title>HTML5 Audio Player </title>
<!-- Uncomment the following meta tag if you have issues rendering this page on an intranet site. -->
<!-- <meta http-equiv="X-UA-Compatible" content="IE=9"/> -->
<script type="text/javascript">
// Global variable to track current file name.
var currentFile = "";
function playAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById('myaudio');
var btn = document.getElementById('play');
var audioURL = document.getElementById('audiofile');
//Skip loading if current file hasn't changed.
if (audioURL.value !== currentFile) {
oAudio.src = audioURL.value;
currentFile = audioURL.value;
}
// Tests the paused attribute and set state.
if (oAudio.paused) {
oAudio.play();
btn.textContent = "Pause";
}
else {
oAudio.pause();
btn.textContent = "Play";
}
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error("Error:" + e));
}
}
}
// Rewinds the audio file by 30 seconds.
function rewindAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById('myaudio');
oAudio.currentTime -= 30.0;
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error("Error:" + e));
}
}
}
// Fast forwards the audio file by 30 seconds.
function forwardAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById('myaudio');
oAudio.currentTime += 30.0;
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error("Error:" + e));
}
}
}
// Restart the audio file to the beginning.
function restartAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById('myaudio');
oAudio.currentTime = 0;
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error("Error:" + e));
}
}
}
</script>
</head>
<body>
<p>
<input type="text" id="audiofile" size="80" value="等一分钟.mp3" />
</p>
<audio id="myaudio">
HTML5 audio not supported
</audio>
<button id="play" onclick="playAudio();">
Play
</button>
<button onclick="rewindAudio();">
Rewind
</button>
<button onclick="forwardAudio();">
Fast forward
</button>
<button onclick="restartAudio();">
Restart
</button>
</body>
</html>
<html>
<head>
<title>HTML5 Audio Player </title>
<!-- Uncomment the following meta tag if you have issues rendering this page on an intranet site. -->
<!-- <meta http-equiv="X-UA-Compatible" content="IE=9"/> -->
<script type="text/javascript">
// Global variable to track current file name.
var currentFile = "";
function playAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById('myaudio');
var btn = document.getElementById('play');
var audioURL = document.getElementById('audiofile');
//Skip loading if current file hasn't changed.
if (audioURL.value !== currentFile) {
oAudio.src = audioURL.value;
currentFile = audioURL.value;
}
// Tests the paused attribute and set state.
if (oAudio.paused) {
oAudio.play();
btn.textContent = "Pause";
}
else {
oAudio.pause();
btn.textContent = "Play";
}
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error("Error:" + e));
}
}
}
// Rewinds the audio file by 30 seconds.
function rewindAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById('myaudio');
oAudio.currentTime -= 30.0;
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error("Error:" + e));
}
}
}
// Fast forwards the audio file by 30 seconds.
function forwardAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById('myaudio');
oAudio.currentTime += 30.0;
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error("Error:" + e));
}
}
}
// Restart the audio file to the beginning.
function restartAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById('myaudio');
oAudio.currentTime = 0;
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error("Error:" + e));
}
}
}
</script>
</head>
<body>
<p>
<input type="text" id="audiofile" size="80" value="等一分钟.mp3" />
</p>
<audio id="myaudio">
HTML5 audio not supported
</audio>
<button id="play" onclick="playAudio();">
Play
</button>
<button onclick="rewindAudio();">
Rewind
</button>
<button onclick="forwardAudio();">
Fast forward
</button>
<button onclick="restartAudio();">
Restart
</button>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询