【问题标题】:Am I loading this Music correctly with Bass.dll?我是否使用 Bass.dll 正确加载了此音乐?
【发布时间】:2014-02-20 20:50:39
【问题描述】:

根据 Bass 的文档,我正在尝试使用以下代码加载 ogg 文件:

var
  FFile : string;
  Music: HSAMPLE; 
  ch: HCHANNEL;
  OpenDialog1 : TOpenDialog;
begin
  Dynamic_Bass.Load_BASSDLL('Library/Bass.dll');
  Dynamic_Bass.BASS_Init(1,44000,Bass_DEVICE_SPEAKERS,0,nil);  
  OpenDialog1 := TOpenDialog.Create(nil);
  if not OpenDialog1.Execute then
    Exit;
  ffile := OpenDialog1.FileName;
  Music := BASS_SampleLoad(FALSE, PChar(ffile), 0, 0, 3, BASS_SAMPLE_OVER_POS);
  ch := BASS_SampleGetChannel(Music, False);
  BASS_ChannelSetAttribute(ch, BASS_ATTRIB_PAN, 0);
  BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, 1);
  BASS_ChannelPlay(ch, False);
  ShowMessage(IntToStr(BASS_ErrorGetCode));
end;

ShowMessage 返回 5。根据文档,这是句柄错误,这意味着错误在 music 变量中。如果我评论BASS_SampleLoad 下面的行,我会收到错误 2。意味着无法加载文件。所以它是一个普通的OGG文件。所以我的问题是:我做错了什么吗?

提前致谢。

【问题讨论】:

  • 您似乎没有检查您调用的函数的返回值
  • 您必须正确使用该库。在您实施正确的错误检查之前,我不会尝试提供帮助。 Chuck Norris 可以不检查返回值而侥幸逃脱。我们其余的人?没那么多。
  • 请在你打开的对话框中使用try..finally!!
  • 交叉发帖是怎么回事? pt.stackoverflow.com/questions/6778/…
  • @whosrdaddy 我刚刚读到 Stack Team 的这篇文章:“我们希望葡萄牙语网站上提出的几乎所有问题都会在英文网站上被提出(并回答)”也许我解释错了?关于冒犯,在我看来,如果我选择你的答案而不是他们的答案,人们也会以同样的方式被冒犯。

标签: delphi delphi-xe3 ogg bass


【解决方案1】:

您的代码似乎是正确的,只需确保在每次调用时检查错误即可。

固定代码:

var
  ffile : string;
  music: HSAMPLE; 
  ch: HCHANNEL;
  opendialog1 : topendialog;

begin 
 if not Dynamic_Bass.Load_BASSDLL('Library/Bass.dll') then
  Exit;
 // change device to -1 which is the default device
 if Dynamic_Bass.BASS_Init(-1,44000,Bass_DEVICE_SPEAKERS,0,nil) then
  begin
   opendialog1 := topendialog.Create(nil);
   try
    if OpenDialog1.Execute then
     begin
      ffile := OpenDialog1.FileName;
      music := BASS_SampleLoad(FALSE, PChar(ffile), 0, 0, 3, BASS_SAMPLE_OVER_POS);
      if music <> 0 then
       begin
        ch := BASS_SampleGetChannel(music, False);
        if ch <> 0 then
         begin
          // omitted, see if the basics work   
          // BASS_ChannelSetAttribute(ch, BASS_ATTRIB_PAN, 0);
          // BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, 1);
          if not BASS_ChannelPlay(ch, False) then
           ShowMessage(inttostr(bass_errorgetcode));
         end
        else
         ShowMessage(inttostr(bass_errorgetcode));
       end
      else
       ShowMessage(inttostr(bass_errorgetcode));
     end;
   finally  
    OpenDialog1.Free;
   end;
  end
 else
  ShowMessage(inttostr(bass_errorgetcode));
end;

更新

我应该早点看到,你的代码失败了,因为BASS_SampleLoad 需要 ANSI 格式的文件名the documentation clearly mentions this,因为你有 Delphi XE3 并且字符串是 Unicode,你必须提供 BASS_UNICODE 标志来表明这一点。

所以Bass_SampleLoad 行变为:

music := BASS_SampleLoad(FALSE, PChar(ffile), 0, 0, 3, BASS_SAMPLE_OVER_POS 
          or BASS_UNICODE);

更新2

根据鲁弗爵士的要求: 我添加了一个基于异常的错误检查例程,使代码更简洁,调试更直接。

function BASS_ErrorToString(BASS_ErrorCode : Integer) : String;

begin

 case BASS_ErrorCode of :
  0: Result := 'BASS_OK';
  1: Result := 'BASS_ERROR_MEM';
  2: Result := 'BASS_ERROR_FILEOPEN';
  3: Result := 'BASS_ERROR_DRIVER';
  4: Result := 'BASS_ERROR_BUFLOST';
  5: Result := 'BASS_ERROR_HANDLE';
  6: Result := 'BASS_ERROR_FORMAT';
  7: Result := 'BASS_ERROR_POSITION';
  8: Result := 'BASS_ERROR_INIT';
  9: Result := 'BASS_ERROR_START';
  14: Result := 'BASS_ERROR_ALREADY';
  18: Result := 'BASS_ERROR_NOCHAN';
  19: Result := 'BASS_ERROR_ILLTYPE';
  20: Result := 'BASS_ERROR_ILLPARAM';
  21: Result := 'BASS_ERROR_NO3D';
  22: Result := 'BASS_ERROR_NOEAX';
  23: Result := 'BASS_ERROR_DEVICE';
  24: Result := 'BASS_ERROR_NOPLAY';
  25: Result := 'BASS_ERROR_FREQ';
  27: Result := 'BASS_ERROR_NOTFILE';
  29: Result := 'BASS_ERROR_NOHW';
  31: Result := 'BASS_ERROR_EMPTY';
  32: Result := 'BASS_ERROR_NONET';
  33: Result := 'BASS_ERROR_CREATE';
  34: Result := 'BASS_ERROR_NOFX';
  37: Result := 'BASS_ERROR_NOTAVAIL';
  38: Result := 'BASS_ERROR_DECODE';
  39: Result := 'BASS_ERROR_DX';
  40: Result := 'BASS_ERROR_TIMEOUT';
  41: Result := 'BASS_ERROR_FILEFORM';
  42: Result := 'BASS_ERROR_SPEAKER';
  43: Result := 'BASS_ERROR_VERSION';
  44: Result := 'BASS_ERROR_CODEC';
  45: Result := 'BASS_ERROR_ENDED';
  46: Result := 'BASS_ERROR_BUSY';
 else
  Result := 'BASS_ERROR_UNKNOWN';
 end;
end;

procedure BassResultCheck(ResultCode : Integer);
begin
 if ResultCode = 0 then
  begin
   ResultCode := BASS_ErrorGetCode;
   raise Exception.CreateFmt('BASS error : %s(%d)', [BASS_ErrorToString(ResultCode), ResultCode]);
  end;
end;

procedure BassBoolCheck(const BoolResult : Boolean);
begin
 BassResultCheck(Integer(BoolResult));
end;

procedure PlaySample(const SampleFilename : String);

var
  Sample : HSAMPLE; 
  Channel: HCHANNEL;


begin 
 if not Dynamic_Bass.Load_BASSDLL('Library/Bass.dll') then
  raise Exception.Create('Could not load BASS DLL');
 // change device to -1 which is the default device
 BassBoolCheck(Dynamic_Bass.BASS_Init(-1,44000,Bass_DEVICE_SPEAKERS,0,nil);
 Sample := BASS_SampleLoad(FALSE, PChar(SampleFilename), 0, 0, 3, BASS_SAMPLE_OVER_POS OR BASS_UNICODE);
 BassResultCheck(Sample);
 Channel := BASS_SampleGetChannel(Sample, False);
 BassResultCheck(Channel);
 BassBoolCheck(BASS_ChannelPlay(Channel, False));
end;

【讨论】:

  • 输出 2,无论如何都在问题中。在music &lt;&gt; 0 条件下。
  • 刚刚做到了!当然我没有把代码放到 GT 中,那会是一团糟 :)
  • 我喜欢那种白白获得 100 个 pontos 的感觉 :-) 你也得到了我的 voto。
  • 我会再次投票,但我已经投票了 - 这个问题首先得到了很好的回答。现在它是一颗宝石 :)
  • @Guil 无需在这里道歉!一条黄金法则,没有人永远不会老到不能每天学习新东西;)
猜你喜欢
  • 2012-05-27
  • 2022-01-10
  • 2011-07-14
  • 2014-07-19
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多