Saeed Rahmatolahi
Saeed Rahmatolahi

Reputation: 1338

how to put an image for every music that playing in lock screen in swift 3?

I have a player that plays music in swift 3 and want to showing image in lock screen in every songs I searched stack overflow But the codes doesn't helped me please tell me some thing simple because I don't want to use different images ! I just want to show one Image when the app is playing Song in the lock screen so here is my codes

var player : AVAudioPlayer = AVAudioPlayer()
let audioPath = Bundle.main.path(forResource: "Torpedo", ofType: "mp3")
            try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)

        }catch {

            //Error

        }


        let session = AVAudioSession.sharedInstance()
        do {

            try session.setCategory(AVAudioSessionCategoryPlayback)

        }

        catch {



        }
  player.play()

and here is the codes that I used But didn't worked

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyArtist : AudioCenter.sharedInstnce.currentReciter().name,
                                                                 MPMediaItemPropertyTitle : AudioCenter.sharedInstnce.currentSurah()!.name,
                                                                 MPMediaItemPropertyArtwork:MPMediaItemArtwork(image: UIImage(named: "Logo")!)]

Upvotes: 0

Views: 1790

Answers (1)

Ellen
Ellen

Reputation: 5190

set Session playback category for Audio session where you will be updating MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo

       try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])
       try! AVAudioSession.sharedInstance().setActive(true)

EDIT1

Add in viewDidLoad of your VC :

self.becomeFirstResponder()
UIApplication.shared.beginReceivingRemoteControlEvents()

and add 1 more method if it is not there in your VC

 override var canBecomeFirstResponder: Bool { return true }

EDIT2
Try with

   try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.mixWithOthers)

instead of

   try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])

Upvotes: 1

Related Questions