播放音频时设置自定义元数据(控制中心、锁屏...)

Set custom metadata when audio is playing (Control Centre, Lockscreen...)

总结

我目前正在尝试为每次播放音频时设置应用范围内的默认元数据。目前,只有 URL 的音频流显示在控制中心和锁屏上。我正在尝试自己的自定义值;但是,我无法让它工作。可能值得注意的是,音频是通过 UIWebView.

播放的

(下图续)

ViewController.swift

import UIKit
import AVFoundation
import MediaPlayer

class ViewController: UIViewController, UIWebViewDelegate, UIScrollViewDelegate {

    var mPlayer: MPMediaItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        let image = UIImage(named: "Artwork")!
        let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
                return image
            })

        let nowPlaying: NSDictionary = [MPMediaItemPropertyTitle: "Song",
                                        MPMediaItemPropertyArtist: "Artist",
                                        MPMediaItemPropertyArtwork: artwork]

        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlaying as? [String : Any]
    }
}

您必须使用初始化程序初始化 MPMediaItemArtwork

let image = UIImage(named: "Artwork")!
let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
    return image
})