下面是这个一个相当完整的解决方案,我只是执行:
首先,定义瓦尔持有两个按钮:
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *playButton;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *pauseButton;
(在这里,我的理解是,该工具栏是一个弱的裁判,因为它是通过IB定义的,它有一个强大的指针,但是play/pause按钮是强指针,因为我们在下面创建它们。但是我的fu在这方面有点弱,因为这是一个相当新手,所以更正赞赏。 )
二,我n IB创建一个播放按钮(无暂停按钮)并将其绑定到上面的playButton变种。
第三,建立这种方法:
- (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];
}
}
四,拨打setAsPlaying无论你要播放或暂停模式。例如:
-(IBAction) playAudio:(id)sender
{
[self.audioPlayer play];
[self setAsPlaying:YES];
}
-(IBAction) pauseAudio:(id)sender
{
[self.audioPlayer pause];
[self setAsPlaying:NO];
}
你用'=='而不是'='来试试吗?......无论如何我会用一个ivar布尔标志来检查音频是否播放。 – Mat 2012-01-05 09:00:10
哦对不起!非常感谢,我会检查它,稍后再来找你。 – 2012-01-05 10:11:30
我检查过它,但它不起作用。 – 2012-01-05 10:53:01