views:

550

answers:

2

Hello,

I have several UIButtons that control the iPhone's music player.

I would like to have the music player seek backwards if the previous button is touched and held, but skip to the previous item if the button is single-tapped.

Currently, I am able to skip to the previous item with a tap, but touching and holding will seek backwards AND skip to the previous item once the button is touched up.

Here is the code I am currently using:

- (void)previousButtonTouchedDown {
 [self performSelector:@selector(seekBackwards) withObject:nil afterDelay:1];
}

- (void)previousButtonTouchedUpInside { 
 MPMusicPlaybackState playbackState = self.iPodPlayer.playbackState;

 if (playbackState == MPMusicPlaybackStateSeekingBackward) {
  [self.iPodPlayer endSeeking];
 } else {
  [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(seekBackwards) object:nil];
  [self.iPodPlayer skipToPreviousItem];
 }
}

- (void)seekBackwards {
 [self.iPodPlayer beginSeekingBackward];
}

How can I seek forward, but not skip to the previous item, if the button is touched, held, then touched up?

Thanks.

A: 

I haven't tested this, and I don't know if it's the best solution.

  • You could attach an action to the button's UIControlEventTouchedDown, and then trigger a timer for about 1 sec.
  • When the timer fires, it could call a method that begins seeking.
  • Then attach another method to the button's UIControlEventTouchedUp.
    • Check if the player is seeking
      • if it is seeking, stop seeking
      • else, skip forward.

How does that sound?

Jasarien
A: 

Huh.

After running my app on a device with OS 3.1.2 (using the same code from the original post), it appears that the issue is fixed. The same code that seeks AND skips on a 3.0 device ONLY seeks on a 3.1.2 device.

So it seems that on 3.0, the player's playback state on UIControlEventTouchedUp is equal to playing, not skipping. On 3.1.2, it's the opposite.

Does anyone know of a solution that doesn't involve checking the player's playback state?

dev