Music Player using BASS library Delphi tutorial (Tip for 10 )


Delphi mp3 music player

Example how to create mp3 player with Delphi and BASS library.

BASS library can be downloaded from :
http://www.un4seen.com/

In this video you can see in detail how to create such mp3 player.


 
And following is source code of this example.




(code style formatted by http://hilite.me/ )


unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, bass;

type
  TfrmMain = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Stop: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure StopClick(Sender: TObject);
  private
    procedure Error(msg: string);
  public
    channel:HSTREAM;
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.Error(msg: string);
var
 s: string;
begin
 s := msg + #13#10 + '(Error code: ' + IntToStr(BASS_ErrorGetCode) + ')';
 MessageBox(Handle, PChar(s), nil, 0);
end;

procedure TfrmMain.Button1Click(Sender: TObject);
var
i:Integer;
begin
if OpenDialog1.Execute() then
begin
ListBox1.Items.Clear;
BASS_ChannelStop(channel);
 for I := 0 to OpenDialog1.Files.Count-1 do
 begin
 ListBox1.Items.Add(OpenDialog1.Files[i]);
 end;
end;

end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin
if channel>0 then begin
BASS_StreamFree(channel);  //destroy any previous channel if exists
channel:=0;
end;
channel := BASS_StreamCreateFile(FALSE, PChar(ListBox1.Items[ListBox1.ItemIndex]), 0, 0,BASS_UNICODE);
//create the stream
BASS_ChannelPlay(channel,false); // play the stream
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
if ListBox1.ItemIndex>0 then
ListBox1.Selected[ListBox1.ItemIndex-1]:=True
else if ListBox1.ItemIndex=0 then
ListBox1.Selected[ListBox1.Items.Count-1]:=True;
Button2.Click;
end;

procedure TfrmMain.Button4Click(Sender: TObject);
begin
if ListBox1.ItemIndex<ListBox1.Items.Count-1 then
ListBox1.Selected[ListBox1.ItemIndex+1]:=True
else if ListBox1.ItemIndex=ListBox1.Items.Count-1 then
ListBox1.Selected[0]:=True;
Button2.Click;
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 // Close BASS
 BASS_Free();
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  // check the correct BASS was loaded
 if (HIWORD(BASS_GetVersion) <> BASSVERSION) then
 begin
  MessageBox(0,'An incorrect version of BASS.DLL was loaded',nil,MB_ICONERROR);
  Halt;
 end;

 // Initialize audio - default device, 44100hz, stereo, 16 bits
 if not BASS_Init(-1, 44100, 0, Handle, nil) then
  Error('Error initializing audio!');
end;

procedure TfrmMain.StopClick(Sender: TObject);
begin
BASS_ChannelStop(channel);
end;

end.
Share on Google Plus

About Clear SoftX

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.