0 Replies
      Latest reply on Apr 6, 2019 3:25 PM by alo21
      alo21 Level 1 Level 1 (0 points)

        Hi, I am developing a music player. I used two viewContainers, one to list the songs, another as a media player (with play, pause and fast-forward). My problem is that music stops immediately after I selected the song.  It seems like the media player controller isn't kept alive all the time. I'll post a bit of code to understand what I mean.

         

        //
        //  MediaPlayerViewController.swift
        //  Moment
        //
        //  Created by Alessandro Losavio on 06/04/2019.
        //  Copyright © 2019 Losavio. All rights reserved.
        //
        
        import UIKit
        import AVFoundation
        
        
        class MediaPlayerViewController: UIViewController {
            
            
            @IBOutlet var forwardButton: UIButton!
            @IBOutlet var backwardButton: UIButton!
            @IBOutlet var songNameLabel: UILabel!
            @IBOutlet var albumImage: UIImageView!
        
            var AudioPlayer = AVAudioPlayer()
        
            override func viewDidLoad() {
                super.viewDidLoad()
                
        
            }
            
            func playTrack(filePath: URL){
                
                print(filePath)
                
                do {
                    print("Yeee")
                    AudioPlayer = try AVAudioPlayer(contentsOf: filePath)
                    AudioPlayer.play()
                    print("lol")
                    
                } catch {
                    print("Something went wrong")
                }
                
                
            }
            
            
            @IBAction func playPressed(_ sender: Any) {
                
                print("Print pressed")
            }
            
            
            
        
        
        }
        
        

         

         

         

         

        And here's where the track come from...

         

        //
        //  PlaylistTracksTableViewController.swift
        //  Moment
        //
        //  Created by Alessandro Losavio on 02/04/2019.
        //  Copyright © 2019 Losavio. All rights reserved.
        //
        
        import UIKit
        import AVFoundation
        
        class PlaylistTracksTableViewController: UITableViewController {
            
            
            var AudioPlayer = AVAudioPlayer()
        
            
            var playlist: playlist!
        
            override func viewDidLoad() {
                super.viewDidLoad()
                
                PlaylistTracksNetwork().getPlaylistTracks(playlistCode: playlist.id, completionHandler: updateTableView)
                
            }
        
            // MARK: - Table view data source
        
            override func numberOfSections(in tableView: UITableView) -> Int {
                return 1
            }
            
            func updateTableView(){
                tableView.reloadData()
            }
        
            override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return PlaylistTracksData().getPlaylistsTracks().count
            }
        
            override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                
                let cell = tableView.dequeueReusableCell(withIdentifier: "PlaylistTrackCell", for: indexPath)
                let playlist = PlaylistTracksData().getPlaylistsTracks()[(indexPath as NSIndexPath).row]
                
                cell.textLabel?.text = playlist.name
                
                //cell.imageView?.image = meme.finalImage
                
                return cell
            }
            
            
            override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                
                let track = PlaylistTracksData().getPlaylistsTracks()[(indexPath as NSIndexPath).row]
                
                PlaylistTracksNetwork().downloadTrack(trackUrl: track.audio, completionHandler: MediaPlayerViewController().playTrack)
        
                }
        

         

         

        Can someone help me, please?

         

        Thanks in advance