如何多次跳转到特定偏移量?

发布于 2024-07-12 03:46:50 字数 578 浏览 9 评论 0原文

想象一下这个函数:

void SoundManager::playSource(ALuint sourceID, float offset)
{
    alSourceStop(sourceID);

    ALint iTotal = 0;
    ALint iCurrent = 0;
    ALint uiBuffer = 0;
    alGetSourcei(sourceID, AL_BUFFER, &uiBuffer);
    alGetBufferi(uiBuffer, AL_SIZE, &iTotal);
    iCurrent = iTotal * offset;

    alSourcei(sourceID, AL_BYTE_OFFSET, iCurrent);
    alSourcePlay(sourceID);
}

这个想法是调用 playSource(x, 0.5f) 会跳转到(大约)缓冲区的一半,等等。

我第一次调用它时它工作正常,但如果我调用它再次在同一源上(无论该源是否正在播放)它开始播放,就好像我用偏移量 0 调用它一样。

有什么想法吗?

Imagine this function:

void SoundManager::playSource(ALuint sourceID, float offset)
{
    alSourceStop(sourceID);

    ALint iTotal = 0;
    ALint iCurrent = 0;
    ALint uiBuffer = 0;
    alGetSourcei(sourceID, AL_BUFFER, &uiBuffer);
    alGetBufferi(uiBuffer, AL_SIZE, &iTotal);
    iCurrent = iTotal * offset;

    alSourcei(sourceID, AL_BYTE_OFFSET, iCurrent);
    alSourcePlay(sourceID);
}

The idea is calling playSource(x, 0.5f) would jump to (roughly) halfway through the buffer, etc.

It works fine the first time I call it, but if I call it again on the same source (whether that source is playing or not) it begins playing as though I'd called it with offset 0.

Any ideas why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2024-07-19 03:46:50

解决了!

尽管 API 声称设置偏移量适用于任何状态的源,但问题是我应该在开始时调用 alSourceRewind 而不是 alSourceStop

看来设置偏移量仅适用于处于 AL_INITIAL 状态的源。

Solved!

Even though the API claims that setting the offsets works on sources in any state, the problem was I should have been calling alSourceRewind instead of alSourceStop at the start.

It seems setting offsets only works on sources in the AL_INITIAL state.

离去的眼神 2024-07-19 03:46:50

你可以先播放音频文件,然后像这样调用setOffset,不需要调用alSourcePlay

- (BOOL)setOffset:(float)offset
{
  ALint iTotal = 0;
  ALint iCurrent = 0;
  ALint uiBuffer = 0;
  alGetSourcei(_sourceID, AL_BUFFER, &uiBuffer);
  alGetBufferi(uiBuffer, AL_SIZE, &iTotal);
  iCurrent = iTotal * offset;
  alSourcei(_sourceID, AL_BYTE_OFFSET, iCurrent);
  return ((_error = alGetError()) != AL_NO_ERROR);
}

you can play the audio file first,then call setOffset like this,don't need to call alSourcePlay:

- (BOOL)setOffset:(float)offset
{
  ALint iTotal = 0;
  ALint iCurrent = 0;
  ALint uiBuffer = 0;
  alGetSourcei(_sourceID, AL_BUFFER, &uiBuffer);
  alGetBufferi(uiBuffer, AL_SIZE, &iTotal);
  iCurrent = iTotal * offset;
  alSourcei(_sourceID, AL_BYTE_OFFSET, iCurrent);
  return ((_error = alGetError()) != AL_NO_ERROR);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文