如何用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

以上代码给你做个参考
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-01-21
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;
第2个回答  2012-06-01
MediaPlayer
也可以调用其他音乐插件
第3个回答  2012-05-18
搜索:delphi bass

如何用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, ...

谁有Delphi简单音乐播放器源码,要求简单,太复杂了,没用,发链接就行,我...
用 delphi 编写音乐播放器,可以考虑使用 Bass 音乐库,BASS音频库是一款对非商业用户免费的音频处理类库,功能强大,支持几乎所有音频的解码编码等处理。Bass 的官方主页提供了包括 C、 delphi、 VB 等多种语言和平台的示例代码,其中 delphi 语言示例中有 BaseTest 示例了简单的音乐播放:示例文件的目录...

delphi 的roundclass函数怎么用
delphi制作音乐播放器完整代码如下: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, MPlayer, ComCtrls,registry,inifiles; type TForm1 = class(TForm)

为什么易语言制作音乐播放器,显示频谱都需要bass.dll文件。
因为没人去写,有人写当然可以实现这些功能,bass这个库,什么delphi什么vb、c++这些有时有人会用,但是如何不能实现效果,就自己写库吧!

python手机编程?
4、安装完成,点击打开或在桌面找到应用点击进去,接下来是使用他写Python最关键的步骤了,进去TermuxAPP之后在TermuxAPP的界面中输入:pkginstallpython则会自动下载Python的解码器以便正常运行我们的程序。 5、完毕以后,则会看到下方出现一个对话框,就是最后确定我们是否要安装Python的解码器,Y代表yes(就是确认)N代表no(...

软件设计用到哪些工具和技术
软件呢?桌面软件如:音乐播放器、优化大师、QQ、网际快车等软件界面的设计会用到哪些工具、必须掌握哪些技术??解析:我认为桌面软件开发应包括 编程语言包括C,C++,Delphi或着C# 以及具体对象相关的技术: 如音视频的编解码,网络相关的知识,和系统相关的编程;开发工具: 集成开发环境(IDE)如MFC, .NET...

有没有什么软件可以显示电脑播放音乐的频谱,要全屏只有频谱的那种...
像千千静听、百度音乐、Windows Media Player播放器都可以实现,下面以Windows Media Player播放器为例说明一下具体设置:1、右键打开需要播放的软件,选择"打开方式-Windows Media Player播放器"。2、在播放器页面,右键选择”可视化效果-条形与波浪-波形“即可。

求 最好用的 一款 Winmap 或者 更好用的播放器
本软件是在win2000下用delphi6开发的多媒体播放程序。此软件的特点为:全部为简体中文 www.onlinedown.net\/soft\/7472.htm 31. Blaze DVD播放器 中文版 1.5 非常容易使用的DVD,VCD,SVCD播放器 download.pchome.net\/multimedia\/media\/14058.html 32. Flash 播放器汉化版 5.0 从光盘上找到的一个小软件,希望...

win7 中开机进程有那些。分别代表什么意思。
补充说明:这个进程在WIN7下可以对耳机音质进行大幅度的提升,具体设置如下:将耳机插上,点击音量图标,打开后点击上面的耳机图标,选择增强功能,内有三项设置:低音增强、耳机虚拟化、响度均衡,笔者建议大家把这三项全选,点击确定,重新启动音乐播放器,播放一首歌曲,你会发现一首原本普通的音质的歌曲现在几乎拥有了家庭影院...

相似回答