views:

322

answers:

4

Hi

When does the AVAudioPlayer's currentTime method return a negative value? The audio file is playing (I am putting in a check before getting currentTime) but making a call to currentTime returns a negative value.

Any ideas? Thanks

if(thePlayer != nil && [thePlayer isPlaying]){
    double playerTime = [thePlayer currentTime];
    NSLog(@"Player Time: %f", playerTime);
}

Output

Player Time: -0.019683
+1  A: 

Are you testing this on the simulator? There are several bugs with AVAudioPlayer on the simulator. One is that the currentTime can be a very large positive or negative number. Even if you set the currentTime to a particular number, it will still often show something different. As far as I know this is only an issue on the simulator and not when running on a device.

Here is the code I use to set the currentTime property of an AVAudioPlayer instance:

- (void)safeSetCurentTime:(NSTimeInterval)newTime {
self._player.currentTime = newTime;
if (self._player.currentTime != newTime)
{
    // code falls through to here all the time
            // the second attempt _usually_ works.
    [self prepareAudioForPlayback];
    self._player.currentTime = newTime;
    //NSLog(@"Set time failed");
}

}

Harkonian
I got this issue when testing on a device. Was logging the current time and this is what came up in the console. Thanks.
lostInTransit
I just ran into this issue as well while testing my app on the iPad. There is definitely a bug in AVAudioPlayer in regards to setting the time and getting the current time. In my code, I've placed some extra protection around setting that property. It falls through to the failure area often. I'll edit my answer above to include the code.
Harkonian
Harkonian, with your protection code, do you actually ever get it to succeed and give you the correct time? I'm seeing it fail every time.
NeilDurant
Neil -- Yes, it does succeed sometimes. I haven't perused determining in depth what causes it to succeed or fail. Rather than trying to make this work on the simulator I simply started testing my audio player code on the device.
Harkonian
A: 

Are you still having this problem? I'm seeing the same thing. On the device, even when I specifically set the currentTime to 0. I can log the currentTime and it is zero. But when I play, it still jumps back about .04 seconds and then starts.

Did you find a solution?

Eric Christensen
haven't found a solution yet. Neither do I know why this happens :(
lostInTransit
A: 

I believe this issue is fixed the iOS4 beta 2 SDK release, so you shouldn't see it on the iPhone. See here. However, I think we're stuck with the problem on the iPad until iOS4 is available on that device.

Anyone know of a workaround? Any way to predict how the current time will be incorrectly reported, so a correction factor can be applied? What I'm seeing is that the current time is reported to be a few seconds behind the actual playback time (which could be negative, if you're near the start of he audio), and it tracks along with the correct position. So perhaps there's some offset that can be applied whenever the app is used on an earlier iOS version?

NeilDurant
As a follow-up to this, I think it may be possible to force the correct behaviour on the iPad by setting the currentTime to zero before setting it to the desired value. I have this working for me half of the time...needs further investigation.
NeilDurant
A: 

I found a workaround that fixes the problem on the iPad, which is workable until iOS4 is released for the iPad and fixes the issue.

Keep hold of the audio buffer, and when you're about to resume playback after pausing/stopping, reload the audio buffer into the AVAudioPlayer instance, set currentTime to where you want playback to resume from, and then resume playback.

Works perfectly for me, and reloading the audio buffer seems very fast.

NeilDurant