Oto bardzo kompletne rozwiązanie do tego, że po prostu realizowane:
Najpierw zdefiniuj vars trzymać oba przyciski:
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *playButton;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *pauseButton;
(moim rozumieniu jest to, że pasek narzędzi jest słabym ref ponieważ jest określany przez IB i ma silny wskaźnik do niego, ale przyciski play/pause są mocnymi wskazówkami, ponieważ tworzymy je poniżej, ale moje fu jest trochę słabe na tym froncie jako dość początkujący, więc poprawki są doceniane.)
Po drugie, mogę n IB utwórz tylko przycisk odtwarzania (bez przycisku pauzy) i powiązaj go z powyższym var playButton.
trzecie, skonfigurować tę metodę:
- (void) setAsPlaying:(BOOL)isPlaying
{
self.rootViewController.playing = isPlaying;
// we need to change which of play/pause buttons are showing, if the one to
// reverse current action isn't showing
if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
{
UIBarButtonItem *buttonToRemove = nil;
UIBarButtonItem *buttonToAdd = nil;
if (isPlaying)
{
buttonToRemove = self.playButton;
self.playButton = nil;
self.pauseButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause
target:self
action:@selector(pauseAudio:)];
buttonToAdd = self.pauseButton;
}
else
{
buttonToRemove = self.pauseButton;
self.pauseButton = nil;
self.playButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:@selector(playAudio:)];
buttonToAdd = self.playButton;
}
// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];
// Remove a button from the toolbar and add the other one
if (buttonToRemove)
[toolbarButtons removeObject:buttonToRemove];
if (![toolbarButtons containsObject:buttonToAdd])
[toolbarButtons insertObject:buttonToAdd atIndex:2]; // vary this index to put in diff spots in toolbar
[self.toolbar setItems:toolbarButtons];
}
}
czwarte, zadzwoń setAsPlaying gdziekolwiek rozegrają lub w trybie wstrzymania. Na przykład:
-(IBAction) playAudio:(id)sender
{
[self.audioPlayer play];
[self setAsPlaying:YES];
}
-(IBAction) pauseAudio:(id)sender
{
[self.audioPlayer pause];
[self setAsPlaying:NO];
}
Czy próbowałeś z '==' zamiast '='? ... w każdym razie użyłbym flagi ivar bool, aby sprawdzić, czy dźwięk jest odtwarzany, czy nie. – Mat
oh przepraszam! wielkie dzięki, sprawdzę to i przyjdę do ciebie później. –
Sprawdziłem, ale to nie działa. –