Author Topic: Playing MP3 files in the Lazarus IDE  (Read 154 times)

USERPC01

  • Posts: 26
Playing MP3 files in the Lazarus IDE
« on: 10 Aug '18 - 20:56 »
Simple example ( using examples from te Internet ) for BASS 2.0

project1.lpr
Code: [Select]
program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, Unit1
  { you can add units after this };

{$R *.res}

begin
  RequireDerivedFormResource:=True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.


unit1.pas
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation
uses LBass; // <--- Подключаем BASS

{$R *.lfm}

var
  sound : TBass;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  sound.InitBass;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 // sound.load(ExtractFilePath(ParamStr(0)) + 'music' + PathDelim + 'first.mp3', 1);
  sound.load( 'first.mp3', 1);
  sound.play(1, True);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  //sound.load(ExtractFilePath(ParamStr(0)) + 'music' + PathDelim + 'second.mp3', 2);
  sound.load( 'second.mp3', 2);
  sound.play(2, True);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  sound.DoneBass;
end;

end.



USERPC01

  • Posts: 26
Re: Playing MP3 files in the Lazarus IDE
« Reply #1 on: 10 Aug '18 - 20:57 »
unit1.lfm

Code: [Select]
object Form1: TForm1
  Left = 1137
  Height = 300
  Top = 271
  Width = 400
  Caption = 'Form1'
  ClientHeight = 300
  ClientWidth = 400
  DesignTimePPI = 120
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  LCLVersion = '1.8.4.0'
  object Button1: TButton
    Left = 135
    Height = 31
    Top = 149
    Width = 94
    Caption = 'Button1'
    OnClick = Button1Click
    TabOrder = 0
  end
  object Button2: TButton
    Left = 144
    Height = 31
    Top = 192
    Width = 94
    Caption = 'Button2'
    OnClick = Button2Click
    TabOrder = 1
  end
end


LBass.pas
Code: [Select]
{$mode delphi}
Unit LBass;
 // version 0.2

INTERFACE
 Uses Windows,
      BassFPC;

 Const
      MaxStream = 30;

 type
     TBass = object
      // Inum      : Integer;
      Stream    : Array[1..MaxStream] of  HStream;
      Procedure InitBass;
      Procedure DoneBass;
       // wav,mp3,ogg
      Procedure load(name:string;num:integer);
      Procedure play(num:integer;Loop:boolean);
      Procedure stop(num:integer);
       // mod,s3m,xm,mod
      Procedure load_music(name:string;num:integer;Loop:boolean);
      Procedure play_music(num:integer);
     end;


IMPLEMENTATION

// TBass.
Procedure TBass.InitBass;
 Begin
   if BASS_GetVersion() <> MAKELONG(2,0) then begin Halt; end;
   if not BASS_Init(1, 44100, 0, 0, nil) then begin end;
 End;

Procedure TBass.DoneBass;
 var inum: integer;
 Begin
  for Inum:= 1 to MaxStream do begin
                                BASS_StreamFree(stream[Inum]);
                               end;
  BASS_Free();
 End;

Procedure TBass.load(name:string;num:integer);
 var
   p  :  PChar;
 Begin
   p := PChar(name);

   stream[num] :=BASS_StreamCreateFile(FALSE,p,0,0,0);
   if (stream[num] = 0) then begin end;
 End;

Procedure TBass.play(num:integer;Loop:boolean);
 Var Lf: byte;
 Begin
   if Loop = True then begin Lf:=4; end
                                     else
                       begin Lf:=0; end;
   if not BASS_StreamPlay(stream[num], FALSE, Lf) then begin end;
 End;

procedure TBass.stop(num:integer);
 Begin
   BASS_ChannelStop(stream[num]);
 End;

Procedure TBass.load_music(name:string;num:integer;Loop:boolean);
 var
   p  : PChar;
   Lf : byte;
 Begin
   p := PChar(name);
   if Loop = True then begin Lf:=BASS_MUSIC_LOOP; end
                                                   else
                       begin Lf:=0; end;
   stream[num] :=BASS_MusicLoad(FALSE,p,0,0,Lf,0);
   if (stream[num] = 0) then begin end;
 End;

Procedure TBass.play_music(num:integer);
 Begin
   if not BASS_MusicPlay(stream[num]) then begin end;
 End;
end.



USERPC01

  • Posts: 26
Re: Playing MP3 files in the Lazarus IDE
« Reply #2 on: 10 Aug '18 - 21:02 »
bassfpc.pas , bass.dll 2.0

USERPC01

  • Posts: 26
Re: Playing MP3 files in the Lazarus IDE
« Reply #3 on: 10 Aug '18 - 21:03 »
How to rebuild for Lazarus (Windows/Linux ) for bass24 ?