【问题标题】:How to reduce delay in AVplayer for looping audio play - ios swift如何减少 AVplayer 中循环音频播放的延迟 - ios swift
【发布时间】:2018-10-30 05:02:04
【问题描述】:

我在项目文件夹中有一个 mp3 文件,它会反复播放,但播放器在播放完 mp3 时会出现延迟。此延迟约为 1 秒。如何减少延迟并不断重播?我使用了循环数,但没有帮助。代码如下:

DispatchQueue.main.async
        {
            guard let url = Bundle.main.url(forResource: "finalbreathin", withExtension: "mp3") else { return }

            do {
                try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                try AVAudioSession.sharedInstance().setActive(true)



                /* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
                self.player = try AVAudioPlayer(contentsOf: url)

                //iOS 10 and earlier require the following line:
                self.player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)

                guard let player = self.player else { return }
                player.numberOfLoops = -1
                self.imageanimate.startAnimating()
                player.play()

            } catch let error {
                print(error.localizedDescription)
            }

    }

【问题讨论】:

  • 我已经使用播放器的循环数音频大约是 9 秒长
  • 请添加您的代码

标签: iphone swift4 avplayer avaudioplayer audio-player


【解决方案1】:

我认为重播延迟是每个循环声明的主要原因

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)

所以将这两行移出循环或 AppDelegate 在 application:didFinishLaunchingWithOptions: 方法中,将应用的音频会话类别设置为 AVAudioSessionCategoryPlayback。 查看更多详情 -> https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/GettingStarted/GettingStarted.html

【讨论】:

  • 我不明白是因为 AVplayer 委托还是 AVaudiosessionCategoryplayback?
  • 请看这个 url 我想你明白了如何实现
  • 我做到了,但玩家每次提前 1 秒完成时无法将游戏时间与计时器匹配
猜你喜欢
  • 2019-03-09
  • 2016-04-13
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多