首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPNowPlayingInfoCenter没有显示细节

MPNowPlayingInfoCenter没有显示细节
EN

Stack Overflow用户
提问于 2016-09-08 16:38:59
回答 1查看 853关注 0票数 1

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

以下是问题所在:

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

代码语言:javascript
复制
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被称为init,之后,我使用"itemToPlay“方法来更改视频。我还正确地(我认为)在AppDelegate中配置了音频会话:

代码语言:javascript
复制
do
    {
        let session = AVAudioSession.sharedInstance()
        try session.setCategory(AVAudioSessionCategoryPlayback)
        try session.setActive(true)
    }
    catch
    {
        print("[AppDelegate] Something went wrong")
    }

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

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-09-16 09:37:52

updatesNowPlayingInfoCenter,10开始,AVPlayerViewController中有一个名为BOOL的属性,该属性具有默认值:YES。只需将其改为NO

代码语言:javascript
复制
//playerController is an instance of AVPlayerViewController
if ([self.playerController respondsToSelector:@selector(setUpdatesNowPlayingInfoCenter:)])
{
    self.playerController.updatesNowPlayingInfoCenter = NO;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39396224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档