Author Topic: audio play is not working  (Read 84 times)

draggdh

  • Posts: 2
audio play is not working
« on: 29 Jan '25 - 11:35 »
when I click PlayButton_Label1, bass library error 5 keeps happening.


<code>---------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  deviceInfo: BASS_DEVICEINFO;
begin
  // Initialize streams and devices
  for I := 0 to 4 do
  begin
    FStreams := 0;
    FDevices := -1; // Default to no device selected
end;


  i := 1; // Start from 1 to skip the "No sound" device
  while BASS_GetDeviceInfo(i, deviceInfo) do
  begin
    console.Lines.Add('device ' + IntToStr(i) + ': ' + deviceInfo.name);
    Inc(i);
  end;
end;

procedure TForm1.source_button1Click(Sender: TObject);
var
  DeviceIndex: Integer;
begin
  DeviceIndex := (Sender as TLabel).Tag;

  if OpenDialog.Execute then
  begin
    FFileNames[DeviceIndex] := OpenDialog.FileName;
    console.Lines.Add('file was set ' + IntToStr(DeviceIndex + 1) + ': ' + FFileNames[DeviceIndex] + 'error code' + IntToStr(BASS_ErrorGetCode));
  end;
end;



procedure TForm1.PlayButton_Label1Click(Sender: TObject);
var
  DeviceIndex: Integer;
begin
  DeviceIndex := (Sender as TLabel).Tag;

  BASS_Free;

  if not BASS_Init(FDevices[DeviceIndex], 44100, BASS_DEVICE_LATENCY, Handle, nil) then
  begin
    console.Lines.Add('init failed' + 'error code' + IntToStr(BASS_ErrorGetCode));
    console.Lines.Add('device' + IntToStr(FDevices[DeviceIndex]));
    console.Lines.Add('file' + FFilenames[DeviceIndex]);
  end;


  console.Lines.Add('device' + IntToStr(FDevices[DeviceIndex]));
  console.Lines.Add('파일 위치' + FFilenames[DeviceIndex]);

  BASS_SetDevice(FDevices[DeviceIndex]);
  FStreams[DeviceIndex] := BASS_StreamCreateFile(False, PChar(FFilenames[DeviceIndex]), 0, 0, BASS_SAMPLE_LOOP);
  if not BASS_ChannelPlay(FStreams[DeviceIndex], False) then
  begin
    console.Lines.Add('장치 1 초기화 실패' + '에러  코드(0이여야 정상)' + IntToStr(BASS_ErrorGetCode));
  end;
end;



//재생 정지
procedure TForm1.StopButton_Label1Click(Sender: TObject);
var
  DeviceIndex: Integer;
begin
  DeviceIndex := (Sender as TLabel).Tag;

  if FStreams[DeviceIndex] <> 0 then
  begin
    BASS_ChannelStop(FStreams[DeviceIndex]);
    BASS_StreamFree(FStreams[DeviceIndex]);
    FStreams[DeviceIndex] := 0;

    console.Lines.Add('device' + IntToStr(DeviceIndex + 1 + 'stop'));
  end;
end;



procedure TForm1.device_button1Click(Sender: TObject);
var
  DeviceIndex: Integer;
  DeviceList: TStringList;
  I: Integer;
  DeviceInfo: BASS_DEVICEINFO;
  SelectedDevice: string;
  SelectedDeviceIndex: Integer;
begin
  DeviceIndex := (Sender as TLabel).Tag;

  DeviceList := TStringList.Create;
  try
    I := 1;
    while BASS_GetDeviceInfo(I, DeviceInfo) do
    begin
      DeviceList.Add(IntToStr(I) + ': ' + DeviceInfo.name);
      Inc(I);
    end;

    if DeviceList.Count = 0 then
    begin
      console.Lines.Add('no output device');
      Exit;
    end;

    SelectedDevice := InputBox('출력 장치 선택', '사용 가능한 장치:' + sLineBreak + DeviceList.Text, '');
    if TryStrToInt(SelectedDevice, SelectedDeviceIndex) and (SelectedDeviceIndex > 0) and (SelectedDeviceIndex <= DeviceList.Count) then
    begin
      FDevices[DeviceIndex] := SelectedDeviceIndex;

      // Free the previous device if it was initialized
      if BASS_GetDevice = SelectedDeviceIndex then
        BASS_Free;

      // Initialize BASS for the selected device
      if BASS_Init(SelectedDeviceIndex, 44100, 0, 0, nil) then
        console.Lines.Add('device ' + SelectedDevice + '가 장치 ' + IntToStr(DeviceIndex + 1) + '에 대해 선택되었습니다. 디버깅 코드(0이여야 정상)' + IntToStr(BASS_ErrorGetCode))
      else
        console.Lines.Add('장치 ' + SelectedDevice + ' init failed - error code: ' + IntToStr(BASS_ErrorGetCode));
    end
    else
    begin
      console.Lines.Add('wrong device.');
    end;
  finally
    DeviceList.Free;
  end;
end;

Chris

  • Posts: 2222
Re: audio play is not working
« Reply #1 on: 29 Jan '25 - 14:25 »
You should change the following
FStreams[DeviceIndex] := BASS_StreamCreateFile(False, PChar(FFilenames[DeviceIndex]), 0, 0, BASS_SAMPLE_LOOP);
to
FStreams[DeviceIndex] := BASS_StreamCreateFile(False, PWideChar(FFilenames[DeviceIndex]), 0, 0, BASS_SAMPLE_LOOP or BASS_UNICODE);

by the way is it delphi (which version ?) or Lazarus ?

draggdh

  • Posts: 2
Re: audio play is not working
« Reply #2 on: 30 Jan '25 - 13:25 »
Thanks for your help!