我正在尝试使用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?

查看:74
本文介绍了我正在尝试使用AVQueuePlayer创建一个无缝的音频循环,但是,我不知道为什么在循环之间会有一个小的静默暂停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.wav格式的简单音频文件(该音频文件被完美剪切以循环播放).我尝试了不同的方法来循环它.我的第一次尝试只是使用AVPlayer和NSNotification来检测audioItem何时结束以寻求零时间并再次播放.但是,显然存在差距.

I have a simple audio file in .wav format (the audio file is cut perfectly to loop). 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. However, there was clearly a gap.

我一直在在线寻找不同的解决方案,发现人们使用AVQueuePlayer进行切换: 无缝循环AVPlayer

I've been looking at different solutions online, and found people using AVQueuePlayer to do a switching: Looping AVPlayer seamlessly

但是,在实施时仍然会产生差距.

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

    })

这是我的代码,用于设置当前的QueuePlayer.

Here's my code to set up the current QueuePlayer.

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


currentAudioItemTwo = audioPlayerItem

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

currentQueuePlayer.play()

我已经为这个问题工作了几天.任何线索或尝试的新事物将不胜感激.到目前为止,我唯一没有尝试过的就是质量较低的音频文件.这些.wav文件的大小都超过1mb,并且怀疑文件大小可能会影响无缝循环.

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.

使用AVPlayerLooper创建跑步机"效果:

Using AVPlayerLooper to create the 'Treadmill' effect:

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

我的一个wav文件上的afinfo:

afinfo on one of my wav files:

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

推荐答案

您在当前解决方案中插入项目太晚了.您需要排队多个初始项目,因此总是有准备好的AVPlayerItem准备就绪.

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.

这被称为AVPlayerQueue跑步机模式",称为在WWDC 2016会议中进行了更好的描述.如果您的目标是iOS 10,则可以使用新的AVPlayerLooper类为您执行此操作(在同一链接中也有介绍).苹果还提供了示例项目,它提供了一个两种策略的例子.

This is called the AVPlayerQueue "treadmill pattern" as better described in this WWDC 2016 session. If you're targeting iOS 10, you can use new AVPlayerLooper class which does it for you (also described in the same link). Apple has also provided a sample project which provides an example of both strategies.

较低级别的解决方案包括将音频缓冲区排队到AVAudioEngine实例或使用AudioQueue或将自己与AudioUnit混搭在一起.

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.

这篇关于我正在尝试使用AVQueuePlayer创建一个无缝的音频循环,但是,我不知道为什么在循环之间会有一个小的静默暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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