现在主流的音乐播放器基本都实现了锁屏后还能控制暂停、播放、切换的功能,之前一直没有在意,这两天闲来无事就研究了一下:
其实主要原理就是利用了iOS5.0以后出来的新API,
MPNowPlayingInfoCenter ,类实现其实很简单
MP_EXTERN_CLASS_AVAILABLE(5_0)
@interface MPNowPlayingInfoCenter : NSObject
// Returns the default now playing info center.
// The default center holds now playing info about the current application.
+ (MPNowPlayingInfoCenter *)defaultCenter;
// The current now playing info for the center.
// Setting the info to nil will clear it.
@property (copy, nullable) NSDictionary<NSString *, id> *nowPlayingInfo;
@end
可以看到是一个全局管理的单例模式,里面只有一个字典属性,用来保存在锁屏界面显示的信息;
在锁屏界面的点击事件是调用的一个返回方法,前提是要先激活一下

本文介绍了如何实现iOS音乐播放器在后台和锁屏状态下仍能进行播放、暂停和切换等操作。主要依赖于iOS5.0引入的MPNowPlayingInfoCenter API,通过激活相关方法来实现锁屏界面的控制功能。
最低0.47元/天 解锁文章
1767

被折叠的 条评论
为什么被折叠?



