Swift 3 iOS-与AVAudioPlayer同步暂停 [英] Swift 3 iOS - Synchronised Pause with AVAudioPlayer

查看:76
本文介绍了Swift 3 iOS-与AVAudioPlayer同步暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AVAudioPlayer 在播放器中播放一组同步的MP3文件样式界面(播放/暂停/停止).我知道我可以使用 play(atTime:)来同步播放它们一个>,但是我的问题是,如何同步暂停它们?目前,要暂停它们,我正在使用:

I am using AVAudioPlayer to play a set of synchronised MP3 files in a player style interface (Play/Pause/Stop). I am aware that I can synchronise playing them by using play(atTime:) but my question is, how do I synchronise pausing them? At the moment, to pause them, I am using:

for (_, audioTrackPlayer) in audioTrackPlayers.enumerated() {
    audioTrackPlayer.pause()
}

但是,当然,它们都一个接一个地暂停.没有等效的pause(atTime :)函数.那么如何为暂停实现精确的同步呢?

But of course, they all pause one after the other. There is no equivalent pause(atTime:) function. So how can precise synchronisation be achieved for the pausing?

编辑-额外信息

在关于matt的可查询性之后,我得到了一些数据.暂停5条曲目大约需要半秒钟.为了说明这一点,我在暂停循环之后添加了一个单独的循环,以报告每个轨道上的currentTime,并且在进行暂停之前还添加了两个循环-供参考,并显示它们与开始时是同步的.

Following matt's query about how noticeable it is, I got some data on that. It takes almost half a second to pause 5 tracks. To show that, I added a separate loop, after the pause loop, to report the currentTime on each track, and also added a couple of loops before doing the pausing - for reference and to show they were in sync to start with.

Loop benchmark, 0: 3.2118820861678
Loop benchmark, 0: 3.21580498866213
Loop benchmark, 0: 3.21888888888889
Loop benchmark, 0: 3.22126984126984
Loop benchmark, 0: 3.22328798185941

Before pause, 0: 3.22560090702948
Before pause, 1: 3.22750566893424
Before pause, 2: 3.22975056689342
Before pause, 3: 3.23185941043084
Before pause, 4: 3.23439909297052

After pause, 0: 3.35040816326531
After pause, 1: 3.46598639455782
After pause, 2: 3.56979591836735
After pause, 3: 3.67455782312925
After pause, 4: 3.77895691609977

因此,磁道在暂停之前是同步的,时间差仅是由于循环的每次迭代需要3或4毫秒造成的.暂停后,每首曲目之间的间隔约为十分之一秒.

So the tracks were in sync before the pause, with the time difference being only due to the 3 or 4 milliseconds it takes for each iteration of the loop. After the pause there was around one tenth of a second between each track.

参考代码:

for _ in 0..<5 {
    print ("Loop benchmark, 0: \(audioTrackPlayers[0].currentTime)")
}
for (index, audioTrackPlayer) in audioTrackPlayers.enumerated() {
    print ("Before pause, \(index): \(audioTrackPlayer.currentTime)")
}
for audioTrackPlayer in audioTrackPlayers {
    audioTrackPlayer.pause()
}
for (index, audioTrackPlayer) in audioTrackPlayers.enumerated() {
    print ("After pause, \(index): \(audioTrackPlayer.currentTime)")
}

推荐答案

我已通过在停止或暂停音轨之前将所有音轨的音量设置为0来部分解决此问题.这使用户在音频方面具有即时响应"的感觉,因此可以最大程度地解决此问题.不需要花半秒钟来暂停曲目还是很不错的,所以我不接受这个答案,但是它确实以一种可用的方式解决了这个问题.

I have partially fixed this issue by setting the volume of all the audio tracks to 0 before stopping or pausing the tracks. This gives the user the "instant response" feel in terms of the audio, so mostly fixes this issue. It would still be nice to not need to take half a second to pause the tracks, so I am not accepting this answer, but it does solve the problem in a usable way.

示例代码(我用来证明这一点的简化版本),包括必要的代码,以重置currentTime值以使其在延迟的暂停后不满意. truncatingRemainder是必需的,因为它们都是循环音频.

Example code (a simplified version of what I'm using to demonstrate the point), including the necessary code to reset the currentTime values to match after being upset by the delayed pause. The truncatingRemainder is necessary because these are all looping audios.

// Set volumes of all audio tracks to 0, so delay on pause is not noticed
for audioTrackPlayer in audioTrackPlayers {
    audioTrackPlayer.volume = 0
}
// Pause audio track players
for audioTrackPlayer in audioTrackPlayers {
    audioTrackPlayer.pause()
}
// Update the currentTime values so they all match
for index in 1..<audioTrackPlayers.count {
    audioTrackPlayers[index].currentTime = audioTrackPlayers[0].currentTime.truncatingRemainder(dividingBy: audioTrackPlayers[index].duration)
}
// Reset the volumes now the tracks are paused
for audioTrackPlayer in audioTrackPlayers {
    audioTrackPlayer.volume = 1
}

这篇关于Swift 3 iOS-与AVAudioPlayer同步暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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