如何强制一个方法在另一个方法之前执行?
我有2个方法。 一种方法开始播放音频文件 (.mp3),另一种方法更新 UIToolBar 以显示按钮(播放或暂停)。这两个方法按以下顺序调用:
//Adds some UIBarButtonItems to a UIToolBar
[self togglePlayer];
//Uses AVAudioPlayer
[audioPlayer play];
TogglePlayer 执行以下操作:
-(void)togglePlayer
{
NSLog(@"Toggling Player");
NSArray *barButtonItems;
UIBarButtonItem *barButtonSpaceL;
UIBarButtonItem *barButtonSpaceR;
UIBarButtonItem *barButtonItemPlayer;
UIBarButtonItem *barButtonItemCancel;
UIBarButtonItem *barButtonItemLyrics;
UIBarButtonItem *barButtonItemTweet;
if([myToolbar.items count] > 0){
NSEnumerator *enumerator = [myToolbar.items objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
[object release];
object = nil;
}
}
if(!downloadInProgress){
barButtonSpaceL = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
barButtonSpaceR = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
if(thePlayerState == PLAYER_PLAYING){
barButtonItemPlayer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"pauseIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pauseButtonPressed:)];
barButtonItemLyrics = [[UIBarButtonItem alloc] initWithTitle:@"Lyrics" style:UIBarButtonItemStyleBordered target:self action:@selector(switchPageLyrics:)];
barButtonItemTweet = [[UIBarButtonItem alloc] initWithTitle:@"Tweet This" style:UIBarButtonItemStyleBordered target:self action:@selector(tweetSong:)];
if(canTweet){
barButtonItems = [NSArray arrayWithObjects:barButtonItemTweet, barButtonSpaceR, barButtonItemPlayer, barButtonSpaceL, barButtonItemLyrics, nil];
}else{
barButtonItems = [NSArray arrayWithObjects:barButtonItemPlayer, barButtonSpaceR, barButtonItemLyrics, nil];
}
}else if(thePlayerState == PLAYER_PAUSED){
barButtonItemPlayer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"playIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(playButtonPressed:)];
barButtonItemLyrics = [[UIBarButtonItem alloc] initWithTitle:@"Lyrics" style:UIBarButtonItemStyleBordered target:self action:@selector(switchPageLyrics:)];
barButtonItemTweet = [[UIBarButtonItem alloc] initWithTitle:@"Tweet This" style:UIBarButtonItemStyleBordered target:self action:@selector(tweetSong:)];
if(canTweet){
barButtonItems = [NSArray arrayWithObjects:barButtonItemTweet, barButtonSpaceR, barButtonItemPlayer, barButtonSpaceL, barButtonItemLyrics, nil];
}else{
barButtonItems = [NSArray arrayWithObjects:barButtonItemPlayer, barButtonSpaceR, barButtonItemLyrics, nil];
}
}else{
//PLAYER OFF
barButtonItems = [NSArray arrayWithObjects:barButtonSpaceL, nil];
}
[myToolbar setItems:barButtonItems];
}else{
barButtonSpaceL = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
barButtonSpaceR = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
barButtonItemCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"VC1_DownloadCancel", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(downloadCancelled:)];
if(thePlayerState == PLAYER_PLAYING){
barButtonItemPlayer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"pauseIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pauseButtonPressed:)];
barButtonItems = [NSArray arrayWithObjects:barButtonItemPlayer, barButtonSpaceR, barButtonItemCancel, nil];
}else if(thePlayerState == PLAYER_PAUSED){
barButtonItemPlayer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"playIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(playButtonPressed:)];
barButtonItems = [NSArray arrayWithObjects:barButtonItemPlayer, barButtonSpaceR, barButtonItemCancel, nil];
}else{
//PLAYER OFF
barButtonItems = [NSArray arrayWithObjects:barButtonSpaceL, barButtonItemCancel, barButtonSpaceR, nil];
}
[myToolbar setItems:barButtonItems];
}
}
我按上述顺序调用方法,以便在歌曲开始播放时显示(暂停)按钮。但是,问题是歌曲首先开始播放,并且 UIToolBar 在相当长一段时间内(从 2 到 5 秒)保持不变,直到添加并显示按钮。
我想要的是在歌曲开始播放的同时显示按钮(即无延迟)。有什么办法可以做到这一点吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了更新 GUI,您需要给它一个实际更新 GUI 的机会。由于音频播放发生在不同的线程中,因此它将立即开始播放。但是,由于您可能处于 gui 的事件中(例如触摸事件)并且尚未从该函数返回,因此负责更新 gui 的代码尚未运行。
请参阅 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ApplicationEnvironment/ApplicationEnvironment.html#//apple_ref/doc/uid/TP40007072-CH7-SW2有关 Cocoa 框架的信息
使用此函数来调用 play 应该可以按照您想要的方式工作。这将在处理当前事件后将事件置于调用播放位置。
你的例子:
in order to update the gui you will need to give it a chance to actually update the gui. Since the audio playing happens in a different thread it will begin playing right away. However, since you are probably in an event from gui (say like a touch event) and you haven't returned from that function, the code that responsible for updating the gui hasn't ran yet.
See http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ApplicationEnvironment/ApplicationEnvironment.html#//apple_ref/doc/uid/TP40007072-CH7-SW2 for information on the Cocoa Framework
Using this function to call play should work the way you want. this will place the event to call play after your current event is processed.
Your example:
来准备系统播放音频文件。
另一种方法可能是使用初始化代码中的某个位置(例如
viewDidLoad
) 这样,一旦您想要播放音频,所有内容都会被设置,并且play
方法不会导致明显的延迟。 (不在您的用户界面代码中,也不在实际播放中)Another approach could be to prepare the system to play your audio file, using
somewhere in your initialisation code, e.g. in
viewDidLoad
. This way everything will be set once you want the audio to play, and theplay
method will not cause a significant delay. (not in your user interface code, and not in actual playback)