Many people asked me of what they need to make a media player in delphi.
So i write these tutorial that anybody can create it’s own media player.
What you’ll need:
1) Delphi
2) BASS.DLL(http://un4seen.com)
What is BASS?
BASS is an audio library for use in Windows and Mac OSX software. Its purpose is to provide developers with powerful and efficient sample, stream (MP3, MP2, MP1, OGG, WAV, AIFF, custom generated, and more via add-ons), MOD music (XM, IT, S3M, MOD, MTM, UMX), MO3 music (MP3/OGG compressed MODs), and recording functions. All in a tiny DLL, under 100KB* in size.
Now lets get started
1) Create a new project
2)In the form create function we must initialize BASS.DLL
begin
BASS_Init(-1,44100,0,handle,0); //initialize bass
end;
Now let’s go to play section.
Put a open dialog, a scrollbar and a button to the form.
On the button on click function do something like this:
channel: HSTREAM;
begin
OpenDialog.Title := 'Open Files';
OpenDialog.Filter := 'All files|*.*';
if channel0 then
begin
BASS_StreamFree(channel); //destroy any previous channel if exists
channel:=0;
end;
if not OpenDialog.Execute then exit;
Channel := BASS_StreamCreateFile(FALSE, PChar(OpenDialog.FileName), 0, 0,BASS_UNICODE); //create the stream
scrollbar1.Max:=round(BASS_ChannelBytes2Seconds(channel, BASS_ChannelGetLength(channel, BASS_POS_BYTE))); //retrieve media length in seconds
BASS_ChannelPlay(channel,0); // play the stream
end;
Now if you Pause,Stop functions:
For pause you’ll need to call this function:
To stop you’ll need to call this function:
Of course you’ll need to set position to the song. This is a very simple task:
Now let’s say you want to display a waveform like winamp does. No problem, this can be done like this:
1) create a new unit and put this code in it.
{ Oscilloscope Visualyzation by Alessandro Cappellozza
version 0.8 05/2002
http://digilander.iol.it/Kappe/audioobject
}
interface
uses Windows, Dialogs, Graphics, SysUtils, CommonTypes, Classes;
type TOcilloScope = Class(TObject)
private
VisBuff : TBitmap;
BackBmp : TBitmap;
BkgColor : TColor;
ScopeOff : Integer;
PenColor : TColor;
DrawType : Integer;
DrawRes : Integer;
FrmClear : Boolean;
UseBkg : Boolean;
public
Constructor Create (Width, Height : Integer);
procedure Draw(HWND : THandle; WaveData : TWaveData; X, Y : Integer);
procedure SetBackGround (Active : Boolean; BkgCanvas : TGraphic);
property BackColor : TColor read BkgColor write BkgColor;
property Offset : Integer read ScopeOff write ScopeOff;
property Pen : TColor read PenColor write PenColor;
property Mode : Integer read DrawType write DrawType;
property Res : Integer read DrawRes write DrawRes;
property FrameClear : Boolean read FrmClear write FrmClear;
end;
var OcilloScope : TOcilloScope;
implementation
Constructor TOcilloScope.Create(Width, Height : Integer);
begin
VisBuff := TBitmap.Create;
BackBmp := TBitmap.Create;
VisBuff.Width := Width;
VisBuff.Height := Height;
BackBmp.Width := Width;
BackBmp.Height := Height;
BkgColor := clBlack;
ScopeOff := 50;
PenColor := clWhite;
DrawType := 0;
DrawRes := 1;
FrmClear := True;
UseBkg := False;
end;
procedure TOcilloScope.SetBackGround (Active : Boolean; BkgCanvas : TGraphic);
begin
UseBkg := Active;
BackBmp.Canvas.Draw(0, 0, BkgCanvas);
end;
procedure TOcilloScope.Draw(HWND : THandle; WaveData : TWaveData; X, Y : Integer);
var i, YPos : LongInt; R, L : SmallInt;
begin
if FrmClear then begin
VisBuff.Canvas.Pen.Color := BkgColor;
VisBuff.Canvas.Brush.Color := BkgColor;
if UseBkg then
BitBlt(VisBuff.Canvas.Handle, // Destination
0, 0, // X, Y (target pos)
VisBuff.Width, VisBuff.Height, // Size to copy
BackBmp.Canvas.handle, // Source
0, 0, // X, Y (source pos)
SrcCopy) // plain copy
else
VisBuff.Canvas.Rectangle(0, 0, VisBuff.Width, VisBuff.Height) // only if no background
end;
VisBuff.Canvas.Pen.Color := PenColor;
R := SmallInt(LOWORD(WaveData[0]));
L := SmallInt(HIWORD(WaveData[0]));
YPos := Trunc(((R + L) / (2 * 65535)) * ScopeOff) ;
VisBuff.Canvas.MoveTo(X , Y + YPos);
for i := 1 to 256 do begin
R := SmallInt(Loword(WaveData[i * DrawRes]));
L := SmallInt(HIword(WaveData[i * DrawRes]));
YPos := Trunc(((R + L) / (2 * 65535)) * ScopeOff) ;
case DrawType of
0 : VisBuff.Canvas.lineto(X + i, Y + YPos);
1 : begin
VisBuff.Canvas.MoveTo(X + i, Y);
VisBuff.Canvas.lineto(X + i, Y + YPos);
end;
2 : VisBuff.Canvas.Pixels[X + i, Y + YPos] := PenColor;
end;
end;
BitBlt(HWND, 0, 0, VisBuff.Width, VisBuff.Height, VisBuff.Canvas.Handle, 0, 0, srccopy)
end;
end.
2) Put a tpaintbox and a ttimer on the form and put this code to the timer. Also you’ll have to set timer interval to at least 30ms.
begin
if BASS_ChannelIsActive(Channel) BASS_ACTIVE_PLAYING then Exit;
BASS_ChannelGetData(Channel, @WaveData, 2048);
OcilloScope.Draw (PaintFrame.Canvas.Handle, WaveData,0,50);
This will display a nice waveform of the media that you are playing.
So…i end for now this tutorial, in the hope that you’ll apreciate it.


thanks a lot my friend,, really useful.,.
Thanks for this tutorial, it is very usefull.
BASS_ChannelPlay(channel,0); // play the stream
what is the problem this line?