后一个声音播放完毕,最好的办法执行code [英] The best way to execute code AFTER a sound has finished playing

查看:95
本文介绍了后一个声音播放完毕,最好的办法执行code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个声音已播放完毕后执行一些code,但不知道该怎么做的最好的办法是。我看了 performSelectorOnMainThread .... ,但不完全理解 - 即使是这样,我不知道,如果这对解决这个问题的最好办法。我想我的声音播放完毕后,更改标签的文本颜色。我倒是AP preciate一些帮助。我的code是如下:

   - (无效)playWordSound:(*的UILabel)标签
{
    * NSString的路径;
    SystemSoundID soundId;
    开关(label.tag)
    {
        情况1:
            路径= [[一个NSBundle mainBundle] pathForResource:@矮胖ofType:@WAV];
            打破;
        案例2:
            路径= [[一个NSBundle mainBundle] pathForResource:@矮胖子ofType:@WAV];
            打破;
        案例3:
            路径= [[一个NSBundle mainBundle] pathForResource:@SATYofType:@WAV];
            打破;
        情况4:
            路径= [[一个NSBundle mainBundle] pathForResource:@2ofType:@WAV];
            打破;
        情况5:
            路径= [[一个NSBundle mainBundle] pathForResource:@3​​ofType:@WAV];
            打破;
        情况6:
            路径= [[一个NSBundle mainBundle] pathForResource:@墙ofType:@WAV];
            打破;
    }
        NSURL * URL = [NSURL fileURLWithPath:路径];
        AudioServicesCreateSystemSoundID((CFURLRef)objc_unretainedPointer(URL),和放大器; soundId);
    AudioServicesPlaySystemSound(soundId);
    //这是我想改变标签的颜色
}


解决方案

使用一个AVPlayerItem并添加一个观察员事件AVPlayerItemDidPlayToEndTimeNotification。

例如

...

  AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:资产];
[playerItem的addObserver:自forKeyPath:@状态选项:0语境:放大器; ItemStatusContext]。
[NSNotificationCenter defaultCenter]的addObserver:自我
                                             选择:@选择(playerItemDidReachEnd :)
                                             名称:AVPlayerItemDidPlayToEndTimeNotification
                                             对象:playerItem];
玩家= [AVQueuePlayer playerWithPlayerItem:playerItem];
[播放器播放];

...

   - (无效)playerItemDidReachEnd:(NSNotification *)通知{
    //这里做的东西
}

...

I'm looking to execute some code after a sound has finished playing but don't know what the best way to do it is. I've looked at performSelectorOnMainThread.... but don't fully understand it - even then, i'm not sure if that's the best way to approach the problem. I want to change the text color of a label after my sound has finished playing. I'd appreciate some help. My code is below:

-(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
}

解决方案

Use an AVPlayerItem and add an observer for the event 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
}

....

这篇关于后一个声音播放完毕,最好的办法执行code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆