Skip to content

Commit

Permalink
AudioPlayer features #58: Support play/pause, previous and next audio…
Browse files Browse the repository at this point in the history
… on lock screen
  • Loading branch information
filimo committed Dec 31, 2019
1 parent 2f77723 commit 2e01bf5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ReaderTranslator/Extentions/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import os.log
import Foundation

var PREVIEW_MODE: Bool = false

final class Logger {
static func log(
log: OSLog = .default,
Expand Down
59 changes: 50 additions & 9 deletions ReaderTranslatorPlayer/Store/AudioStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
//

import AVFoundation
import MediaPlayer
import SwiftUI

var PREVIEW_MODE: Bool = false

final class AudioStore: NSObject, ObservableObject {
static let shared = AudioStore()

Expand Down Expand Up @@ -49,7 +48,7 @@ final class AudioStore: NSObject, ObservableObject {
do {
let sharedInstance = AVAudioSession.sharedInstance()

try sharedInstance.setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
try sharedInstance.setCategory(.playback, mode: .default, options: .init(rawValue: 0))
try sharedInstance.setActive(true)
} catch {
Logger.log(type: .error, value: error)
Expand All @@ -58,6 +57,36 @@ final class AudioStore: NSObject, ObservableObject {
if player == nil {
if let lastAudio = lastAudio { openAudio(url: lastAudio) }
}

initMPRemoteCommandCenter()
}

private func initMPRemoteCommandCenter() {
let commandCenter = MPRemoteCommandCenter.shared()

commandCenter.previousTrackCommand.isEnabled = true
commandCenter.previousTrackCommand.addTarget { _ in
self.prevPlay()
return .success
}

commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { _ in
self.isPlaying = true
return .success
}

commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { _ in
self.isPlaying = false
return .success
}

commandCenter.nextTrackCommand.isEnabled = true
commandCenter.nextTrackCommand.addTarget { _ in
self.nextPlay()
return .success
}
}
}

Expand Down Expand Up @@ -105,17 +134,29 @@ extension AudioStore {
print(error)
}
}

func play(_ url: URL?) {
guard let url = url else {
isPlaying = false
return
}
openAudio(url: url)
isPlaying = true
}

func prevPlay() {
play(FileStore.shared.prevFile(file: lastAudio))
}

func nextPlay() {
play(FileStore.shared.nextFile(file: lastAudio))
}
}

extension AudioStore: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
guard let url = FileStore.shared.nextFile(file: lastAudio) else {
isPlaying = false
return
}
openAudio(url: url)
isPlaying = true
nextPlay()
}
}
}
12 changes: 10 additions & 2 deletions ReaderTranslatorPlayer/Store/FileStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ final class FileStore: ObservableObject {

}

func prevFile(file: URL?) -> URL? {
guard let file = file else { return nil }
guard let current = files.firstIndex(of: file) else { return nil }
let index = files.index(before: current)

return files.indices.contains(index) ? files[index] : nil
}

func nextFile(file: URL?) -> URL? {
guard let file = file else { return nil }
guard let current = files.firstIndex(of: file) else { return nil }
let next = files.index(after: current)
let index = files.index(after: current)

return files.indices.contains(next) ? files[next] : nil
return files.indices.contains(index) ? files[index] : nil
}
}

0 comments on commit 2e01bf5

Please sign in to comment.