//播放前先要用 BASS_Init 函數進行播放設備初始化
窗體設計圖:
function BASS_Init(
device: Integer; {指定輸出設備, 第一個是 1、第二個是 2; -1 表示使用當前設備}
freq: DWord; {采樣率, 一般是 44100}
flags: DWord; {是 BASS_DEVICE_MONO 等常量的組合值, 是效果參數; 0 是默認值}
win: HWND; {指定窗口句柄; 0 表示當前窗口}
clsid: PGUID {指定一個 GUID, 用以初始化 DirectSound; nil 表示使用默認}
): BOOL; stdcall; external bassdll;
//當然需要從文件或內存加載文件流以後才能播放
function BASS_StreamCreateFile(
mem: BOOL; {從文件載入這裡是 False; 從內存載入這裡是 True}
f: Pointer; {文件名或內存流的指針}
offset: QWord; {播放起始點, 單位是 1/10 毫米; 只在參數 1: mem = False 時有效; 默認是 0}
length: QWord; {播放終止點, 單位是 1/10 毫米; 只在參數 1: mem = False 時有效; 默認是 0}
flags: DWord {BASS_SAMPLE_3D 等參數的組合; 控制播放效果、反復和解碼等等}
): HSTREAM; stdcall; external bassdll;
{另外: 在調入內存流時, 參數 length 要指定為流的大小}
代碼文件:unit Unit1;
窗體文件:
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Bass;
var
hs: HSTREAM; {流句柄}
procedure TForm1.FormCreate(Sender: TObject);
begin
if HiWord(BASS_GetVersion) <> BASSVERSION then
MessageBox(0, '"Bass.dll" 文件版本不合適! ', nil, MB_ICONERROR);
if not BASS_Init(-1, 44100, 0, 0, nil) then
ShowMessage('初始化錯誤');
end;
{打開 mp3 文件, 並建立播放流}
procedure TForm1.Button1Click(Sender: TObject);
var
Mp3Path: AnsiString;
begin
{打開文件}
OpenDialog1.Filter := 'Mp3 文件(*.mp3)|*.mp3|Wav 文件(*.wav)|*wav';
if OpenDialog1.Execute then
Mp3Path := AnsiString(OpenDialog1.FileName);
{如果已有文件打開, 先要釋放它}
BASS_StreamFree(hs);
{建立播放流}
hs := BASS_StreamCreateFile(False, PAnsiChar(Mp3Path), 0, 0, 0);
{是否打開成功, 顯示一下}
if hs < BASS_ERROR_ENDED then
Text := '打開失敗' else Text := string(Mp3Path);
end;
{播放}
procedure TForm1.Button2Click(Sender: TObject);
begin
BASS_ChannelPlay(hs, False); {參數 1 是流句柄; 參數 2 若是 True 每次都會從頭播放}
end;
{暫停}
procedure TForm1.Button3Click(Sender: TObject);
begin
BASS_ChannelPause(hs);
end;
{停止}
procedure TForm1.Button4Click(Sender: TObject);
begin
BASS_ChannelStop(hs);
end;
{釋放}
procedure TForm1.FormDestroy(Sender: TObject);
begin
BASS_Free;
end;
end.object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 117
ClIEntWidth = 202
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 16
Top = 17
Width = 75
Height = 25
Caption = #25171#24320
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 112
Top = 17
Width = 75
Height = 25
Caption = #25773#25918
TabOrder = 1
OnClick = Button2Click
end
object Button3: TButton
Left = 112
Top = 48
Width = 75
Height = 25
Caption = #26242#20572
TabOrder = 2
OnClick = Button3Click
end
object Button4: TButton
Left = 112
Top = 79
Width = 75
Height = 25
Caption = #20572#27490
TabOrder = 3
OnClick = Button4Click
end
object OpenDialog1: TOpenDialog
Left = 40
Top = 56
end