mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-30 14:49:47 +02:00
Moved NowPlaingInfo to its own class
This commit is contained in:
parent
d0b7a958aa
commit
e998a03571
5 changed files with 86 additions and 65 deletions
|
@ -10,14 +10,6 @@ import AVFoundation
|
||||||
import UIKit
|
import UIKit
|
||||||
import MediaPlayer
|
import MediaPlayer
|
||||||
|
|
||||||
func getData(from url: URL, completion: @escaping (UIImage?) -> Void) {
|
|
||||||
URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
|
|
||||||
if let data = data {
|
|
||||||
completion(UIImage(data:data))
|
|
||||||
}
|
|
||||||
}).resume()
|
|
||||||
}
|
|
||||||
|
|
||||||
class AudioPlayer: NSObject {
|
class AudioPlayer: NSObject {
|
||||||
// enums and @objc are not compatible
|
// enums and @objc are not compatible
|
||||||
@objc dynamic var status: Int
|
@objc dynamic var status: Int
|
||||||
|
@ -28,7 +20,6 @@ class AudioPlayer: NSObject {
|
||||||
|
|
||||||
private var playerContext = 0
|
private var playerContext = 0
|
||||||
private var playerItemContext = 0
|
private var playerItemContext = 0
|
||||||
private var nowPlayingInfo: [String: Any] = [:]
|
|
||||||
|
|
||||||
private var playWhenReady: Bool
|
private var playWhenReady: Bool
|
||||||
|
|
||||||
|
@ -46,7 +37,7 @@ class AudioPlayer: NSObject {
|
||||||
|
|
||||||
initAudioSession()
|
initAudioSession()
|
||||||
setupRemoteTransportControls()
|
setupRemoteTransportControls()
|
||||||
invokeMetadataUpdate()
|
NowPlayingInfo.setAudiobook(audiobook: audiobook)
|
||||||
|
|
||||||
// Listen to player events
|
// Listen to player events
|
||||||
self.audioPlayer.addObserver(self, forKeyPath: #keyPath(AVPlayer.rate), options: .new, context: &playerContext)
|
self.audioPlayer.addObserver(self, forKeyPath: #keyPath(AVPlayer.rate), options: .new, context: &playerContext)
|
||||||
|
@ -67,10 +58,6 @@ class AudioPlayer: NSObject {
|
||||||
pause()
|
pause()
|
||||||
audioPlayer.replaceCurrentItem(with: nil)
|
audioPlayer.replaceCurrentItem(with: nil)
|
||||||
|
|
||||||
DispatchQueue.main.sync {
|
|
||||||
UIApplication.shared.endReceivingRemoteControlEvents()
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try AVAudioSession.sharedInstance().setActive(false)
|
try AVAudioSession.sharedInstance().setActive(false)
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -78,10 +65,9 @@ class AudioPlayer: NSObject {
|
||||||
print(error)
|
print(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
nowPlayingInfo.removeAll()
|
DispatchQueue.main.sync {
|
||||||
nowPlayingInfo[MPMediaItemPropertyTitle] = ""
|
UIApplication.shared.endReceivingRemoteControlEvents()
|
||||||
nowPlayingInfo[MPMediaItemPropertyArtist] = ""
|
}
|
||||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Methods
|
// MARK: - Methods
|
||||||
|
@ -162,9 +148,6 @@ class AudioPlayer: NSObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func shouldFetchCover() -> Bool {
|
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyExternalContentIdentifier] as? String != audiobook.streamId || nowPlayingInfo[MPMediaItemPropertyArtwork] == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Now playing
|
// MARK: - Now playing
|
||||||
func setupRemoteTransportControls() {
|
func setupRemoteTransportControls() {
|
||||||
|
@ -216,7 +199,7 @@ class AudioPlayer: NSObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
commandCenter.changePlaybackRateCommand.isEnabled = true
|
commandCenter.changePlaybackRateCommand.isEnabled = true
|
||||||
commandCenter.changePlaybackRateCommand.supportedPlaybackRates = [0.5, 0.75, 0.9, 1, 1.2, 1.5, 2]
|
commandCenter.changePlaybackRateCommand.supportedPlaybackRates = [0.5, 0.75, 1.0, 1.25, 1.5, 2]
|
||||||
commandCenter.changePlaybackRateCommand.addTarget { event in
|
commandCenter.changePlaybackRateCommand.addTarget { event in
|
||||||
guard let event = event as? MPChangePlaybackRateCommandEvent else {
|
guard let event = event as? MPChangePlaybackRateCommandEvent else {
|
||||||
return .noSuchContent
|
return .noSuchContent
|
||||||
|
@ -227,49 +210,8 @@ class AudioPlayer: NSObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func invokeMetadataUpdate() {
|
|
||||||
if !shouldFetchCover() || audiobook.artworkUrl == nil {
|
|
||||||
setMetadata(nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let url = URL(string: audiobook.artworkUrl!) else { return }
|
|
||||||
getData(from: url) { [weak self] image in
|
|
||||||
guard let self = self,
|
|
||||||
let downloadedImage = image else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
let artwork = MPMediaItemArtwork.init(boundsSize: downloadedImage.size, requestHandler: { _ -> UIImage in
|
|
||||||
return downloadedImage
|
|
||||||
})
|
|
||||||
|
|
||||||
self.setMetadata(artwork)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func setMetadata(_ artwork: MPMediaItemArtwork?) {
|
|
||||||
if artwork != nil {
|
|
||||||
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
|
|
||||||
} else if shouldFetchCover() {
|
|
||||||
nowPlayingInfo[MPMediaItemPropertyArtwork] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyExternalContentIdentifier] = audiobook.streamId
|
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyAssetURL] = URL(string: audiobook.playlistUrl)
|
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = false
|
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = "hls"
|
|
||||||
|
|
||||||
nowPlayingInfo[MPMediaItemPropertyTitle] = audiobook.title
|
|
||||||
nowPlayingInfo[MPMediaItemPropertyArtist] = audiobook.author ?? "unknown"
|
|
||||||
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = audiobook.series
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateNowPlaying() {
|
func updateNowPlaying() {
|
||||||
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = getDuration()
|
NowPlayingInfo.update(duration: getDuration(), currentTime: getCurrentTime(), rate: rate)
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = getCurrentTime()
|
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = rate
|
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1.0
|
|
||||||
|
|
||||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Observer
|
// MARK: - Observer
|
79
ios/App/App/util/NowPlayingInfo.swift
Normal file
79
ios/App/App/util/NowPlayingInfo.swift
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
//
|
||||||
|
// NowPlaying.swift
|
||||||
|
// App
|
||||||
|
//
|
||||||
|
// Created by Rasmus Krämer on 22.03.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import MediaPlayer
|
||||||
|
|
||||||
|
func getData(from url: URL, completion: @escaping (UIImage?) -> Void) {
|
||||||
|
URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
|
||||||
|
if let data = data {
|
||||||
|
completion(UIImage(data:data))
|
||||||
|
}
|
||||||
|
}).resume()
|
||||||
|
}
|
||||||
|
|
||||||
|
class NowPlayingInfo {
|
||||||
|
private static var nowPlayingInfo: [String: Any] = [:]
|
||||||
|
private static var audiobook: Audiobook?
|
||||||
|
|
||||||
|
public static func setAudiobook(audiobook: Audiobook) {
|
||||||
|
self.audiobook = audiobook
|
||||||
|
setMetadata(nil)
|
||||||
|
|
||||||
|
if !shouldFetchCover() || audiobook.artworkUrl == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let url = URL(string: audiobook.artworkUrl!) else { return }
|
||||||
|
getData(from: url) { [self] image in
|
||||||
|
guard let downloadedImage = image else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let artwork = MPMediaItemArtwork.init(boundsSize: downloadedImage.size, requestHandler: { _ -> UIImage in
|
||||||
|
return downloadedImage
|
||||||
|
})
|
||||||
|
|
||||||
|
self.setMetadata(artwork)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static func update(duration: Double, currentTime: Double, rate: Float) {
|
||||||
|
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration
|
||||||
|
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime
|
||||||
|
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = rate
|
||||||
|
nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1.0
|
||||||
|
|
||||||
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
||||||
|
}
|
||||||
|
public static func reset() {
|
||||||
|
audiobook = nil
|
||||||
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func setMetadata(_ artwork: MPMediaItemArtwork?) {
|
||||||
|
if self.audiobook == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if artwork != nil {
|
||||||
|
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
|
||||||
|
} else if shouldFetchCover() {
|
||||||
|
nowPlayingInfo[MPMediaItemPropertyArtwork] = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
nowPlayingInfo[MPNowPlayingInfoPropertyExternalContentIdentifier] = audiobook!.streamId
|
||||||
|
nowPlayingInfo[MPNowPlayingInfoPropertyAssetURL] = URL(string: audiobook!.playlistUrl)
|
||||||
|
nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = false
|
||||||
|
nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = "hls"
|
||||||
|
|
||||||
|
nowPlayingInfo[MPMediaItemPropertyTitle] = audiobook!.title
|
||||||
|
nowPlayingInfo[MPMediaItemPropertyArtist] = audiobook!.author ?? "unknown"
|
||||||
|
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = audiobook!.series
|
||||||
|
}
|
||||||
|
private static func shouldFetchCover() -> Bool {
|
||||||
|
audiobook != nil && (nowPlayingInfo[MPNowPlayingInfoPropertyExternalContentIdentifier] as? String != audiobook!.streamId || nowPlayingInfo[MPMediaItemPropertyArtwork] == nil)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue