title: Delphi: load BASS DLL and play MP3
body: <p>I want to load a <a href="http://www.un4seen.com/" rel="nofollow">bass dll</a> manually from somewhere and then play a mp3 file. How to do that? Delphi XE2.</p>
  
  <p>My attempt doesn't work:</p>
  
  <pre><code>type
    QWORD = Int64;
    HSTREAM = LongWord;
  type
    TBASS_ChannelPlay = function(handle: HSTREAM; restart: LongBool): LongBool; stdcall;
  
  type
    TBASS_StreamCreateFile = function(mem: LongBool; f: Pointer;
      offset, length: QWORD; flags: LongWord): HSTREAM; stdcall;
  
  procedure PlayMP3(FileName: string);
  var
    BASS_ChannelPlay: TBASS_ChannelPlay;
    BASS_StreamCreateFile: TBASS_StreamCreateFile;
    DllHandle: THandle;
    MP3Stream: HSTREAM;
    DllPath:string;
    pFileName:pchar;
  begin
    DllPath:= 'c:\dll\bass.dll';
    DllHandle := LoadLibrary(pchar(DLLPath));
    try
      if DllHandle = 0 then
        Exit;
      @BASS_StreamCreateFile := GetProcAddress(DllHandle,
        'BASS_StreamCreateFile');
      if @BASS_StreamCreateFile &lt;&gt; nil then
      begin
        pfilename:=pchar(FileName);
        MP3Stream := BASS_StreamCreateFile(false, Pfilename, 0, 0, 0);
        if MP3Stream &lt;&gt; 0 then
        begin
          @BASS_ChannelPlay := GetProcAddress(DllHandle, 'BASS_ChannelPlay');
          if @BASS_ChannelPlay &lt;&gt; nil then
            BASS_ChannelPlay(MP3Stream,false);
        end;
      end;
    finally
      FreeLibrary(DllHandle);
    end;
  end;
  </code></pre>
  
  <p><strong>But BASS_StreamCreateFile returns 0 all time :(</strong></p>
  
  <p>The code from their example (Bass Test) and Bass.pas:</p>
  
  <pre><code>type
    DWORD = LongWord;
    BOOL = LongBool;
    QWORD = Int64;
    HSTREAM = DWORD;
  
  function BASS_StreamCreateFile(mem: BOOL; f: Pointer; offset, length: QWORD; flags: DWORD): HSTREAM; {$IFDEF WIN32}stdcall{$ELSE}cdecl{$ENDIF}; external bassdll;
  
  strs: array[0..128] of HSTREAM;
  
  procedure TForm1.Button15Click(Sender: TObject);
  var
      f: PChar;
  begin
      if not OpenDialog2.Execute then Exit;
      f := PChar(OpenDialog2.FileName);
      strs[strc] := BASS_StreamCreateFile(False, f, 0, 0, 0 {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF});
      if strs[strc] &lt;&gt; 0 then
      begin
          ListBox3.Items.Add(OpenDialog2.FileName);
          Inc(strc);
      end
      else
          Error('Error creating stream!');
  end;
  </code></pre>
  
  <h2>Added</h2>
  
  <pre><code>  MP3Stream := BASS_StreamCreateFile(false, Pfilename, 0, 0, 0);
    ShowMessage(SysErrorMessage(GetLastError));
  </code></pre>
  
  <p><code>GetLastError</code> doesn't show any errors!</p>
  
  <h2>Added</h2>
  
  <p>There is BASS 2.4 Delphi unit (dynamic) too.</p>
  
  <p>It's written:</p>
  
  <blockquote>
    <p>How to install    Copy DYNAMIC_BASS.PAS to the \LIB subdirectory of
    your Delphi path or your project dir   Call Load_BASSDLL (eg. in
    FormCreate) to load BASS before using any functions, and<br>
    Unload_BASSDLL (eg. in FormDestory) to unload it when you're done.</p>
    
    <p>NOTE: Delphi 2009 users should use the BASS_UNICODE flag where
    possible</p>
  </blockquote>
  
  <p>I do:</p>
  
  <pre><code> if Load_BASSDLL(DllPath) then
    begin
    FileName:='C:\Users\V\Desktop\cmd.mp3';
    MP3Stream:=BASS_StreamCreateFile(False, pchar(FileName), 0, 0, 0 {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF});
      if MP3Stream &lt;&gt; 0 then
       BASS_ChannelPlay(MP3Stream, False);
       Unload_BASSDLL;
    end;
  </code></pre>
  
  <p>It still doesn't work :(</p>
  
  <h2>Added:</h2>
  
  <p>If to do </p>
  
  <pre><code>BASS_Init(-1, 44100, 0, Form1.Handle, nil);
  </code></pre>
  
  <p>then <code>BASS_ChannelPlay(MP3Stream, False)</code> returns true but I don't hear a sound:(</p>