简体   繁体   English

我正在尝试使用AVQueuePlayer创建一个无缝的音频循环,但是,我不知道为什么循环之间会有一个小的静音暂停?

[英]I'm trying to use AVQueuePlayer to create a seamless audio loop, however, I don't know why there is a small silent pause between loops?

I have a simple audio file in .wav format (the audio file is cut perfectly to loop). 我有一个.wav格式的简单音频文件(音频文件完全切割到循环)。 I've tried different methods to loop it. 我尝试了不同的方法来循环它。 My first attempt was simply using AVPlayer and NSNotification to detect when audioItem ended to seek time at zero and play again. 我的第一次尝试只是使用AVPlayer和NSNotification来检测audioItem何时结束以寻找零时间并再次播放。 However, there was clearly a gap. 但是,显然存在差距。

I've been looking at different solutions online, and found people using AVQueuePlayer to do a switching: Looping AVPlayer seamlessly 我一直在网上寻找不同的解决方案,并发现人们使用AVQueuePlayer进行切换: 无缝循环AVPlayer

However, when implemented, this still produces a gap. 但是,实施时,这仍然会产生差距。

Here's my current notification code: 这是我当前的通知代码:

weak var weakSelf = self
NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: nil, queue: nil, usingBlock: {(note: NSNotification) -> Void in

        if weakSelf?.currentQueuePlayer.currentItem == weakSelf?.currentAudioItemOne {

            weakSelf?.currentQueuePlayer.insertItem((weakSelf?.currentAudioItemTwo)!, afterItem: nil)
            weakSelf?.currentAudioItemTwo.seekToTime(kCMTimeZero)
        } else {
            weakSelf?.currentQueuePlayer.insertItem((weakSelf?.currentAudioItemOne)!, afterItem: nil)
            weakSelf?.currentAudioItemOne.seekToTime(kCMTimeZero)
        }

    })

Here's my code to set up the current QueuePlayer. 这是我设置当前QueuePlayer的代码。

let audioPlayerItem = AVPlayerItem(URL: url)
currentAudioItemOne = audioPlayerItem


currentAudioItemTwo = audioPlayerItem

currentQueuePlayer = AVQueuePlayer()
currentQueuePlayer.insertItem(currentAudioItemOne, afterItem: nil)

currentQueuePlayer.play()

I've been working at this problem for several days now. 我已经在这个问题上工作了好几天了。 Any leads or new things to try would be appreciated. 任何线索或新的尝试将不胜感激。 The only thing I haven't tried so far is lower quality audio files. 到目前为止我唯一没有尝试过的是低质量的音频文件。 These .wav files are all over 1mb, and had be suspecting that the file size could be affecting the seamless looping. 这些.wav文件都超过1mb,并且怀疑文件大小可能会影响无缝循环。

EDIT: 编辑:

Using AVPlayerLooper to create the 'Treadmill' effect: 使用AVPlayerLooper创建“跑步机”效果:

        let url = URL(fileURLWithPath: path)
        let audioPlayerItem = AVPlayerItem(url: url)
        currentAudioItemOne = audioPlayerItem

        currentQueuePlayer = AVQueuePlayer()
        currentAudioPlayerLayer = AVPlayerLayer(player: currentQueuePlayer)
        currentAudioLooper = AVPlayerLooper(player: currentQueuePlayer, templateItem: currentAudioItemOne)

        currentQueuePlayer.play() 

EDIT 2: 编辑2:

afinfo on one of my wav files: 我的一个wav文件的afinfo:

Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, 'lpcm' (0x0000000C) 16-bit little-endian signed integer
                no channel layout.
estimated duration: 11.302336 sec
audio bytes: 1993732
audio packets: 498433
bit rate: 1411200 bits per second
packet size upper bound: 4
maximum packet size: 4
audio data file offset: 44
not optimized
source bit depth: I16
----

You are inserting the item too late in your current solution. 您在当前的解决方案中插入项目太晚了。 You need to queue up more than one initial item, so there's always a primed AVPlayerItem ready to go. 您需要排队多个初始项目,因此总是有一个准备就绪的准备好的AVPlayerItem

This is called the AVPlayerQueue "treadmill pattern" as better described in this WWDC 2016 session . 这被称为AVPlayerQueue “跑步机模式”, 在本WWDC 2016会议中有更好的描述 If you're targeting iOS 10, you can use new AVPlayerLooper class which does it for you (also described in the same link). 如果你的目标是iOS 10,你可以使用新的AVPlayerLooper类为你做这个(也在同一个链接中描述)。 Apple has also provided a sample project which provides an example of both strategies. Apple还提供了一个示例项目 ,它提供了两种策略的示例。

Lower level solutions include queuing up the audio buffers to an AVAudioEngine instance or using an AudioQueue or mashing the buffers together yourself with an AudioUnit . 较低级别的解决方案包括排队的音频缓冲到AVAudioEngine实例或使用AudioQueue或自己用捣碎的缓冲区一起AudioUnit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM