0

Every time I close the app (not quit it) or turn off the screen the music stops. is their something I am missing?

https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos/enabling_background_audio

My code.

I have a file called MediaPlayer.swift

inside it I have the following

//
//  MusicPlayer.swift
//  DRN1
//
//  Created by Russell Harrower on 25/11/19.
//  Copyright © 2019 Russell Harrower. All rights reserved.
//

import Foundation
import AVFoundation


class MusicPlayer {
    static let shared = MusicPlayer()
    var player: AVPlayer?

    func startBackgroundMusic() {
        let urlString = "http://stream.radiomedia.com.au:8003/stream"
        guard let url = URL.init(string: urlString)
            else {
                return
        }
        let playerItem = AVPlayerItem.init(url: url)
        player = AVPlayer.init(playerItem: playerItem)
        player!.play()
    }
}

I am calling that from my FirstViewController.swift

override func viewDidLoad() {

          MusicPlayer.shared.startBackgroundMusic()
          // Do any additional setup after loading the view.
          self.artist.textAlignment = .center
          self.song.textAlignment = .center
          scheduledTimerWithTimeInterval()
          self.nowplaying()



      }

I have the following ticked.

Background

here a demo of it currently https://www.youtube.com/watch?v=REtKHUR13k0

RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
  • It's Pretty simple as you need to search with keep playing with background app. take a look this answer might that solve your issue : https://stackoverflow.com/questions/30280519/how-to-play-audio-in-background-swift – Nitin Gohel Nov 25 '19 at 11:20
  • @NitinGohel I did search and did not see that - I don't understand why people believe people don't search - I been trying to get this solution for over 12 hours now. On side not I have noticed the player does not show in the lockscreen no more. – RussellHarrower Nov 25 '19 at 11:44
  • see this one may be it helps you : https://stackoverflow.com/questions/4771105/how-do-i-get-my-avplayer-to-play-while-app-is-in-background – Anbu.Karthik Nov 25 '19 at 12:23
  • @RussellHarrower i can understand your fustration but as you can see there is only 2 line of code you need to setting at app delegate on start method. that must be working. as you can see accepted answer mean that working solution. I am wonder that how that wont work for you. – Nitin Gohel Nov 25 '19 at 13:42

0 Answers0