Skip to content

Commit 7023a61

Browse files
committed
PlayerSettings: start audio playback only on explicit press on Play
1 parent c41a012 commit 7023a61

File tree

7 files changed

+40
-6
lines changed

7 files changed

+40
-6
lines changed

Amperfy/Screens/ViewController/SettingsHostVC.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ class SettingsHostVC: UIViewController {
122122
self.appDelegate.storage.settings.isScrobbleStreamedItems = newValue
123123
}))
124124

125+
settings.isPlaybackStartOnlyOnPlay = self.appDelegate.storage.settings.isPlaybackStartOnlyOnPlay
126+
changesAgent.append(settings.$isPlaybackStartOnlyOnPlay.sink(receiveValue: { newValue in
127+
self.appDelegate.storage.settings.isPlaybackStartOnlyOnPlay = newValue
128+
}))
129+
125130
settings.swipeActionSettings = self.appDelegate.storage.settings.swipeActionSettings
126131
changesAgent.append(settings.$swipeActionSettings.sink(receiveValue: { newValue in
127132
self.appDelegate.storage.settings.swipeActionSettings = newValue

Amperfy/SwiftUI/Settings/ObservableSettings.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ final class Settings: ObservableObject {
3838
@Published var isAutoCacheLatestPodcastEpisodes = false
3939
@Published var isPlayerAutoCachePlayedItems = false
4040
@Published var isScrobbleStreamedItems = false
41+
@Published var isPlaybackStartOnlyOnPlay = false
4142
@Published var isShowMusicPlayerSkipButtons = false
4243
@Published var swipeActionSettings = SwipeActionSettings(leading: [], trailing: [])
4344
@Published var cacheSizeLimit : Int = 0 // limit in byte

Amperfy/SwiftUI/Settings/PlayerSettingsView.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ struct PlayerSettingsView: View {
206206
, footer: {
207207
Text("Transcoding is recommended due to incompatibility with some formats. Changes will not effect already downloaded songs, if this is wanted: Clear cache and redownload. \(((appDelegate.storage.loginCredentials?.backendApi ?? .ampache) == .ampache) ? "" : "\nIf cache format 'raw' is selected Amperfy will use the Subsonic API action 'download' for caching. Every other option requires Amperfy to use the Subsonic API action 'stream' for caching. Only 'stream' allows server side transcoding. Please check for correct server configuration regarding the active API action.")")
208208
})
209+
210+
Section(content: {
211+
HStack {
212+
Text("Start audio playback only on explicit press on Play")
213+
Spacer()
214+
Toggle(isOn: $settings.isPlaybackStartOnlyOnPlay) {}
215+
.frame(width: 130)
216+
}
217+
}
218+
, footer: {
219+
Text("When enabled, audio playback starts only when the Play button is actively pressed. Otherwise, audio playback starts automatically.")
220+
})
209221
}
210222
}
211223
.navigationTitle("Player, Stream & Scrobble")

AmperfyKit/AmperfyKit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class AmperKit {
8181
}
8282
let playerData = storage.main.library.getPlayerData()
8383
let queueHandler = PlayQueueHandler(playerData: playerData)
84-
let curPlayer = AudioPlayer(coreData: playerData, queueHandler: queueHandler, backendAudioPlayer: backendAudioPlayer, userStatistics: userStatistics)
84+
let curPlayer = AudioPlayer(coreData: playerData, queueHandler: queueHandler, backendAudioPlayer: backendAudioPlayer, settings: storage.settings, userStatistics: userStatistics)
8585
audioSessionHandler.musicPlayer = curPlayer
8686
audioSessionHandler.configureObserverForAudioSessionInterruption()
8787

AmperfyKit/Player/AudioPlayer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ public class AudioPlayer: NSObject, BackendAudioPlayerNotifiable {
4444
private var playerStatus: PlayerStatusPersistent
4545
private var queueHandler: PlayQueueHandler
4646
private let backendAudioPlayer: BackendAudioPlayer
47+
private let settings: PersistentStorage.Settings
4748
private let userStatistics: UserStatistics
4849
private var notifierList = [MusicPlayable]()
4950

50-
init(coreData: PlayerStatusPersistent, queueHandler: PlayQueueHandler, backendAudioPlayer: BackendAudioPlayer, userStatistics: UserStatistics) {
51+
init(coreData: PlayerStatusPersistent, queueHandler: PlayQueueHandler, backendAudioPlayer: BackendAudioPlayer, settings: PersistentStorage.Settings, userStatistics: UserStatistics) {
5152
self.playerStatus = coreData
5253
self.queueHandler = queueHandler
5354
self.backendAudioPlayer = backendAudioPlayer
5455
self.backendAudioPlayer.isAutoCachePlayedItems = coreData.isAutoCachePlayedItems
56+
self.settings = settings
5557
self.userStatistics = userStatistics
5658
super.init()
5759
self.backendAudioPlayer.responder = self
@@ -79,7 +81,7 @@ public class AudioPlayer: NSObject, BackendAudioPlayerNotifiable {
7981
private func insertIntoPlayer(playable: AbstractPlayable) {
8082
userStatistics.playedItem(repeatMode: playerStatus.repeatMode, isShuffle: playerStatus.isShuffle)
8183
playable.countPlayed()
82-
backendAudioPlayer.requestToPlay(playable: playable, playbackRate: playerStatus.playbackRate)
84+
backendAudioPlayer.requestToPlay(playable: playable, playbackRate: playerStatus.playbackRate, autoStartPlayback: !self.settings.isPlaybackStartOnlyOnPlay)
8385
}
8486

8587
//BackendAudioPlayerNotifiable

AmperfyKit/Player/BackendAudioPlayer.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class BackendAudioPlayer {
144144
player.seek(to: CMTime(seconds: toSecond, preferredTimescale: CMTimeScale(NSEC_PER_SEC)))
145145
}
146146

147-
func requestToPlay(playable: AbstractPlayable, playbackRate: PlaybackRate) {
147+
func requestToPlay(playable: AbstractPlayable, playbackRate: PlaybackRate, autoStartPlayback: Bool) {
148148
userDefinedPlaybackRate = playbackRate
149149
if let relFilePath = playable.relFilePath,
150150
fileManager.fileExits(relFilePath: relFilePath) {
@@ -153,7 +153,11 @@ class BackendAudioPlayer {
153153
return
154154
}
155155
insertCachedPlayable(playable: playable)
156-
self.continuePlay()
156+
if autoStartPlayback {
157+
self.continuePlay()
158+
} else {
159+
isPlaying = false
160+
}
157161
self.responder?.notifyItemPreparationFinished()
158162
} else if !isOfflineMode{
159163
guard playable.isPlayableOniOS || backendApi.isStreamingTranscodingActive else {
@@ -167,7 +171,11 @@ class BackendAudioPlayer {
167171
if self.isAutoCachePlayedItems {
168172
self.playableDownloader.download(object: playable)
169173
}
170-
self.continuePlay()
174+
if autoStartPlayback {
175+
self.continuePlay()
176+
} else {
177+
self.isPlaying = false
178+
}
171179
self.responder?.notifyItemPreparationFinished()
172180
}.catch { error in
173181
self.responder?.notifyErrorOccured(error: error)

AmperfyKit/Storage/PersistentStorage.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public class PersistentStorage {
248248
case IsAutoDownloadLatestSongsActive = "isAutoDownloadLatestSongsActive"
249249
case IsAutoDownloadLatestPodcastEpisodesActive = "isAutoDownloadLatestPodcastEpisodesActive"
250250
case IsScrobbleStreamedItems = "isScrobbleStreamedItems"
251+
case IsPlaybackStartOnlyOnPlay = "isPlaybackStartOnlyOnPlay"
251252
case LibrarySyncVersion = "librarySyncVersion"
252253

253254
case LibrarySyncInfoReadByUser = "librarySyncInfoReadByUser"
@@ -453,6 +454,11 @@ public class PersistentStorage {
453454
set { UserDefaults.standard.set(newValue, forKey: UserDefaultsKey.IsScrobbleStreamedItems.rawValue) }
454455
}
455456

457+
public var isPlaybackStartOnlyOnPlay: Bool {
458+
get { return UserDefaults.standard.object(forKey: UserDefaultsKey.IsPlaybackStartOnlyOnPlay.rawValue) as? Bool ?? false }
459+
set { UserDefaults.standard.set(newValue, forKey: UserDefaultsKey.IsPlaybackStartOnlyOnPlay.rawValue) }
460+
}
461+
456462
public var isOnlineMode: Bool {
457463
return !isOfflineMode
458464
}

0 commit comments

Comments
 (0)