【问题标题】:The best way to execute code AFTER a sound has finished playing声音播放完毕后执行代码的最佳方式
【发布时间】:2012-05-15 16:10:39
【问题描述】:

我希望在声音播放完毕后执行一些代码,但不知道最好的方法是什么。我看过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
}

【问题讨论】:

    标签: iphone objective-c ios audio


    【解决方案1】:

    使用 AVPlayerItem 并为事件 AVPlayerItemDidPlayToEndTimeNotification 添加观察者。

    例如

    ...

    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
    }
    

    ....

    【讨论】:

    • 感谢您的回答。我的 .h 文件中有 #import <AudioToolbox/AudioServices.h> #import <AVFoundation/AVFoundation.h>,但出现了一些错误 - itemStatusContextplayer 都抛出未声明的标识符错误
    • 你只需要AVFoundation,但你必须声明所有变量,上面的代码只是一个sn-p,你仍然需要声明一个“AVQueuePlayer *player”和一个“static const NSString” *项目状态上下文”。查看 Xcode 文档中的播放系统指南。
    【解决方案2】:

    这个方法可以帮到你:

    OSStatus AudioServicesAddSystemSoundCompletion (
       SystemSoundID                           inSystemSoundID,
       CFRunLoopRef                            inRunLoop,
       CFStringRef                             inRunLoopMode,
       AudioServicesSystemSoundCompletionProc  inCompletionRoutine,
       void                                    *inClientData
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 2010-11-09
      • 2018-11-27
      相关资源
      最近更新 更多