CocosDenshion:为什么总是假(我能听到音乐)?

时间:2013-02-28 16:26:01

标签: ios cocos2d-iphone openal

我有这个:

-(ALuint)ID
{ return self.soundSource->_sourceId; }

-(void)play
{
    [self.engine playSound:self.soundSource.soundId
             sourceGroupId:0
                     pitch:self.pitch
                       pan:1.0
                      gain:1.0
                      loop:NO];
}

-(NSTimeInterval)duration
{ return durationOfSourceId(self.ID); }

-(NSTimeInterval)offset
{ return elapsedTimeOfSourceId(self.ID); }

-(BOOL)isPlaying
{
    NSTimeInterval secondsRemaining = self.duration - self.offset;
    NSLog(@"<%.3f>", self.soundSource.durationInSeconds);
    NSLog(@"<%.3f> - <%.3f> = <%.3f> isPlaying <%i>", self.duration, self.offset, secondsRemaining, self.soundSource.isPlaying);
    return (secondsRemaining > 0.0);
}


#pragma mark - OpenAL addons

static NSTimeInterval elapsedTimeOfSourceId(ALuint sourceID)
{
    float result = 0.0;
    alGetSourcef(sourceID, AL_SEC_OFFSET, &result);
    return result;
}

static NSTimeInterval durationOfSourceId(ALuint sourceID)
{
    //Thanks to http://stackoverflow.com/a/8822347

    ALint bufferID, bufferSize, frequency, bitsPerSample, channels;
    alGetSourcei(sourceID, AL_BUFFER, &bufferID);
    alGetBufferi(bufferID, AL_SIZE, &bufferSize);
    alGetBufferi(bufferID, AL_FREQUENCY, &frequency);
    alGetBufferi(bufferID, AL_CHANNELS, &channels);
    alGetBufferi(bufferID, AL_BITS, &bitsPerSample);
    NSTimeInterval result = ((double)bufferSize)/(frequency*channels*(bitsPerSample/8));
    return result;
}

引擎只是CDSoundEngine的一个实例。我真的想知道音乐什么时候会停止。我现在已经整整一天了,我很累。

记录: [1445:707]&lt; 1.656&gt; [1445:707]&lt; 1.656&gt; - &lt; 0.000&gt; =&lt; 1.656&gt; isPlaying&lt; 0&gt;

所以OpenAL源ID是正确的(因为我可以得到持续时间)。 CDSoundSource也是正确的(因为我也可以从中得到持续时间)。 我能听到声音播放。

但AL_SEC_OFFSET始终为0.0,isPlaying始终为NO。

1 个答案:

答案 0 :(得分:1)

为什么不获取源的状态并检查它是否真的被播放?:

alGetSourcei(source, AL_SOURCE_STATE, &state);

return (state == AL_PLAYING);
相关问题