[HTML] 如何用html播放mp3文件?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HTML] 如何用html播放mp3文件?相关的知识,希望对你有一定的参考价值。

我要实现的是:
鼠标单击一个图片,然后播放一个声音.
就是像有道词典那样实现英文发音的功能,
希望能给出可用的html代码

你看,播放MP3可以直接用html标签embed
具体用法:
<embed autoplay="false" src="音乐文件名字.mp3" width="200" height="45" />
autoplay可选参数:true,false,其中true代表自动播放,false代表手动播放。
src就是MP3的路径。
width和height代表播放器的宽度和高度。
希望能帮到你。追问

我想用javascript控制这个embed标签,来播放我指定的文件,怎么写?
听说embed不允许动态修改src的值? 那用嵌套这个embed,又怎么实现呢? 我写的代码总是有问题。.

参考技术A

 HTML 播放音乐教材

如何用delphi编写音乐播放器

一、使用Delphi自带的MediaPlayer控件。
1.在窗体里添加MediaPlayer1控件.
2.在窗体创建处(TForm1.FormCreate)添加如下代码即可播放
MediaPlayer1.FileName:='<需要播放的MP3文件>';
MediaPlayer1.Open;

二、使用Bass.dll函数库(在www.un4seen.com里下载)。
简单例子:
uses
Bass;
BASS_Init(-1, 44100, 0, 0, nil);//初始化Bass库
var
chan:HSTREAM;//声明流句柄
chan:=BASS_StreamCreateFile(False, PChar('<需要播放的MP3文件>'),0,0,0);//装载媒体文件获取句柄
if chan<>0 then BASS_ChannelPlay(chan, False);//播放
用完后:
BASS_StreamFree(chan);//释放文件
BASS_Free();关闭Bass

以上代码给你做个参考
参考技术A delphi制作音乐播放器完整代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, MPlayer, ComCtrls,registry,inifiles;

type
TForm1 = class(TForm)
Image2: TImage;
Label1: TLabel;
Label2: TLabel;
openimg: TImage;
playimg: TImage;
stopimg: TImage;
Label3: TLabel;
MediaPlayer1: TMediaPlayer;
Timer1: TTimer;
OpenDialog1: TOpenDialog;
Image1: TImage;
Image3: TImage;
Image4: TImage;
ListBox1: TListBox;
Image5: TImage;
SaveDialog1: TSaveDialog;
TrackBar1: TTrackBar;
repeatimg: TImage;
ListBox2: TListBox;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure playimgClick(Sender: TObject);
procedure openimgClick(Sender: TObject);
procedure stopimgClick(Sender: TObject);
procedure pauseimgClick(Sender: TObject);
procedure Label1Click(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image3Click(Sender: TObject);
procedure Image4Click(Sender: TObject);
procedure Image5Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure openimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure openimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure playimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure playimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure stopimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure stopimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure repeatimgClick(Sender: TObject);
procedure MediaPlayer1Notify(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);

private
Private declarations
public
Public declarations
end;

var
Form1: TForm1;
repeat_bl : boolean = false;
mode : boolean;
musicindex : integer = -1;
exname : string ;

implementation
$R *.DFM

function move(str : string) : string;
//走马灯
var
s : string;
temp : char;
i : integer;
begin
temp := str[1];
s := str;
for i := 1 to length(str)-1 do
begin
s[i] := s[i+1];
str := s;
end;
s[length(str)] := temp;
move := s;
end;
function updatetitle (str : string) : string;
//读出歌名
var
temp , title : string;
i : integer;
begin
i := length(str);
while str[i] <> '\' do
begin
temp := temp + str[i];
i := i-1;
end;
i := length(temp);
while i > 0 do
begin
title := title + temp[i];
i := i - 1;
end;
for i := 1 to 85-length(title) do title := title +' ';
updatetitle := title;
end;

function timefmt(time : integer) : string;
//转换时间格式
begin
time := round(time / 1000);
timefmt := FormatFloat('00' , Trunc(time / 3600)) + ':' +
FormatFloat('00' , (Trunc(time / 60)) MOD 60) + ':' +
FormatFloat('00' , time MOD 60);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if MediaPlayer1.Position = MediaPlayer1.Length then
Begin
if repeat_bl then
Begin
MediaPlayer1.Open;
MediaPlayer1.Play;
End;
End;

if mediaplayer1.mode = mpplaying then
begin
if mode then
label1.caption := timefmt(mediaplayer1.position)
else
label1.caption := timefmt(mediaplayer1.Length - mediaplayer1.position );
label3.caption := move(label3.caption);
label2.caption := timefmt(mediaplayer1.length);
trackbar1.max := mediaplayer1.Length div 1000;
trackbar1.Position := mediaplayer1.position div 1000;
end
else
begin
label1.caption := '00:00:00';
label2.caption :='00:00:00';
label3.caption := updatetitle(mediaplayer1.filename);
timer1.Enabled := false;
trackbar1.Position := 0;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
reg : tregistry;
i : integer;

begin
exname := ExtractFilePath(Application.ExeName);
repeat_bl := false;
mode := true;//true 代表时间格式为 elapse,false 为remaining
reg := tregistry.create;
try
reg.rootkey := hkey_current_user;
if reg.openkey('\software\minmediaplayer',true) then
begin
if reg.readstring('filename') <> '' then
begin
mediaplayer1.FileName := reg.readstring('filename');
label3.caption := updatetitle(mediaplayer1.filename);
end;
end;
finally
reg.closekey;
reg.free;
end;
end;

procedure TForm1.playimgClick(Sender: TObject);
begin
if mediaplayer1.FileName <> '' then
case mediaplayer1.Mode of
mpPlaying :
begin
mediaplayer1.Pause;
timer1.Enabled := false;
end;
mpPaused :
begin
mediaplayer1.pause;
timer1.Enabled := true;
end
else
mediaplayer1.open;
mediaplayer1.Play;
timer1.Enabled := true;
end;
end;

procedure TForm1.openimgClick(Sender: TObject);
var
i : integer;
temp , title : string;
reg : tregistry;
begin
if opendialog1.execute then
if opendialog1.FilterIndex <> 4 then
begin
mediaplayer1.filename := opendialog1.filename;
mediaplayer1.open;
mediaplayer1.Play;
timer1.Enabled := true;
label3.caption := updatetitle(mediaplayer1.filename);

for i := 1 to 40-length(label3.caption) do label3.caption := label3.caption+' ';
//将档案路径储存於ms windows 注册表
reg := tregistry.create;
try
reg.rootkey := hkey_current_user;
if reg.openkey('\software\minmediaplayer',true) then
reg.writestring('filename',mediaplayer1.filename);
finally
reg.closekey;
reg.free;
end;
listbox1.Items.Add(updatetitle(mediaplayer1.filename));
listbox2.items.add(mediaplayer1.filename);
listbox1.Itemindex := listbox1.Items.count -1;
listbox2.ItemIndex := listbox2.Items.count -1;

end
else
begin
listbox2.items.LoadFromFile(opendialog1.FileName);
listbox1.Items.Clear;
for i := 0 to listbox2.Items.Count-1 do
listbox1.Items.add(updatetitle(listbox2.items.strings[i]));
end;

end;

procedure TForm1.stopimgClick(Sender: TObject);
begin
if mediaplayer1.Mode = mpplaying then
begin
mediaplayer1.Stop;
trackbar1.Position := 0 ;
timer1.Enabled := false;
label3.caption := updatetitle(mediaplayer1.filename);
label1.caption := '00:00:00';
end;
end;

procedure TForm1.pauseimgClick(Sender: TObject);
begin
mediaplayer1.Pause;
timer1.Enabled := not(timer1.enabled);
end;

procedure TForm1.Label1Click(Sender: TObject);
begin
mode := not(mode);
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
Const
Sc_DragMove:LongInt=$F012;
begin
if Button = mbLeft then
begin
ReleaseCapture;
SendMessage(handle,wm_SysCommand,Sc_DragMove,0);
end;
end;

procedure TForm1.Image3Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Image4Click(Sender: TObject);
begin
Application.Minimize;
end;

procedure TForm1.Image5Click(Sender: TObject);
begin
if savedialog1.execute then listbox2.Items.SaveToFile(savedialog1.filename);

end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
mediaplayer1.filename := listbox2.Items[listbox1.itemindex];
listbox2.ItemIndex := listbox1.ItemIndex;
label3.caption := updatetitle(mediaplayer1.filename);
mediaplayer1.Open;
mediaplayer1.Play;
timer1.Enabled := true;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
listbox2.itemindex := listbox1.itemindex;
mediaplayer1.FileName := listbox2.Items.Strings[listbox2.itemindex];
listbox1.Hint := listbox2.Items[listbox1.itemindex];
end;

procedure TForm1.openimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin

openimg.Picture.LoadFromFile(exname+'image/open_c.bmp');
end;

procedure TForm1.openimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
openimg.Picture.LoadFromFile(exname+'image/open.bmp');
end;

procedure TForm1.playimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
playimg.Picture.LoadFromFile(exname+'image/play_c.bmp');
end;

procedure TForm1.playimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
playimg.Picture.LoadFromFile(exname+'image/play.bmp');
end;

procedure TForm1.stopimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
stopimg.Picture.LoadFromFile(exname+'image/stop_c.bmp');
end;

procedure TForm1.stopimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
stopimg.Picture.LoadFromFile(exname+'image/stop.bmp');
end;

procedure TForm1.repeatimgClick(Sender: TObject);
begin
repeat_bl := not(repeat_bl);
if repeat_bl then repeatimg.Picture.LoadFromFile(exname+'image/repeat_c.bmp')
else repeatimg.Picture.LoadFromFile(exname+'image/repeat.bmp');
end;
procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
if not(repeat_bl) then
if listbox1.ItemIndex < listbox1.items.Count-1 then begin
timer1.Enabled := true;
listbox1.ItemIndex := listbox1.itemindex+1;
listbox2.itemindex := listbox2.itemindex+1;
mediaplayer1.FileName := listbox2.Items.Strings[listbox2.itemindex];
mediaplayer1.Open;
mediaplayer1.Play;
end;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
if mediaplayer1.Mode = mpplaying then
begin
mediaplayer1.Position := trackbar1.Position*1000;
mediaplayer1.Play;
end;
end;
参考技术B MediaPlayer
也可以调用其他音乐插件
参考技术C 搜索:delphi bass

以上是关于[HTML] 如何用html播放mp3文件?的主要内容,如果未能解决你的问题,请参考以下文章

如何用js控制audio元素连续播放mp3文件

如何用 C 或 python 找出 mp3 的 bpm [关闭]

如何用HTML写代码实现自动播放音乐

html中的语法自动循环播放.mp3文件

使用 JavaScript 按顺序播放 MP3 文件,无需 HTML5 音频

SpringBoot+thymeleaf+html+javascript 实现的播放mp3简单的一个功能