如何用delphi编写音乐播放器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用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写代码实现自动播放音乐

参考技术A 由于浏览器的安全策略问题,现在已经不再支持打开页面自动播放音乐了,如需要播放还是要让用户点击播放才可以,可以使用html5的audio标签加载音乐播放。
<audio src="https://lanye.org/demo.mp3" controls width="100%"></audio>
参考技术B

前几天一个朋友找到我,他说他女朋友马上过生日,于是想问问我能不能写一个生日祝福代码。好兄弟的请求当然不能拒绝,直接安排!!

于是我用html写了一个简单的页面:点开后会显示来到这个世界多长时间和祝福话语,下滑后是自转相册(有背景音乐)。

原文链接:

html生日快乐代码

核心代码(不是完整代码):

#head

width:100%;
height:100%;
position: absolute;
-webkit-transform-style: preserve-3d;
-webkit-animation:donghua 15s linear 0s infinite;
-moz-transform-style: preserve-3d;
-moz-animation:donghua 15s linear 0s infinite;
-ms-transform-style: preserve-3d;
-ms-animation:donghua 25s linear 0s infinite;

#head div

position: absolute;
top:0;
left:0;
width:300px;
height:400px;
border:1px solid #000
text-align: center;
line-height:100px;

#head div:nth-child(1)

-webkit-transform:rotateY(0deg) translateZ(400px);
-moz-transform:rotateY(0deg) translateZ(400px);
-ms-transform:rotateY(0deg) translateZ(400px);
background:url(images/01.png);
background-size:cover;
12345678910111213141516171819202122232425262728293031
Heart.prototype.draw = function()
this.size -= this.speedSize;
this.x += this.speedX;
this.y += this.speedY;
ctx.save();
ctx.translate(-1000,this.y);
ctx.scale(this.size, this.size);
ctx.beginPath();
for (var i = 0; i < precision; i++)
var vector = this.vertices[i];
ctx.lineTo(vector.x, vector.y);

ctx.globalAlpha = this.size;
ctx.shadowBlur = Math.round((3 - this.size) * 10);
ctx.shadowColor = "hsla(0, 100%, 60%,0.5)";
ctx.shadowOffsetX = this.x + 1000;
ctx.globalCompositeOperation = "screen"
ctx.closePath();
ctx.fill()
ctx.restore();
;


function render(a)
requestAnimationFrame(render);

hearts.push(new Heart())
ctx.clearRect(0,0,ww,wh);
for (var i = 0; i < hearts.length; i++)
hearts[i].draw();
if(hearts[i].size <= 0)
hearts.splice(i,1);
i--;





onResize();
window.addEventListener("mousemove", onMove);
window.addEventListener("touchmove", onMove);
window.addEventListener("resize", onResize);
requestAnimationFrame(render);

window.οnlοad=function starttime()
time(h1,'2000,1,1');     // 出生时间
ptimer = setTimeout(starttime,1000); // 添加计时器


function time(obj,futimg)
var nowtime = new Date().getTime(); // 现在时间转换为时间戳
var futruetime =  new Date(futimg).getTime(); // 未来时间转换为时间戳
var msec = nowtime-futruetime; // 毫秒 未来时间-现在时间
var time = (msec/1000);  // 毫秒/1000
var day = parseInt(time/86400); // 天  24*60*60*1000
var hour = parseInt(time/3600)-24*day;    // 小时 60*60 总小时数-过去的小时数=现在的小时数
var minute = parseInt(time%3600/60); // 分 -(day*24) 以60秒为一整份 取余 剩下秒数 秒数/60 就是分钟数
var second = parseInt(time%60);  // 以60秒为一整份 取余 剩下秒数
obj.innerHTML="陈陈<br>你已经来到这个世界:<br>"+day+"天"+hour+"小时"+minute+"分"+second+"秒"+"了<br><span>今日是你的生日,愿所有的快乐、所有的幸福、所有的温馨、所有的好运围绕在你身边。生日快乐!</span><p>下<br>滑<br>有<br>惊<br>喜</p>"

return true;
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162

效果如下:


这里面的文字可以自行修改,并且时间是会一直走动的,出生时间改成你女朋友的。

下滑后有自转照片:

照片可以自己换哦。

补充:

很多朋友想手机端打开文件,那就需要换图片的地址和音频的地址(你可以发一个仅自己可见的说说,然后就可以生成图片的链接地址了),此时只需要发html文件就可以用手机浏览器打开了。

上图的地址换成图片的链接网址(一共十个)。

对于音频文件,我这里提供一个链接(可能过期):https://www.0dutv.com/upload/dance/20200305/D1E8DB5EB16A57732BDD636C759DA034.mp3

上图的地址换成音频的链接网址。

大家可以网上找音乐外链。
如果是特定的音频录音,可以用这个方法:利用邮箱附件的形式,比如QQ邮箱,给自己发一封邮件,把音乐以附件的形式附带在上面,收信的时候用来下载附件的那个地址,就是歌曲的下载链接了。

完整项目:

里面有我自己找的十张照片和两个背景音乐(可以换成自己的录音),另外还有使用说明。

完整项目地址:html生日快乐代码

以上是关于如何用delphi编写音乐播放器的主要内容,如果未能解决你的问题,请参考以下文章

如何用mciSendCommand()/mciSendString() 函数指定声卡播放音乐

如何用c语言插入(背景)音乐

如何用iis做的http服务器可以播放音乐

c语言如何用mciSendString来实现音乐播放功能

怎么用C # winform 编写的音乐播放器? 求高手指点

如何用扬声器清晰地录制人声和音乐? (不是耳机)