如何在AVQueuePlayer中循环声音队列?

时间:2013-03-11 19:08:19

标签: ios objective-c avaudioplayer avqueueplayer

我知道存在关于同一问题的多个问题,但在遵循this one's建议后,我遇到了一些问题。

我已经完成了所有设置,但每次使用kMTTimeZero时我都会遇到马赫错误。

soundQueue = [AVQueuePlayer queuePlayerWithItems:soundEmotions];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[soundEmotions lastObject]];

这就是我所做的。

- (void)playerItemDidReachEnd:(NSNotification *)notification {
    // Do stuff here
    NSLog(@"End has been reached.");

    // Set it back to the beginning
    [soundQueue seekToTime:kCMTimeZero];

    //Replay
    [soundQueue play];

}
  

错误:架构armv7的未定义符号:“_ kCMTimeZero”,   引自:          - ViewController.o中的[ViewController playerItemDidReachEnd:] ld:未找到架构armv7 clang的符号:错误:链接器   命令失败,退出代码为1(使用-v查看调用)

2 个答案:

答案 0 :(得分:17)

kCMTimeZero CoreMedia.framework 中的符号,因此您必须将此框架添加到目标“Build Phases”中的“Link Binary With Libraries”部分。

答案 1 :(得分:0)

我使用这种方法观察最后一项然后seek to kCMTimeZero

 override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

        if keyPath == "currentItem" {

            print("Next Track...", currentTrackIndex)

            if currentTrackIndex > 0 {
                self.isPlaying = true
            }

            currentTrackIndex += 1
            if currentTrackIndex > playerQueue.items().count {
                currentTrackIndex = 0
                playerQueue.seek(to: kCMTimeZero)
            }
        }
    }

然后

private func observeTrackChanged(of player : AVQueuePlayer) {

        player.addObserver(self, forKeyPath: "currentItem", options: .new, context: nil)
    }