ios - MPNowPlayingInfoCenter 不显示详细信息

标签 ios swift

今天我花了几个小时试图找出为什么 MPNowPlayingInfoCenter 不工作,但没有成功。我希望它在控制中心和锁屏中显示信息,媒体是视频。

问题是:

我有一个名为 GlobalAVPlayer 的单例类,它包含一个 AVPlayerViewController。它是单例,因为必须只有一个,我需要全局访问它。

class GlobalAVPlayer: NSObject {
static let sharedInstance = GlobalAVPlayer()

private var _currentVideo: Video?

var playerViewController = AVPlayerViewController()

var isPlaying: Bool = false
var almostPlaying: Bool = false
var hasItemToPlay: Bool = false

var currentVideo: Video?
{
    set {
        _currentVideo = newValue
        notify_VideoChanged()
    }
    get {
        return _currentVideo
    }
}

private var player: AVPlayer!

override init()
{
    super.init()
    player = AVPlayer()
    playerViewController.player = player
    
    player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.New, context: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didPlayToEnd), name: "AVPlayerItemDidPlayToEndTimeNotification", object: nil)
    
}

func itemToPlay(item: AVPlayerItem)
{
    if let player = player {
        almostPlaying = true
        hasItemToPlay = true
        player.replaceCurrentItemWithPlayerItem(item)
    }
}

func didPlayToEnd()
{
    print("[GlobalAVPlayer] End video notification")
    let time = CMTimeMakeWithSeconds(0, 1)
    player.seekToTime(time)
}
func play()
{
    if player.rate == 0
    {
        player.play()
        if player.rate != 0 && player.error == nil
        {
            isPlaying = true
            print("[GlobalAVPlayer] Playing video without errors")
        }
    }
}

func pause()
{
    if player.rate == 1
    {
        player.pause()
        if player.rate == 0 && player.error == nil
        {
            isPlaying = false
            print("[GlobalAVPlayer] Pausing video without errors")
        }
    }
}

func notify_PlaybackChanged()
{
    NSNotificationCenter.defaultCenter().postNotificationName("globalAVPlayerPlaybackChanged", object: self)
}

func notify_VideoChanged()
{
    NSNotificationCenter.defaultCenter().postNotificationName("globalAVPlayerVideoChanged", object: self)
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath == "rate" {
        let newRate = change!["new"] as! Int
        //rate = 0 (il player è fermo)      rate = 1 (il player sta andando)
        self.isPlaying = newRate == 1 ? true : false
        notify_PlaybackChanged()
    }
}

deinit
{
    player.removeObserver(self, forKeyPath: "rate", context: nil)
}
}

应用程序启动时调用 init 一次,之后我使用“itemToPlay”方法更改视频。 我还正确地(我认为)在 AppDelegate 中配置了 Audio Session :

do
    {
        let session = AVAudioSession.sharedInstance()
        try session.setCategory(AVAudioSessionCategoryPlayback)
        try session.setActive(true)
    }
    catch
    {
        print("[AppDelegate] Something went wrong")
    }

我试着把 MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = something 放在任何地方,但是当我把它放入 时,我最多只能在控制中心看到 1 秒的视频标题play() 方法。之后它消失了,播放控件变灰了。信息已正确存储,因为当我将 nowPlayingInfo 内容打印到控制台时,一切都符合预期。 我尝试在不同的地方使用 becomeFirstResponderUIApplication.beginReceivingRemoteControlEvents 但没有成功。

谢谢

最佳答案

从 iOS 10 开始,AVPlayerViewController 中有一个名为 updatesNowPlayingInfoCenterBOOL 属性,它具有默认值:。只需将其更改为 NO:

//playerController is an instance of AVPlayerViewController
if ([self.playerController respondsToSelector:@selector(setUpdatesNowPlayingInfoCenter:)])
{
    self.playerController.updatesNowPlayingInfoCenter = NO;
}

关于ios - MPNowPlayingInfoCenter 不显示详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39396224/

相关文章:

ios - CollectionView水平滚动约束问题

ios - 将 CLLocationpoints 保存到 Parse 然后检索它们的最佳方法

ios - Swift 3 照片拍摄

ios - 如何将此 OpenGL 指针数学转换为 Swift?

ios - AppDelegate 应该用于共享数据吗?

objective-c - 将 JSON 结果解析为 NSString 或 RestKit 中类似的 "primitive"数据类型

ios - iOS:从字典获取数组中的值

ios - 如何将数据从iOS中的数据模型存储到核心数据中,并以数据模型的格式获取

swift - 快速编写委托(delegate)引用的不同选项

Xcode 7/Swift 2.0、imageWithRenderingMode(.AlwaysTemplate) 和 EXC_BAD_ACCESS