在范围内找不到 MPRemoteCommandCenter

Posted

技术标签:

【中文标题】在范围内找不到 MPRemoteCommandCenter【英文标题】:Can not find MPRemoteCommandCenter in scope 【发布时间】: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
        
    

【问题讨论】:

【参考方案1】:

您需要导入MediaPlayer 框架

import UIKit
import AVKit
import MediaPlayer

...

【讨论】:

以上是关于在范围内找不到 MPRemoteCommandCenter的主要内容,如果未能解决你的问题,请参考以下文章

Xcode ViewController 显示在范围内找不到“支付”

在范围内找不到类型“ListenerRegistration”

在范围内找不到 MPRemoteCommandCenter

在范围内找不到 UIDocumentPickerViewController

在范围内找不到类型“配置”

FCM - 在范围内找不到类型“MessagingRemoteMessage”