Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

ios - AVAudioPlayer can't open an audio while screen off. Error: Error Domain=NSOSStatusErrorDomain Code=-54

I try to play the next audio when the current audio is finished I catch audioPlayerDidFinishPlaying. It works ok when the iPhone screen on. If I turn off the screen I get the error Error Domain=NSOSStatusErrorDomain Code=-54 when I try to open the next audio by AVAudioPlayer.

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    if flag {
        do {
            player = try AVAudioPlayer(contentsOf: url)
        } catch {
            print(error)
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have to use AVAudioPlayer as it supports playback rate but it doesn't support creating the instance of AVAudioPlayer in sleep device mode. My solution is to create arrays of AVAudioPlayer before the device will be slept. When the current audio is finished you should to get the next instance from stored array.

You can check it here

let players = [AVAudioPlayer]()
let index = 0

do {
    player = [
        try AVAudioPlayer(contentsOf: url1),
        try AVAudioPlayer(contentsOf: url2),
        try AVAudioPlayer(contentsOf: url3)
    ]
} catch {
    print(error)
}

players[index].play()

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    if flag {
        index += 1
        players[index].play()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...