如何减少AVplayer循环播放音频的延迟-iOS Swift

时间:2018-10-30 05:02:04

标签: iphone swift4 avplayer avaudioplayer audio-player

我在项目文件夹中有一个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)
            }

    }

1 个答案:

答案 0 :(得分:0)

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

app.get('/', function (req, res) {
    console.log(1);
    res.sendFile('Trading Inquiry/index.html');
});

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