Happy Christmas with The Nutcrackers
Classic for Christmas🎅🎄
Hi, there. Have you heard The Nutcrackers?
By the Wiki explanation
The Nutcracker is an 1892 two-act ballet , originally choreographed by Marius Petipa and Lev Ivanov with a score by Pyotr Ilyich Tchaikovsky (Op. 71). The libretto is adapted from E. T. A. Hoffmann’s story “The Nutcracker and the Mouse King”.
If you like to see more, you can ckack!
So, I will show My app First!
Simply Main Storyboard!
Now we start to see one by one!
First View Controller
I am Sure you can make Table View So I will skip that part.
import UIKit
import AVFoundation
var songs:[String] = []
var audioPlayer = AVAudioPlayer()
var thisSong = 0
var audioStuffed = false
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return songs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = songs[indexPath.row]
cell.textLabel?.font = UIFont(name:"Menlo", size:22)
cell.textLabel?.textAlignment = .center
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//If user tab the musicname, play the music
do{
let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!)as URL)
audioPlayer.play()
thisSong = indexPath.row
audioStuffed = true
}
catch{
print("ERROR!")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
gettingSongName()
}
func gettingSongName() {
let folderURL = URL(fileURLWithPath: Bundle.main.resourcePath!)
do {
let songPath = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
// loop through the found urls
for song in songPath {
var mySong = song.absoluteString
if mySong.contains(".mp3") {
let findString = mySong.components(separatedBy: "/")
mySong = (findString[findString.count-1])
mySong = mySong.replacingOccurrences(of: "%20", with: " ")
mySong = mySong.replacingOccurrences(of: ".mp3", with: "")
songs.append(mySong)
}
}
myTableView.reloadData()
}
catch{
print ("ERROR!")
}
}
}
Second View Controller
I Found the pretty buttons at :
It is free and have good icons you can go and see!
import UIKit
import AVFoundation
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var myImageView: UIImageView!
@IBAction func playButton(_ sender: UIButton) {
if audioStuffed == true && audioPlayer.isPlaying == false {
audioPlayer.play()
}
}
@IBAction func pauseButton(_ sender: UIButton) {
if audioStuffed == true && audioPlayer.isPlaying {
audioPlayer.pause()
}
}
@IBAction func previousButton(_ sender: UIButton) {
if thisSong > 0 && audioStuffed == true {
playThis(thisOne: songs[thisSong-1])
thisSong -= 1
label.text = songs[thisSong]
}
}
@IBAction func nextButton(_ sender: UIButton) {
if thisSong < songs.count - 1 && audioStuffed == true {
playThis(thisOne: songs[thisSong+1])
thisSong += 1
label.text = songs[thisSong]
}
}
@IBAction func slider(_ sender: UISlider) {
if audioStuffed == true {
audioPlayer.volume = sender.value
}
}
// I like to make this progress Bar, When Music play, the progress Bar will move
@IBOutlet weak var musicProgress: UIProgressView!
func playThis(thisOne:String) {
do{
let audioPath = Bundle.main.path(forResource: thisOne, ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!)as URL)
audioPlayer.play()
}
catch{
print("ERROR!")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
label.text = songs[thisSong]
let playbackTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) {[weak self] (timer) in
let total = audioPlayer.duration
let progressPercentage = audioPlayer.currentTime / total
self?.musicProgress.progress = Float(progressPercentage)
}
}
}
I see this page for progress bar and Music player
Music are not allowed, If you have mp3 file, you can open and try!
Or send me message!
Happy holiday 🥴
Haha🤣