【问题标题】:Can not find MPRemoteCommandCenter in scope在范围内找不到 MPRemoteCommandCenter
【发布时间】:2020-12-04 11:07:46
【问题描述】:

我创建了一个音乐应用程序,现在我想添加一个功能来从锁定屏幕和控制中心控制音乐,但我收到错误消息,MPRemoteCommandCenter 不在范围内。

这是我的代码的样子。

我正在 XCode 12 中开发,但适用于 iOS 12.4。

import UIKit
import AVKit

class SongViewController: UIViewController {
    
    
   
    
override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeRight.direction = .right
        self.view.addGestureRecognizer(swipeRight)
        
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeLeft.direction = .left
        self.view.addGestureRecognizer(swipeLeft)
        
        setupRemoteTransportControls()
        setupNowPlaying()
        
        
    }
    
    func setupRemoteTransportControls() {
        
        let commandCenter = MPRemoteCommandCenter.shared()

       
        commandCenter.playCommand.addTarget { [unowned self] event in
            print("Play command - is playing: \(self.player.isPlaying)")
            if !self.player.isPlaying {
                self.play()
                return .success
            }
            return .commandFailed
        }

        // Add handler for Pause Command
        commandCenter.pauseCommand.addTarget { [unowned self] event in
            print("Pause command - is playing: \(self.player.isPlaying)")
            if self.player.isPlaying {
                self.pause()
                return .success
            }
            return .commandFailed
        }
    }

【问题讨论】:

    标签: ios swift xcode avplayer mpremotecommandcenter


    【解决方案1】:

    您需要导入MediaPlayer 框架

    import UIKit
    import AVKit
    import MediaPlayer
    
    ...
    
    

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 2021-06-18
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多