254 lines
6.1 KiB
Swift
254 lines
6.1 KiB
Swift
//
|
|
// FirstViewController.swift
|
|
// grandfatherrock
|
|
//
|
|
// Created by Micah Gomez on 4/15/20.
|
|
// Copyright © 2020 Micah Gomez. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import AVFoundation
|
|
|
|
public class musicHandler{
|
|
@objc class func updateMusic(){
|
|
|
|
let defaults = UserDefaults.standard
|
|
|
|
timer.invalidate()
|
|
|
|
prevVol = Double(audioPlayer.volume)
|
|
audioPlayer.setVolume(0, fadeDuration: 3)
|
|
print("updating song")
|
|
|
|
GlobalVars.hour = Calendar.current.component(.hour, from: Date())
|
|
if(GlobalVars.hour<10){
|
|
hourPadding = "0"
|
|
}
|
|
else{
|
|
hourPadding = ""
|
|
}
|
|
|
|
switch defaults.integer(forKey: "titleNo") {
|
|
case 0:
|
|
GlobalVars.titleCode = "acnh"
|
|
case 1:
|
|
GlobalVars.titleCode = "acnl"
|
|
case 2:
|
|
GlobalVars.titleCode = "accf"
|
|
case 3:
|
|
GlobalVars.titleCode = "acpg"
|
|
default:
|
|
GlobalVars.titleCode = "acnh"
|
|
}
|
|
|
|
song = GlobalVars.titleCode + hourPadding + String(GlobalVars.hour)
|
|
print(song)
|
|
|
|
do{
|
|
|
|
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: song, ofType: "mp3")!))
|
|
audioPlayer.prepareToPlay()
|
|
audioPlayer.numberOfLoops = -1
|
|
|
|
let audioSession = AVAudioSession.sharedInstance()
|
|
do{
|
|
try audioSession.setCategory(.playback, mode: .default)
|
|
try audioSession.setActive(true)
|
|
}
|
|
|
|
|
|
}
|
|
catch{
|
|
print(error)
|
|
}
|
|
if(GlobalVars.musicStarted){
|
|
audioPlayer.play()
|
|
}
|
|
audioPlayer.setVolume(Float(prevVol), fadeDuration: 1)
|
|
|
|
if(Calendar.current.component(.minute, from: Date()) == 0){
|
|
date = Date().addingTimeInterval(3600)
|
|
}
|
|
else{
|
|
let currentMinute = Calendar.current.component(.minute, from: Date())
|
|
let currentSeconds = Calendar.current.component(.second, from: Date())
|
|
let timeTillTopOfTheHour = 3600 - currentSeconds - (currentMinute * 60)
|
|
date = Date().addingTimeInterval(TimeInterval(timeTillTopOfTheHour))
|
|
}
|
|
print(date)
|
|
timer = Timer(fireAt: date, interval: 0, target: self, selector: #selector(updateMusic), userInfo: nil, repeats: false)
|
|
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
|
|
|
|
}
|
|
}
|
|
|
|
struct GlobalVars {
|
|
static var selectedGame = ""
|
|
static var selectedGameNo = 0
|
|
static var musicStarted = false
|
|
static var titleCode = "acnh"
|
|
static var titleName = UserDefaults.standard.string(forKey: "selectedTitle")
|
|
static var hour = Calendar.current.component(.hour, from: Date())
|
|
}
|
|
|
|
var audioPlayer = AVAudioPlayer()
|
|
|
|
var song = "acnh00"
|
|
//var hour = 0
|
|
var i = 0
|
|
|
|
//var calendar = Calendar.current
|
|
var date = Date()
|
|
var timer = Timer()
|
|
let currentSeconds = Calendar.current.component(.second, from: Date())
|
|
let currentMinute = Calendar.current.component(.minute, from: Date())
|
|
let timeTillTopOfTheHour = 3600 - currentSeconds - (currentMinute * 60)
|
|
var hourPadding = ""
|
|
var prevVol = 0.0
|
|
//var titleCode = "acnh"
|
|
|
|
class FirstViewController: UIViewController{
|
|
|
|
let defaults = UserDefaults.standard
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
// Do any additional setup after loading the view.
|
|
|
|
if(!defaults.bool(forKey: "didRun")){
|
|
defaults.set("New Horizons", forKey: "selectedTitle")
|
|
defaults.set(0, forKey: "titleNo")
|
|
defaults.set(true, forKey: "didRun")
|
|
print("First App run")
|
|
}
|
|
|
|
GlobalVars.selectedGame = defaults.string(forKey: "selectedTitle")!
|
|
GlobalVars.selectedGameNo = defaults.integer(forKey: "titleNo")
|
|
|
|
print(currentSeconds)
|
|
print(currentMinute)
|
|
|
|
print(timeTillTopOfTheHour)
|
|
|
|
GlobalVars.hour = Calendar.current.component(.hour, from: Date())
|
|
if(GlobalVars.hour<10){
|
|
hourPadding = "0"
|
|
}
|
|
else{
|
|
hourPadding = ""
|
|
}
|
|
|
|
switch defaults.integer(forKey: "titleNo") {
|
|
case 0:
|
|
GlobalVars.titleCode = "acnh"
|
|
case 1:
|
|
GlobalVars.titleCode = "acnl"
|
|
case 2:
|
|
GlobalVars.titleCode = "accf"
|
|
case 3:
|
|
GlobalVars.titleCode = "acpg"
|
|
default:
|
|
GlobalVars.titleCode = "acnh"
|
|
}
|
|
|
|
song = GlobalVars.titleCode + hourPadding + String(GlobalVars.hour)
|
|
print(song)
|
|
|
|
date = Date().addingTimeInterval(TimeInterval(timeTillTopOfTheHour))
|
|
print(date)
|
|
timer = Timer(fireAt: date, interval: 0, target: musicHandler.self, selector: #selector(musicHandler.updateMusic), userInfo: nil, repeats: false)
|
|
RunLoop.main.add(timer, forMode: .common)
|
|
|
|
|
|
|
|
do{
|
|
|
|
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: song, ofType: "mp3")!))
|
|
audioPlayer.prepareToPlay()
|
|
audioPlayer.numberOfLoops = -1
|
|
|
|
let audioSession = AVAudioSession.sharedInstance()
|
|
do{
|
|
try audioSession.setCategory(.playback, mode: .default)
|
|
try audioSession.setActive(true)
|
|
}
|
|
|
|
|
|
}
|
|
catch{
|
|
print(error)
|
|
}
|
|
|
|
}
|
|
|
|
@IBOutlet weak var controlButton: UIButton!
|
|
|
|
|
|
@IBAction func play(_ sender: Any) {
|
|
if(!GlobalVars.musicStarted){
|
|
|
|
GlobalVars.musicStarted = true
|
|
|
|
audioPlayer.play()
|
|
GlobalVars.musicStarted = true
|
|
controlButton.setBackgroundImage(UIImage(systemName: "pause.circle"), for: UIControl.State.normal)
|
|
}
|
|
else{
|
|
audioPlayer.pause()
|
|
GlobalVars.musicStarted = false
|
|
controlButton.setBackgroundImage(UIImage(systemName: "play.circle"), for: UIControl.State.normal)
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/*@objc func updateMusic(){
|
|
|
|
//timer.invalidate()
|
|
|
|
prevVol = Double(audioPlayer.volume)
|
|
audioPlayer.setVolume(0, fadeDuration: 3)
|
|
print("updating song")
|
|
|
|
hour = Calendar.current.component(.hour, from: date)
|
|
if(hour<10){
|
|
hourPadding = "0"
|
|
}
|
|
else{
|
|
hourPadding = ""
|
|
}
|
|
|
|
song = "acnh" + hourPadding + String(hour)
|
|
print(song)
|
|
|
|
do{
|
|
|
|
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: song, ofType: "mp3")!))
|
|
audioPlayer.prepareToPlay()
|
|
audioPlayer.numberOfLoops = -1
|
|
|
|
let audioSession = AVAudioSession.sharedInstance()
|
|
do{
|
|
try audioSession.setCategory(.playback, mode: .default)
|
|
try audioSession.setActive(true)
|
|
}
|
|
|
|
|
|
}
|
|
catch{
|
|
print(error)
|
|
}
|
|
if(GlobalVars.musicStarted){
|
|
audioPlayer.play()
|
|
}
|
|
audioPlayer.setVolume(Float(prevVol), fadeDuration: 1)
|
|
date = date.addingTimeInterval(3600)
|
|
print(date)
|
|
timer = Timer(fireAt: date, interval: 0, target: self, selector: #selector(updateMusic), userInfo: nil, repeats: false)
|
|
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
|
|
|
|
}*/
|
|
|
|
|
|
|
|
}
|