views:

356

answers:

3
NSString *path = [[NSBundle mainBundle] pathForResource:[arraySub objectAtIndex:indexPathHere.row] ofType:@"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.player = myPlayer;
[player prepareToPlay];
[player setDelegate:self];
[self.player play];

NSTimeInterval lenghtMusic = player.duration;

if (player.currentTime == lenghtMusic) {
 NSLog(@"It worked");
 [tableThing deselectRowAtIndexPath:indexPathHere animated:YES];
 [myPlayer autorelease];
 [file autorelease];
}

Can you see anything wrong?

Apparently, the "if"-statement never gets called...

+3  A: 

You are comparing double's with ==, which is almost always false due to round-off errors.

Compare with a range e.g.

if (fabs(player.currentTime - lenghtMusic) < 0.0001) {
  ...
}
KennyTM
I'll try!Thanks :)
Emil
Didn't work...Got any other ideas?
Emil
Try this: `if(player.currentTime >= lengthMusic) {` (or, if that doesn't work, `if(player.currentTime >= lengthMusic - 0.001) {`)
Chris Burt-Brown
NSTimeInterval is precise enough that it can return microseconds so that it can actually be to fast to compare to timestamp operations in code. By the time you've executed one line, the time interval as changed before you can execute the next. A straight compare will always fail.
TechZen
This wont help you with what you are trying to do
willcodejavaforfood
A: 

I'm a noob iPhone dev, but are you sure that player.currentTime is a NSTimeInterval? That looks like the most obvious candidate.

Also, I'd log out what your player.currentTime is and what lenghtMusic is before your if and that'll probably let you know what's going on.

Terry Donaghe
currentTime is a NSTimeInterval
willcodejavaforfood
+16  A: 

If I read your code correctly you are trying to find out when the player has finished playing and then you want to deselect the current rode and release the player. Your problem is that you start the player and then immediately checks where it is currently playing. Unless you have an almost 0 length file the currentTime and length will NEVER be equal.

If that is what you are trying to do you should use the AVAudioPlayerDelegate Protocol and especially the:

– audioPlayerDidFinishPlaying:successfully:

method that is called when the player is finished.

So you need to change your class that controls the player by editing the .h file to use the AVAudioPlayerDelegate Protocol. Obviously keep extending whatever you were before.

@interface YourClass <AVAudioPlayerDelegate>

@end

In your .m file: Then when you have created and assigned your player instance:

[player setDelegate:self];

In the .m file also add the method

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    // Your success code goes here
}
willcodejavaforfood
Can you call that inside of another function?
Emil
This gets my vote for the correct answer. This is a delegate method that should always be implemented. It is virtually impossible to compare NSTimeIntervals without using one of the NSData methods. It simply has to high of precision.
TechZen
`[player isPlaying]`That tells me that its running. How do you find out if it finishes out from that?
Emil
Problem with isPlaying is that you would have to 'poll' constantly but audioPlayerDidFinishPlaying will be called once the player is finished without you having to do anything except setting the delegate object
willcodejavaforfood
Go to the page for AVAudioPlayerDelegate Protocol and have a look at the examples there to find out how to use the delegate
willcodejavaforfood
@Emil - There updated it for you, sorry I had to finish making my Shepherds Pie first :)
willcodejavaforfood