我想在声音播放完毕后执行一些代码,但不知道最好的方法是什么。我看过performSelectorOnMainThread....
但并不完全理解它 - 即便如此,我不确定这是否是解决问题的最佳方法。我想在声音播放完毕后更改标签的文字颜色。我很感激一些帮助。我的代码如下:
-(void) playWordSound:(UILabel *)label
{
NSString *path;
SystemSoundID soundId;
switch (label.tag)
{
case 1:
path = [[NSBundle mainBundle] pathForResource:@"humpty" ofType:@"wav"];
break;
case 2:
path = [[NSBundle mainBundle] pathForResource:@"dumpty" ofType:@"wav"];
break;
case 3:
path = [[NSBundle mainBundle] pathForResource:@"saty" ofType:@"wav"];
break;
case 4:
path = [[NSBundle mainBundle] pathForResource:@"on 2" ofType:@"wav"];
break;
case 5:
path = [[NSBundle mainBundle] pathForResource:@"a 3" ofType:@"wav"];
break;
case 6:
path = [[NSBundle mainBundle] pathForResource:@"wall" ofType:@"wav"];
break;
}
NSURL *url = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID( (CFURLRef)objc_unretainedPointer(url), &soundId);
AudioServicesPlaySystemSound(soundId);
//this is where i want to change the color of the label
}
答案 0 :(得分:2)
使用AVPlayerItem并为事件AVPlayerItemDidPlayToEndTimeNotification添加观察者。
e.g。
...
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
player = [AVQueuePlayer playerWithPlayerItem:playerItem];
[player play];
...
- (void)playerItemDidReachEnd:(NSNotification *)notification {
// Do stuff here
}
...
答案 1 :(得分:0)
此方法可以帮助您:
OSStatus AudioServicesAddSystemSoundCompletion (
SystemSoundID inSystemSoundID,
CFRunLoopRef inRunLoop,
CFStringRef inRunLoopMode,
AudioServicesSystemSoundCompletionProc inCompletionRoutine,
void *inClientData
);