14.5 C
New York
Wednesday, November 20, 2024

ios – The flawed knowledge from a plist is being proven when the person chooses a phase


My utility has a function that may change between LastSong and FamousSong from artist based mostly on the person choosing a phase in a UISegmentControl. After I run my app and I decide LastSong the whole lot is okay. However once I decide FamousSong it performs a random tune, not the identical artist.

That is my code:

import UIKit
import AVKit
import AVFoundation
import SwiftyGif

class SongViewController: UIViewController {
    
    @IBOutlet weak var btnStart: UIButton!
    @IBOutlet weak var imgGif: UIImageView!
    @IBOutlet weak var lbName: UILabel!
    @IBOutlet weak var displayShow: UIView!
    @IBOutlet weak var segControl: UISegmentedControl!
    
    let loadPlist = Loading()
    var currentSongName: String = ""
    var participant: AVPlayer?
    var playerLayer: AVPlayerLayer?
    var lastSongs=[String]()
    var famousSongs=[String]()
    var artistNames=[String]()

    
    var currentSongs: [String] = []
    var index=0
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        print("tune title:",currentSongName)
        print(index)
    
        playMV(songName: currentSongName)
        
        do {
            let gif = strive UIImage(gifName: "music.gif")
            imgGif.setGifImage(gif, loopCount: -1)
        } catch {
            print("Error loading GIF: eRRoR!!!!")
        }
        
    }
    
    func takeData() {
        lastSongs=loadPlist.load(file: "lastSong")
        famousSongs=loadPlist.load(file: "famousSong")
        artistNames=loadPlist.load(file: "artistName")
    }
    
    @IBAction func segControlChanged(_ sender: UISegmentedControl) {
        let selectedIndex = sender.selectedSegmentIndex
        if selectedIndex == 0 {
            currentSongs = famousSongs
        } else {
            currentSongs = lastSongs
        }
        
        lbName.textual content = currentSongs.first ?? ""
        
        currentSongName = currentSongs.first ?? ""
        
        playMV(songName: currentSongName)
    }
    
    @IBAction func btnStart(_ sender: UIButton) {
        if imgGif.isAnimatingGif() {
            imgGif.stopAnimatingGif()
            sender.setTitle("Play", for: .regular)
        } else {
            imgGif.startAnimatingGif()
            sender.setTitle("Cease", for: .regular)
        }
    }
    
    @IBAction func btnPlay(_ sender: Any) {
        if !currentSongName.isEmpty {
            participant?.play()
        }
    }
    
    @IBAction func btnPause(_ sender: Any) {
        if let participant = participant, participant.fee != 0 {
            participant.pause()
        } else {
            participant?.play()
        }
    }
    
    @IBAction func btnStop(_ sender: Any) {
        participant?.pause()
        participant?.search(to: CMTime.zero)
    }
    
    func playMV(songName: String) {
        let fullSongName = "(songName).mp4"
        print("Track:",fullSongName)
        
        if let path = Bundle.primary.path(forResource: songName, ofType: "mp4") {
            let url = URL(fileURLWithPath: path)
            participant = AVPlayer(url: url)
            
            // Take away any current participant layer
            playerLayer?.removeFromSuperlayer()
            
            // Create a brand new participant layer
            playerLayer = AVPlayerLayer(participant: participant)
            playerLayer?.body = displayShow.bounds // Set body to match displayShow
            playerLayer?.videoGravity = .resizeAspectFill // Modify video gravity
            
            // Add participant layer to displayShow
            displayShow.layer.addSublayer(playerLayer!)
            
            // Begin enjoying the video
            participant?.play()
            
            // Replace label to indicate presently enjoying tune
            lbName.textual content = songName
            currentSongName = songName // Replace the present tune title for reference
            
        } else {
            print("Cannot discover Video: (fullSongName)")
        }
    }
}

How can I repair this code?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles