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

Source: Internet
Author: User

This example effect chart:

Code files:

Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Extctrls, Comctrls;
Type
TForm1 = Class (Tform)
Opendialog1:topendialog;
Trackbar1:ttrackbar;
Timer1:ttimer;
Button1:tbutton;
Button2:tbutton;
Button3:tbutton;
Procedure Formcreate (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Button3click (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Timer1timer (Sender:tobject);
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Uses Bass;
Var
Hs:hstream; {Stream Handle}
{callback function: MYSYNC1; Although no parameters are used, it is written in the specified parameter format}
Procedure MySync1 (Handle:hsync; channel, data, User:dword); stdcall;
Begin
Form1.text: = ' start playing ';
End
{callback function: MySync2}
{Parameter 1: is a callback handle that can have Bass_channelsetsync returned and can be removed with Bass_channelremovesync}
{parameter 2: Stream handle}
{Parameter 3: not known}
{Parameter 4: is the user data specified by Bass_channelsetsync}
Procedure MySync2 (Handle:hsync; channel, data, User:dword); stdcall;
Begin
Form1.text: = ' past midpoint ';
{If you play to this (middle point) from the beginning again, you can do this:}
Bass_channelplay (channel, True);
End
{callback function: MySync3; Although no parameters are used, it must be specified as required}
Procedure MySync3 (Handle:hsync; channel, data, User:dword); stdcall;
Begin
Form1.text: = ' play finished ';
End
{Initialize}
Procedure Tform1.formcreate (Sender:tobject);
Begin
Trackbar1.height: = 23;
timer1.enabled: = False;
Timer1.interval: = 100;
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
Procedure Tform1.button1click (Sender:tobject);
Var
mp3path:ansistring;
mp3len:cardinal;
Begin
Bass_streamfree (HS);
timer1.enabled: = False;
Opendialog1.filter: = ' Mp3 file (*.mp3) |*.mp3| Wav file (*.wav) |*wav ';
If Opendialog1.execute Then
Mp3path: = ansistring (Opendialog1.filename);
HS: = Bass_streamcreatefile (False, Pansichar (Mp3path), 0, 0, 0);
If HS < bass_error_ended Then
Text: = ' Open failed '
ELSE begin
Text: = string (Mp3path);
{Get the total byte length of the stream first, useful below}
Mp3len: = Bass_channelgetlength (HS, Bass_pos_byte);
Trackbar1.max: = Mp3len;
{Start playback is callback MySync1}
{parameter 1: stream handle; parameter 2: parameter type; parameter 3: parameter value; parameter 4: to callback function pointer; parameter 5: User Data}
Bass_channelsetsync (HS, Bass_sync_pos, 0, @MySync1, nil);
{Callback MySync2} when playing to middle
Bass_channelsetsync (HS, Bass_sync_pos, Mp3len Div 2, @MySync2, nil);
{Callback MySync3} at end of playback
Bass_channelsetsync (HS, Bass_sync_end, 0, @MySync3, nil);
{Of course you can also write:}
Bass_channelsetsync (HS, Bass_sync_pos, Mp3len, @MySync3, nil);
{If the second parameter specifies Bass_sync_onetime, the callback function will execute only once}
Bass_channelsetsync (HS, bass_sync_end or bass_sync_onetime, 0, @MySync3, nil);
End
End
Play
Procedure Tform1.button2click (Sender:tobject);
Begin
Bass_channelplay (HS, False);
timer1.enabled: = True;
End
Suspended
Procedure Tform1.button3click (Sender:tobject);
Begin
Bass_channelpause (HS);
timer1.enabled: = False;
End
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Bass_free;
End
Procedure Tform1.timer1timer (Sender:tobject);
Begin
If Bass_channelisactive (HS) = Bass_active_playing Then
Trackbar1.position: = Bass_channelgetposition (HS, Bass_pos_byte);
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.