I have a question, can you tell me how to download .mp3 files from Firebase Storage?
The program works without Firebase
import UIKit
import AVFoundation
class Audio_ViewController: UIViewController, AVAudioPlayerDelegate {
@IBOutlet weak var titleBlurEffect: UIVisualEffectView!
@IBOutlet weak var effect: UIVisualEffectView!
@IBOutlet weak var coverImage: UIImageView!
@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var artistSong: UILabel!
@IBOutlet weak var titleSong: UILabel!
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var playPause: UIButton!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var durationLbl: UILabel!
@IBOutlet weak var currentTimeLbl: UILabel!
@IBOutlet weak var trackNow: UILabel!
@IBOutlet weak var trackCount: UILabel!
var time = 0
var trackID: Int = 0
var library = MusicLibrary().library
var audioPlayer: AVAudioPlayer!
var timer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
self.titleBlurEffect.alpha = 0.8
self.effect.alpha = 0.5
if let cover = library[trackID]["cover"] {
coverImage.image = UIImage(named: "\(cover).jpg")
}
titleLbl.text = LocalizableManager.getLocalizable(key: "Player" )
if LocalizableManager.type == "hy" {
titleLbl.font = titleLbl.font.withSize(27)
}
artistSong.text = LocalizableManager.getLocalizable(key:(library[trackID]["artist"]!))
titleSong.text = LocalizableManager.getLocalizable(key:(library[trackID]["title"]!))
trackNow.text = String(trackID + 1)
trackCount.text = String(library.count)
progressView.transform = progressView.transform.scaledBy(x: 1, y: 3)
progressView.progress = 0.0
progressView.layer.cornerRadius = 0
progressView.clipsToBounds = true
progressView.layer.sublayers![1].cornerRadius = 1.5
progressView.subviews[1].clipsToBounds = true
music()
}
func music() {
let path = Bundle.main.path(forResource: "\(trackID)", ofType: "mp3")
if let path = path {
let mp3URL = NSURL(fileURLWithPath: path)
do {
audioPlayer = try AVAudioPlayer(contentsOf: mp3URL as URL)
audioPlayer.delegate = self
// audioPlayer.prepareToPlay()
// audioPlayer.play()
if (audioPlayer!.prepareToPlay())
{
audioPlayer!.play()
}
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(Audio_ViewController.updateProgressView), userInfo: nil, repeats: true)
progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: true)
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
func updateTimer() {
guard let player = audioPlayer else { return }
// Update time remaining label
let remainingTimeInSeconds = player.duration
durationLbl.text = getFormattedTime(timeInterval: remainingTimeInSeconds)
currentTimeLbl.text = getFormattedTime(timeInterval: player.currentTime)
}
@objc func updateProgressView() {
if audioPlayer.isPlaying {
progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: true)
time += 1
updateTimer()
trackNow.text = String(trackID + 1)
trackCount.text = String(library.count)
}
}
@IBAction func playPause(_ sender: Any) {
if (audioPlayer.isPlaying == true) {
audioPlayer.pause()
updateProgressView()
playPause.setImage(UIImage(named: "play"), for: UIControl.State.normal)
} else {
audioPlayer.play()
playPause.setImage(UIImage(named: "pause"), for: UIControl.State.normal)
}
}
@IBAction func nextSong(_ sender: Any) {
playPause.setImage(UIImage(named: "pause"), for: UIControl.State.normal)
if trackID == 0 || trackID < library.count - 1 {
trackID += 1
} else if trackID == library.count - 1 {
trackID = library.count - trackID - 1
}
if let cover = library[trackID]["cover"] {
coverImage.image = UIImage(named: "\(cover).jpg")
}
artistSong.text = LocalizableManager.getLocalizable(key:(library[trackID]["artist"]!))
titleSong.text = LocalizableManager.getLocalizable(key:(library[trackID]["title"]!))
timer.invalidate()
time = 0
audioPlayer.currentTime = 0
progressView.progress = 0
music()
}
@IBAction func prevSong(_ sender: Any) {
playPause.setImage(UIImage(named: "pause"), for: UIControl.State.normal)
if trackID != 0 || trackID > 0 {
trackID -= 1 }
else if trackID == 0 {
trackID = library.count - 1 - trackID
}
if let cover = library[trackID]["cover"] {
coverImage.image = UIImage(named: "\(cover).jpg")
}
artistSong.text = LocalizableManager.getLocalizable(key:(library[trackID]["artist"]!))
titleSong.text = LocalizableManager.getLocalizable(key:(library[trackID]["title"]!))
timer.invalidate()
time = 0
audioPlayer.currentTime = 0
progressView.progress = 0
music()
}
@IBAction func fastForward(_ sender: Any) {
var time: TimeInterval = audioPlayer.currentTime
time += 7.0
audioPlayer.currentTime = time
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(Audio_ViewController.updateProgressView), userInfo: nil, repeats: true)
progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: true)
updateTimer()
}
@IBAction func rewind(_ sender: Any) {
var time: TimeInterval = audioPlayer.currentTime
time -= 7.0
audioPlayer.currentTime = time
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(Audio_ViewController.updateProgressView), userInfo: nil, repeats: true)
progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: true)
updateTimer()
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
if trackID == 0 || trackID < library.count - 1 {
trackID += 1
} else if trackID == library.count - 1 {
trackID = library.count - trackID - 1
}
if let cover = library[trackID]["cover"] {
coverImage.image = UIImage(named: "\(cover).jpg")
}
artistSong.text = library[trackID]["artist"]
titleSong.text = library[trackID]["title"]
timer.invalidate()
time = 0
audioPlayer.currentTime = 0
progressView.progress = 0
music()
}
}
func getFormattedTime(timeInterval: TimeInterval) -> String {
let mins = timeInterval / 60
let secs = timeInterval.truncatingRemainder(dividingBy: 60)
let timeformatter = NumberFormatter()
timeformatter.minimumIntegerDigits = 2
timeformatter.minimumFractionDigits = 0
timeformatter.roundingMode = .down
guard let minsStr = timeformatter.string(from: NSNumber(value: mins)), let secsStr = timeformatter.string(from: NSNumber(value: secs)) else {
return ""
}
return "\(minsStr):\(secsStr)"
}
@IBAction func editValue(_ sender: UISlider) {
audioPlayer.volume = slider.value
}
@IBAction func backBtn(_ sender: Any) {
dismiss(animated: true, completion: nil)
audioPlayer.stop()
}
}