【问题标题】:AVAudioEngine and AVAudioPlayerNode: Play/Pause not updating in iOS command center properlyAVAudioEngine 和 AVAudioPlayerNode:播放/暂停未在 iOS 命令中心正确更新
【发布时间】:2021-01-06 01:53:08
【问题描述】:

我已阅读Play/Pause and Elapsed Time not updating in iOS command center properly

我试过MPNowPlayingInfoPropertyElapsedPlaybackTimeMPNowPlayingInfoPropertyDefaultPlaybackRateMPNowPlayingInfoPropertyPlaybackRate

我已经更新了apple demo

苹果演示可以在iOS命令中心显示播放/暂停更新,AVPlayer

这里使用AVAudioEngineAVAudioPlayerNodeAVAudioPlayerNode播放scheduleBuffer的音频。

这里是相关的iOS命令中心代码:

func show(mediaInfo time: TimeInterval){
        let artistName = "test"
        
        guard let duration = length else {
            return
        }
        var isPlaying: Double = 0
        
        var pInfo: [String: Any] = [MPMediaItemPropertyArtist : artistName,
                                    MPMediaItemPropertyTitle : artistName ]
        if let img = UIImage(systemName: "music.note.list"){
            let artwork = MPMediaItemArtwork(boundsSize: img.size, requestHandler: {  (_) -> UIImage in
                return img
            })
            pInfo[MPMediaItemPropertyArtwork] = artwork
            
        }
        
        if streamer.state == .playing{
            isPlaying = 1
            pInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = time
            pInfo[MPMediaItemPropertyPlaybackDuration] = duration
        }
        pInfo[MPMediaItemPropertyPersistentID] = artistName
        pInfo[MPNowPlayingInfoPropertyPlaybackRate] = NSNumber(floatLiteral: isPlaying)
        pInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = NSNumber(floatLiteral: isPlaying)
        pInfo[MPNowPlayingInfoPropertyMediaType] = NSNumber(value: MPNowPlayingInfoMediaType.audio.rawValue)
        MPNowPlayingInfoCenter.default().nowPlayingInfo = pInfo
    }

这里是the example code


对应的音频会话:

func setupAudioSession() {
        let session = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(.playback, mode: .default, policy: .default, options: [.allowBluetoothA2DP,.defaultToSpeaker])
            try session.setActive(true)
        } catch {
            os_log("Failed to activate audio session: %@", log: ViewController.logger, type: .default, #function, #line, error.localizedDescription)
        }
    }

也试过

let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
            audioSession.requestRecordPermission({ (isGranted: Bool) in
                
            })
        } catch  {}

【问题讨论】:

  • 要在命令中心显示您的元数据,您需要有一个活动的不可混合音频会话类别,并且当前也正在制作音频。你能展示你的音频会话配置吗?
  • 已更新。方便的话可以试试github代码github.com/BoxDengJZ/AudioJz/tree/master/UnderTheHood

标签: ios avaudioengine remote-control


【解决方案1】:

您可以尝试停止 AVAudioEngine。 像这样:

engine.stop()

【讨论】:

    【解决方案2】:

    最好使用engine.pause()

       public func pause(){
    
            // Check if the player node is playing
            guard playerNode.isPlaying else {
                return
            }
            
            // Pause the player node and the engine
            playerNode.pause()
            
            // Update the state
            state = .paused
            engine.pause()
        }
    

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2018-02-06
      • 2020-08-11
      • 1970-01-01
      • 2018-06-02
      • 2016-08-13
      相关资源
      最近更新 更多