【问题标题】:AVAudioPlayer does not think it is playing when it isAVAudioPlayer 不认为它在播放时
【发布时间】:2020-05-09 01:43:32
【问题描述】:

稍后我调用superPlay 函数开始音频播放我再次调用superPlay 函数停止播放。但这不起作用,因为即使知道声音显然在无限循环中播放,player.isPlaying 也是错误的。我不知道为什么请帮忙!

   func superPlay(timeInterval: TimeInterval, soundName: String) {
        do {
            alarmSound = soundName
            //set up audio session
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.defaultToSpeaker, .duckOthers])
            try AVAudioSession.sharedInstance().setActive(true)

            let url = Bundle.main.url(forResource: alarmSound, withExtension: "mp3")

            player = try! AVAudioPlayer(contentsOf: url!)
            player.numberOfLoops = -1
            //Start AVAudioPlayer
            print(timeInterval)
            print(time)
            let playbackDelay = timeInterval  // must be ≥ 0

            if player.isPlaying {
                player.stop()
            } else {
                player.play(atTime: player.deviceCurrentTime + playbackDelay) //time is a TimeInterval after which the audio will start
            }
        }
        catch {
            print(error)
        }
    }

我现在又花了几天时间调试它。发生的事情是将原始播放分配给特定的 AudioPlayer ID,例如打印是:"Optional(<AVAudioPlayer: 0x600002802280>)" 当我再次调用该函数以停止播放时,为 AVAudioPlayer 分配了不同的 ID,因此它不会发现旧播放器仍在播放,而是在旧声音之上播放新声音。

我不确定如何存储 AVAudioPlayer ID,然后调用该函数以检查商店播放器是否为“.isPlaying”??

【问题讨论】:

  • 如果发生延迟,说明播放器尚未播放。
  • 我最终发现 audioPlayer 对象被保存在调用启动 audioPlayer 播放的函数的文件中。所以我在该文件中添加了停止,现在它可以工作了。

标签: swift avaudioplayer avaudiosession


【解决方案1】:

发生这种情况是因为您在停止当前音频之前对其进行了初始化。试试这个方法:-

    func superPlay(timeInterval: TimeInterval, soundName: String) {

        //        If audio is playing already then stop it.
        if let audioPlayer = player {
            if player.isplaying {
                player.stop()
            }
        }
        //        Initilize audio player object with new sound
        do {
            alarmSound = soundName
            //set up audio session
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.defaultToSpeaker, .duckOthers])
            try AVAudioSession.sharedInstance().setActive(true)

            let url = Bundle.main.url(forResource: alarmSound, withExtension: "mp3")

            player = try! AVAudioPlayer(contentsOf: url!)
            player.numberOfLoops = -1
            //Start AVAudioPlayer
            print(timeInterval)
            print(time)
            let playbackDelay = timeInterval  // must be ≥ 0

            player.play(atTime: player.deviceCurrentTime + playbackDelay) //time is a TimeInterval after which the audio will start
        }
        catch {
            print(error)
        }
    }

【讨论】:

  • 非常感谢您的帮助。这并没有解决问题,声音仍在播放,实际上第二个声音开始在第一个声音之上播放。我调试了一下它没有输入“if let audioPlayer.isplaying”条件代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
相关资源
最近更新 更多