Checked Audio in Background Modes:
Set up an audio session:
import UIKit import AVFoundation import MediaPlayer @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var audio: AVAudioPlayer? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let audioSession = AVAudioSession.sharedInstance() do { try audioSession.setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: []) try audioSession.setActive(true) } catch { print(error) } do { let url = Bundle.main.url(forResource: "example", withExtension: "mp3") audio = try AVAudioPlayer(contentsOf: url!) } catch { print(error) } audio?.play() var nowPlayingInfo = [String : Any]() nowPlayingInfo[MPMediaItemPropertyArtist] = "Artist" nowPlayingInfo[MPMediaItemPropertyTitle] = "My Track" if let image = UIImage(named: "image") { nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in return image } } nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = audio!.currentTime nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = audio!.duration nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = audio!.rate MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo return true } } When the screen is locked, the sound continues to play, but the player does not appear on the locked screen. Tell me, what did not finish / not doing that?
