Delphi with Multimedia library Bass.dll play MP3 [3]

Source: Internet
Author: User
Tags bool

//播放前先要用 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 要指定为流的大小}

Form Design diagram:

Code files:

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; {Stream Handle}
Procedure Tform1.formcreate (Sender:tobject);
Begin
If HiWord (bass_getversion) <> Bassversion Then
MessageBox (0, "Bass.dll" file version is not suitable!) ', nil, mb_iconerror);
If not Bass_init ( -1, 44100, 0, 0, nil) then
ShowMessage (' initialization error ');
End
{Open MP3 file and create play stream}
Procedure Tform1.button1click (Sender:tobject);
Var
mp3path:ansistring;
Begin
{Open File}
Opendialog1.filter: = ' Mp3 file (*.mp3) |*.mp3| Wav file (*.wav) |*wav ';
If Opendialog1.execute Then
Mp3path: = ansistring (Opendialog1.filename);
{If there is already a file open, let it be released}
Bass_streamfree (HS);
{Create play Stream}
HS: = Bass_streamcreatefile (False, Pansichar (Mp3path), 0, 0, 0);
{Whether the success is turned on, show}
If HS < bass_error_ended Then
Text: = ' open failed ' else Text: = string (Mp3path);
End
Play
Procedure Tform1.button2click (Sender:tobject);
Begin
Bass_channelplay (HS, False); {Parameter 1 is a stream handle; parameter 2 will play from the beginning every time if True}
End
Suspended
Procedure Tform1.button3click (Sender:tobject);
Begin
Bass_channelpause (HS);
End
Stop
Procedure Tform1.button4click (Sender:tobject);
Begin
Bass_channelstop (HS);
End
Release
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Bass_free;
End
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.