⚠️ 重要提醒:Flash 技术已过时
在开始之前,必须强调一个极其重要的点:Adobe Flash Player 已于 2025年1月12日 正式停止支持,并被所有主流浏览器(Chrome, Firefox, Edge, Safari 等)禁用。

(图片来源网络,侵删)
这意味着,任何使用 Flash 技术制作的播放器,在现代浏览器上将无法工作,它们会显示一个“需要激活 Flash”或“插件已阻止”的提示,用户无法播放音频。
强烈建议您不要在实际项目中使用 Flash 技术。 现代、可靠且兼容性好的替代方案是 HTML5 Audio API。
现代替代方案:HTML5 Audio 播放器
这是一个完整、可用且推荐的 MP3 播放器代码示例,它不依赖任何外部库,纯 HTML, CSS 和 JavaScript 实现。
代码结构
您需要创建三个文件:

(图片来源网络,侵删)
index.html(页面结构)style.css(样式)script.js(播放器逻辑)
index.html (主页面)
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">HTML5 MP3 播放器</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="player-container">
<h1>我的音乐播放器</h1>
<!-- 专辑封面和播放器控制区 -->
<div class="player">
<div class="album-cover">
<img id="album-art" src="https://via.placeholder.com/300" alt="专辑封面">
</div>
<div class="info">
<h2 id="song-title">歌曲标题</h2>
<p id="artist-name">艺术家</p>
</div>
<div class="controls">
<button id="prev-btn">⏮</button>
<button id="play-btn">▶</button>
<button id="next-btn">⏭</button>
</div>
<!-- 进度条 -->
<div class="progress-container">
<span id="current-time">0:00</span>
<div class="progress-bar">
<div id="progress"></div>
</div>
<span id="duration">0:00</span>
</div>
<!-- 音量控制 -->
<div class="volume-container">
<span>🔊</span>
<input type="range" id="volume-slider" min="0" max="1" step="0.01" value="1">
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
style.css (样式)
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
background-color: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
.player-container {
background: #ffffff;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
padding: 30px;
width: 90%;
max-width: 400px;
text-align: center;
}
.player-container h1 {
margin-top: 0;
color: #333;
}
.player {
margin-top: 20px;
}
.album-cover {
width: 250px;
height: 250px;
margin: 0 auto 20px;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}
.album-cover img {
width: 100%;
height: 100%;
object-fit: cover;
}
.info h2 {
margin: 10px 0 5px;
font-size: 1.5em;
color: #222;
}
.info p {
margin: 0 0 20px;
color: #666;
font-size: 1em;
}
.controls {
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
}
.controls button {
background: #007bff;
color: white;
border: none;
border-radius: 50%;
width: 50px;
height: 50px;
font-size: 1.2em;
cursor: pointer;
transition: background 0.3s;
}
.controls button:hover {
background: #0056b3;
}
.controls button#play-btn {
width: 60px;
height: 60px;
font-size: 1.5em;
background: #28a745;
}
.controls button#play-btn:hover {
background: #218838;
}
.progress-container {
margin: 20px 0;
cursor: pointer;
}
.progress-bar {
width: 100%;
height: 5px;
background: #e0e0e0;
border-radius: 5px;
position: relative;
}
#progress {
height: 100%;
background: #007bff;
border-radius: 5px;
width: 0%;
transition: width 0.1s linear;
}
.progress-container span {
font-size: 0.8em;
color: #666;
}
.volume-container {
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
}
.volume-container input[type="range"] {
width: 100px;
}
script.js (逻辑)
// 获取DOM元素
const audio = new Audio('path/to/your/music.mp3'); // 替换为你的MP3文件路径
const playBtn = document.getElementById('play-btn');
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
const progressBar = document.getElementById('progress');
const progressContainer = document.querySelector('.progress-container');
const currentTimeEl = document.getElementById('current-time');
const durationEl = document.getElementById('duration');
const volumeSlider = document.getElementById('volume-slider');
const songTitleEl = document.getElementById('song-title');
const artistNameEl = document.getElementById('artist-name');
const albumArtEl = document.getElementById('album-art');
// 播放列表 (示例)
const playlist = [
{ src: 'path/to/your/music1.mp3', title: '第一首歌', artist: '艺术家A', cover: 'cover1.jpg' },
{ src: 'path/to/your/music2.mp3', title: '第二首歌', artist: '艺术家B', cover: 'cover2.jpg' },
{ src: 'path/to/your/music3.mp3', title: '第三首歌', artist: '艺术家C', cover: 'cover3.jpg' }
];
let currentSongIndex = 0;
// 初始化播放器
function loadSong(index) {
const song = playlist[index];
audio.src = song.src;
songTitleEl.textContent = song.title;
artistNameEl.textContent = song.artist;
albumArtEl.src = song.cover; // 确保图片路径正确
audio.play();
playBtn.textContent = '⏸';
}
// 播放/暂停功能
function playPause() {
if (audio.paused) {
audio.play();
playBtn.textContent = '⏸';
} else {
audio.pause();
playBtn.textContent = '▶';
}
}
// 更新进度条
function updateProgress() {
const { duration, currentTime } = audio;
const progressPercent = (currentTime / duration) * 100;
progressBar.style.width = `${progressPercent}%`;
// 更新当前时间显示
currentTimeEl.textContent = formatTime(currentTime);
}
// 格式化时间 (秒 -> 分:秒)
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${minutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
}
// 设置进度条
function setProgress(e) {
const width = this.clientWidth;
const clickX = e.offsetX;
const duration = audio.duration;
audio.currentTime = (clickX / width) * duration;
}
// 上一首/下一首
function prevSong() {
currentSongIndex--;
if (currentSongIndex < 0) {
currentSongIndex = playlist.length - 1;
}
loadSong(currentSongIndex);
}
function nextSong() {
currentSongIndex++;
if (currentSongIndex > playlist.length - 1) {
currentSongIndex = 0;
}
loadSong(currentSongIndex);
}
// 事件监听器
playBtn.addEventListener('click', playPause);
prevBtn.addEventListener('click', prevSong);
nextBtn.addEventListener('click', nextSong);
audio.addEventListener('timeupdate', updateProgress);
audio.addEventListener('ended', nextSong); // 歌曲结束时自动播放下一首
progressContainer.addEventListener('click', setProgress);
// 音量控制
volumeSlider.addEventListener('input', (e) => {
audio.volume = e.target.value;
});
// 当元数据加载完成时,显示总时长
audio.addEventListener('loadedmetadata', () => {
durationEl.textContent = formatTime(audio.duration);
});
// 初始加载第一首歌
loadSong(currentSongIndex);
纯 Flash 播放器代码(仅作历史参考)
如果您只是出于好奇或学习历史技术的目的,这里是一个简单的 Flash MP3 播放器的代码,它需要 Adobe Animate (旧称 Flash Professional) 来创建。
Animate 中的设置
- 创建新文档:打开 Adobe Animate,创建一个新的 "ActionScript 3.0" 文档。
- 绘制播放器:
- 创建一个 按钮,实例名称命名为
playPause_btn。 - 创建另一个 按钮,实例名称命名为
stop_btn。 - 创建一个 动态文本,用于显示当前播放时间,实例名称命名为
time_txt。
- 创建一个 按钮,实例名称命名为
- 发布设置:
- 点击
文件->发布设置。 - 在
格式选项卡中,确保勾选了 Flash (.swf)。 - 在
Flash选项卡中,将 ActionScript 版本 设置为 ActionScript 3.0。 - 点击
确定,然后按Ctrl+Enter(或Cmd+Enter) 来测试和发布,这会生成一个.swf文件。
- 点击
ActionScript 3.0 代码
将以下代码粘贴到 Animate 的 "动作" 面板中(通常是帧动作)。
// 导入必要的类
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
// --- 变量定义 ---
var mySound:Sound;
var myChannel:SoundChannel;
var isPlaying:Boolean = false;
var songPath:String = "path/to/your/music.mp3"; // <-- 替换为你的MP3文件路径
// --- 初始化 ---
function init():void {
// 创建一个 Sound 对象
mySound = new Sound();
// 创建一个 URLRequest 来指定MP3文件的路径
var soundRequest:URLRequest = new URLRequest(songPath);
// 加载MP3文件
mySound.load(soundRequest);
// 初始时,时间文本显示 "0:00"
time_txt.text = "0:00";
// 添加事件监听器
playPause_btn.addEventListener(MouseEvent.CLICK, togglePlayPause);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
myChannel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
}
// --- 播放/暂停功能 ---
function togglePlayPause(e:MouseEvent):void {
if (!isPlaying) {
// 如果当前没有播放,则播放
// myChannel 不存在(例如第一次点击或停止后),则创建它
if (myChannel == null) {
myChannel = mySound.play();
} else {
// 否则,从当前位置继续播放
myChannel = mySound.play(myChannel.position);
}
playPause_btn.gotoAndStop(2); // 假设按钮有两帧,第2帧是暂停图标
isPlaying = true;
// 添加一个事件监听器来更新时间
myChannel.addEventListener(Event.ENTER_FRAME, updateTime);
} else {
// 如果正在播放,则暂停
myChannel.stop();
playPause_btn.gotoAndStop(1); // 返回到播放图标
isPlaying = false;
// 移除更新时间的监听器
myChannel.removeEventListener(Event.ENTER_FRAME, updateTime);
}
}
// --- 停止功能 ---
function stopSound(e:MouseEvent):void {
myChannel.stop();
myChannel = null; // 将 channel 设为 null,以便下次点击播放时从头开始
playPause_btn.gotoAndStop(1); // 返回到播放图标
isPlaying = false;
time_txt.text = "0:00";
// 移除更新时间的监听器
if (myChannel) {
myChannel.removeEventListener(Event.ENTER_FRAME, updateTime);
}
}
// --- 更新时间显示 ---
function updateTime(e:Event):void {
// 将秒数转换为 "分:秒" 格式
var minutes:int = Math.floor(myChannel.position / 60000);
var seconds:int = Math.floor((myChannel.position % 60000) / 1000);
time_txt.text = minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
}
// --- 歌曲播放完成事件 ---
function onSoundComplete(e:Event):void {
// 歌曲播放完后,重置状态
playPause_btn.gotoAndStop(1);
isPlaying = false;
time_txt.text = "0:00";
myChannel.removeEventListener(Event.ENTER_FRAME, updateTime);
}
// --- 启动应用 ---
init();
在 HTML 中嵌入 Flash
发布后,你会得到一个 .swf 文件,要将其嵌入网页,你需要使用 <object> 和 <embed> 标签,现代浏览器通常只支持 <object>。
<object width="300" height="100" data="your_player.swf" type="application/x-shockwave-flash">
<param name="movie" value="your_player.swf">
<param name="quality" value="high">
<param name="bgcolor" value="#ffffff">
<!-- 如果用户没有安装Flash,可以显示此内容 -->
<p>您的浏览器不支持 Flash 播放器。<a href="http://get.adobe.com/flashplayer/">请点击此处下载最新版本。</a></p>
</object>
| 特性 | HTML5 Audio (推荐) | Flash (已过时) |
|---|---|---|
| 兼容性 | 极好,所有现代浏览器原生支持。 | 极差,已被所有主流浏览器禁用。 |
| 依赖 | 无需任何插件。 | 需要 Adobe Flash Player 插件。 |
| 开发 | 使用 HTML, CSS, JavaScript,标准、简单。 | 使用 Adobe Animate 和 ActionScript,专有工具。 |
| 功能 | 功能强大,支持列表、可视化、自定义UI等。 | 功能强大,但在音频方面不如HTML5灵活。 |
| 安全性 | 安全。 | 存在历史安全漏洞。 |
| 未来 | Web标准,是未来发展的方向。 | 已死亡,无未来。 |
请务必选择 HTML5 Audio 方案来构建您的网页音乐播放器。 它更安全、更兼容、更易于维护,并且是业界公认的标准。

(图片来源网络,侵删)
