⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3play.pas

📁 一个很好的阅读器
💻 PAS
字号:
{
模块名称:MP3播放器

使用方法:1、Show
		  2、ListBoxSoundName: 存放音乐文件名

返回值:  无
}

unit MP3Play;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Buttons, ComCtrls, StdCtrls, Registry, MP3Class;

type
  TFormMP3Play = class(TForm)
	PanelSound: TPanel;

	BtnPlay: TSpeedButton;
	BtnPlay1: TSpeedButton;
	BtnPause: TSpeedButton;
	BtnStop: TSpeedButton;
	BtnEject: TSpeedButton;
	BtnFirst: TSpeedButton;
	BtnLast: TSpeedButton;
	BtnRepeat: TSpeedButton;
	BtnCancel: TSpeedButton;
	TrackBar: TTrackBar;

	DlgOpenSound: TOpenDialog;
	ListBoxSoundName: TListBox;
	cbCurrentFilename: TComboBox;

	procedure FormCreate(Sender: TObject);
	procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
	procedure FormClose(Sender: TObject; var Action: TCloseAction);
	procedure FormDestroy(Sender: TObject);
	procedure PanelSoundMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
	procedure cbCurrentFilenameChange(Sender: TObject);

	procedure BtnPlayClick(Sender: TObject);
	procedure BtnStopClick(Sender: TObject);
	procedure BtnEjectClick(Sender: TObject);
	procedure BtnFirstClick(Sender: TObject);
	procedure BtnCancelClick(Sender: TObject);

	procedure TrackBarChange(Sender: TObject);
	procedure MP3PlayThreadEnded(Sender: TObject);
	procedure MP3PlayActFrame(Sender: TObject; ActFrame: Integer);

	procedure RegMP3;
	procedure OpenMP3;
	procedure EnableBtn(Enable : Boolean);
	function  isHaveSound : Boolean;

  private
	{ Private declarations }
	FRegini : TRegIniFile;       //注册表变量
	FMP3Play : TMP3Play;         //MP3控件
	FboolPlayAllFlag : Boolean;  //能否播放其中一个MP3
	FnSoundNumber : integer;     //当前播放的MP3数目
	FState : (stPlay, stPause, stStop, stEject, stError);  //播放状态

	FboolChangeSeek : Boolean;

  public
	{ Public declarations }
  end;

var
  FormMP3Play: TFormMP3Play;

implementation

uses Global;

{$R *.DFM}

procedure TFormMP3Play.FormCreate(Sender: TObject);
begin
	//注册MP3
	RegMP3;

	//初始化设置
	OpenMP3;
end;

procedure TFormMP3Play.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
	if Key = VK_Escape then
		FormMP3Play.Close;
end;

procedure TFormMP3Play.FormClose(Sender: TObject; var Action: TCloseAction);
var i : integer;
begin
	//保存音乐文件设置
	for i := 0 to ListBoxSoundName.Items.Count - 1 do
		RegWriteStr(c_strRegSoundPath, c_strRegSoundFilename + IntToStr(i), ListBoxSoundName.Items[i]);

    //最后一个文件写空
	RegWriteStr(c_strRegSoundPath, c_strRegSoundFilename + IntToStr(ListBoxSoundName.Items.Count), '');
	RegWriteBool(c_strRegSoundPath, c_strRegSoundRepeat, BtnRepeat.Down);  //背景音乐重复播放标志
end;

procedure TFormMP3Play.FormDestroy(Sender: TObject);
begin
	//删除MP3控件
	FMP3Play.Free;
end;

procedure TFormMP3Play.PanelSoundMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
	if Button = mbLeft then
	begin
		ReleaseCapture;
		SendMessage(FormMP3Play.Handle, WM_NCLBUTTONDOWN, HTCaption, 0);
	end;
end;

procedure TFormMP3Play.cbCurrentFilenameChange(Sender: TObject);
begin
    //播放指定MP3
	if (isHaveSound) and (FnSoundNumber - 1 <> cbCurrentFilename.ItemIndex) then
	begin
		FnSoundNumber := cbCurrentFilename.ItemIndex;
		MP3PlayThreadEnded(Self);  //播放MP3音乐
	end;
end;

procedure TFormMP3Play.BtnPlayClick(Sender: TObject);
begin
	case FState of
		stPlay:
		begin  //处于播放状态则暂停
			BtnPlay.Glyph := BtnPlay1.Glyph;  //转换图标
			BtnPlay.Hint := BtnPlay1.Hint;    //转换Hint

			FMP3Play.Pause;
			FState := stPause;
		end;

		stPause:
		begin  //处于暂停状态则播放
			BtnPlay.Glyph := BtnPause.Glyph;  //转换图标
			BtnPlay.Hint := BtnPause.Hint;    //转换Hint

			FMP3Play.Play;
			FState := stPlay;
		end;
	else
		MP3PlayThreadEnded(Self);  //播放
	end;
end;

procedure TFormMP3Play.BtnStopClick(Sender: TObject);
begin
	if FState in [stPlay, stPause, stEject] then
	begin
		FState := stStop;          //停止状态
		MP3PlayThreadEnded(Self);  //停止播放
	end;
end;

procedure TFormMP3Play.BtnEjectClick(Sender: TObject);
var nMP3Index : integer;
begin
	if isHaveSound then
		DlgOpenSound.InitialDir := ExtractFilePath(ListBoxSoundName.Items[0]);

	if DlgOpenSound.Execute then
	begin
		ListBoxSoundName.Clear;
		cbCurrentFilename.Items.Clear;

		for nMP3Index := 0 to DlgOpenSound.Files.Count - 1 do  //添加背景音乐文件到列表框中
		begin
			ListBoxSoundName.Items.Add(DlgOpenSound.Files.Strings[nMP3Index]);
			cbCurrentFilename.Items.Add(IntToStr(nMP3Index + 1) + '. ' + ExtractFileName(DlgOpenSound.Files.Strings[nMP3Index]));
		end;

		//初始化状态
		FnSoundNumber := 0;         //当前MP3曲目写0
		FboolPlayAllFlag := False;  //不能播放其中一个MP3
		EnableBtn(True);            //打开按钮
		FState := stEject;          //写状态:打开状态
		BtnPlayClick(Self);         //播放
	end;
end;

procedure TFormMP3Play.BtnFirstClick(Sender: TObject);
begin
	FnSoundNumber := FnSoundNumber + (Sender As TSpeedButton).Tag;

	if (FnSoundNumber < 0) or (FnSoundNumber > ListBoxSoundName.Items.Count) then
		FnSoundNumber := 0;

	MP3PlayThreadEnded(Self);  //播放MP3音乐
end;

procedure TFormMP3Play.BtnCancelClick(Sender: TObject);
begin
	FormMP3Play.Close;
end;

procedure TFormMP3Play.TrackBarChange(Sender: TObject);
begin
	if (FState <> stError) and FboolChangeSeek then
		FMP3Play.Seek(TrackBar.Position);
end;

procedure TFormMP3Play.MP3PlayThreadEnded(Sender: TObject);
var strPlayFilename : string;   //播放的文件名
	boolError : Boolean;        //错误标志
	boolMainVisible : Boolean;  //主窗口是否显示
begin
    //不存在MP3则退出
	if (not g_boolExistMP3) then
		Exit;

	//建立MP3控件
	try
		if FMP3Play <> nil then
			FMP3Play.Free;

		FMP3Play := TMP3Play.Create(self);
		FMP3Play.Top := -300;
		FMP3Play.Left := -300;
		FMP3Play.Parent := FormMP3Play;
		FMP3Play.OnActFrame := MP3PlayActFrame;
		FMP3Play.OnThreadEnded := MP3PlayThreadEnded;
		FMP3Play.TabStop := False;

		g_boolExistMP3 := True;   //存在MP3控件
	except
		g_boolExistMP3 := False;  //不存在MP3控件
	end;

	if (not g_boolExistMP3) then
	begin
		//错误状态
		FState := stError;

		//关闭按钮
		EnableBtn(False);
		PanelSound.Enabled := False;

		with cbCurrentFilename do
		begin
			Items.Clear;
			Items.Add('初始化MP3错误。');
			ItemIndex := 0;
		end;
		
		Exit;
	end;

	//停止状态
	if FState = stStop then
	begin
		FMP3Play.Close;                   //关闭MP3
		FState := stEject;                //打开状态

		FnSoundNumber := 0;               //当前MP3数目写0
		FboolPlayAllFlag := False;        //不能播放其中一个MP3
		BtnPlay.Glyph := BtnPlay1.Glyph;  //转换图标
		BtnPlay.Hint := BtnPlay1.Hint;    //转换Hint

		//调整设置
		if isHaveSound then
		begin
			EnableBtn(True);
			cbCurrentFilename.ItemIndex := FnSoundNumber;

			//打开MP3
			FMP3Play.Open(ListBoxSoundName.Items[0], '');
			TrackBar.Position := TrackBar.Min;
			TrackBar.Max := FMP3Play.FrameCount;
		end
		else
		begin
			EnableBtn(False);

			with cbCurrentFilename do
			begin
				Items.Clear;
				Items.Add('没有选择背景音乐文件。');
				ItemIndex := 0;
			end;
		end;

		Exit;
	end;

	//调整MP3
	boolMainVisible := FormMP3Play.Visible;

	if not boolMainVisible then
	begin  //处理窗口的显示
		FormMP3Play.Left := -FormMP3Play.Left - FormMP3Play.Width - 100;
		FormMP3Play.Visible := True;
	end;

	FMP3Play.Authorize('LightBringer', '1441658209');  //注册

	if not boolMainVisible then
	begin  //复原窗口的显示
		FormMP3Play.Visible := False;
		FormMP3Play.Left := -(FormMP3Play.Left + FormMP3Play.Width + 100);
	end;

	boolError := False;  //写标志:没有错误

	inc(FnSoundNumber);  //MP3数目+1
	if FnSoundNumber > ListBoxSoundName.Items.Count then
	begin  //超出音乐列表,判断是否重复播放
		if (BtnRepeat.Down) and (FboolPlayAllFlag) then
		begin  //重复播放
			FnSoundNumber := 1;  //重复播放
		end
		else
		begin  //不重复播放
			FState := stEject;   //打开状态
			BtnStopClick(Self);

			Exit;
		end;
	end;

	//获取文件名,打开播放
	strPlayFilename := ListBoxSoundName.Items[FnSoundNumber - 1];
	cbCurrentFilename.ItemIndex := FnSoundNumber - 1;

	if FMP3Play.Open(strPlayFilename, '') <> 0 then
		boolError := True;

	if FMP3Play.Play <> 0 then
		boolError := True;

	//播放错误,则播放下一个
	if boolError then
		MP3PlayThreadEnded(Self)
	else
	begin
		FboolPlayAllFlag := True;         //能播放其中一个MP3

		BtnPlay.Glyph := BtnPause.Glyph;  //转换图标
		BtnPlay.Hint := BtnPause.Hint;    //转换Hint

		FState := stPlay;                 //播放状态

		//调整滚动条
		TrackBar.Position := TrackBar.Min;
		TrackBar.Max := FMP3Play.FrameCount;
	end;
end;

procedure TFormMP3Play.MP3PlayActFrame(Sender: TObject; ActFrame: Integer);
begin
	FboolChangeSeek := False;
	TrackBar.Position := ActFrame;
	FboolChangeSeek := True;
end;

procedure TFormMP3Play.RegMP3;
var strRegMP3Path : string;  //MP3注册路径
	strRegMP3File : string;  //MP3文件路径
begin
	strRegMP3Path := 'CLSID\{3B00B10D-6EF0-11D1-A6AA-0020AFE4DE54}';
	strRegMP3File := ExtractFilePath(ParamStr(0)) + 'taleMP3.dat';
	FRegini  :=  TRegIniFile.Create('');   //打开注册表
	FRegini.RootKey := HKEY_CLASSES_ROOT;  //定义根目录

	//注册MP3控件
	//注册版本
	FRegini.WriteString(strRegMP3Path, '', 'Tale MP3Play Control');

	//注册Control
	FRegini.WriteString(strRegMP3Path + '\Control', '', '');

	//注册InprocServer32
	FRegini.WriteString(strRegMP3Path + '\InprocServer32', '', strRegMP3File);
	FRegini.WriteString(strRegMP3Path + '\InprocServer32', 'ThreadingModel', 'Apartment');

	//注册MiscStatus
	FRegini.WriteString(strRegMP3Path + '\MiscStatus', '', '0');
	FRegini.WriteString(strRegMP3Path + '\MiscStatus\1', '', '131473');

	//注册ProgID
	FRegini.WriteString(strRegMP3Path + '\ProgID', '', 'MP3Play.MP3PlayCtrl.1');

	//注册PToolboxBitmap32
	FRegini.WriteString(strRegMP3Path + '\PToolboxBitmap32', '', strRegMP3File + ', 1');

	//注册ToolboxBitmap32
	FRegini.WriteString(strRegMP3Path + '\ToolboxBitmap32', '', strRegMP3File + ', 1');

	//注册TypeLib
	FRegini.WriteString(strRegMP3Path + '\TypeLib', '', '{3B00B10A-6EF0-11D1-A6AA-0020AFE4DE54}');

	//注册Version
	FRegini.WriteString(strRegMP3Path + '\Version', '', '1.0');

	FRegini.Free;  //关闭注册表
end;

procedure TFormMP3Play.OpenMP3;
var i : integer;
	s : string;
begin
	//读入音乐文件
	ListBoxSoundName.Items.Clear;
	cbCurrentFilename.Items.Clear;

	i := 0;
	s := RegReadStr(c_strRegSoundPath, c_strRegSoundFilename + IntToStr(i), '');

	while (s <> '') do
	begin
		ListBoxSoundName.Items.Add(s);
		cbCurrentFilename.Items.Add(IntToStr(i + 1) + '. ' + ExtractFileName(s));

		inc(i);
		s := RegReadStr(c_strRegSoundPath, c_strRegSoundFilename + IntToStr(i), '');
	end;

	//背景音乐重复播放标志
	BtnRepeat.Down := RegReadBool(c_strRegSoundPath, c_strRegSoundRepeat, False);

	g_boolExistMP3 := True;     //存在MP3控件
	FState := stEject;          //打开状态
	FboolChangeSeek := True;    //可以拖动

	BtnStopClick(Self);
end;

procedure TFormMP3Play.EnableBtn(Enable : Boolean);
begin
	BtnPlay.Enabled := Enable;
	BtnStop.Enabled := Enable;
	BtnFirst.Enabled := Enable;
	BtnLast.Enabled := Enable;
	TrackBar.Enabled := Enable;
end;

function TFormMP3Play.isHaveSound : Boolean;
begin
	Result := (ListBoxSoundName.Items.Count > 0);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -