iOS8 AVAudioSession setActive error

后端 未结 5 1118
独厮守ぢ
独厮守ぢ 2020-12-28 12:00

I\'m testing my app in XCode 6 and find an issue with AVAudioSession in iOS8.

When I call

[[AVAudioSession sharedInstance] setActive:NO error

5条回答
  •  孤独总比滥情好
    2020-12-28 13:01

    Swift 5, IOS 12

    I use AVPlayer, it does not have a prepareToPlay method. Therefore, Alessio Campanelli's answer does not solve my problem. But I noticed the following

    player.pause()
    
    print("currentTime:",  player.currentTime().seconds) //currentTime: 4.258164744
    print("status:", player.status.rawValue) //status: 1 (readyToPlay)
    print("timeControlStatus:", player.timeControlStatus.rawValue) //timeControlStatus: 0 (paused)
    print("rate:", player.rate) //rate: 0.0
    
    sleep(1)
    
    print("currentTime:",  player.currentTime().seconds) //currentTime: 4.261325767
    print("status:", player.status.rawValue) //status: 1 (readyToPlay)
    print("timeControlStatus:", player.timeControlStatus.rawValue) //timeControlStatus: 0 (paused)
    print("rate:", player.rate) //rate: 0.0
    

    After you call the pause method, the player plays the audio file for a while. Although other properties say that it is in a pause. So the only solution I found is

    player.pause()
    DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
        do {
            try AVAudioSession.sharedInstance().setActive(false)
        } catch let error {
            print(error)
        }
    }
    

    In fact, even 0.1 seconds is sufficient. This is a bad way, but I could not find anything better. If anyone knows how to check when the player stops playing the audio file, let me know.

提交回复
热议问题