0

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")!)]
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60

1 Answers1

1

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: [])
Ellen
  • 5,180
  • 1
  • 12
  • 16
  • I can't see any image codes here can you explain more please? – Saeed Rahmatolahi Jul 20 '17 at 18:34
  • where are you updating MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo in code? becuase this is the only way to add image in lock screen. Now you've to check why it is not working in your case? is it same controller which is having all the avaudioplayer code? – Ellen Jul 20 '17 at 18:37
  • I Just have one view controller if it was your question- I used your codes here and the app doesn't let me to run because of if line error and the error is : argument labels error do not match any available overloads – Saeed Rahmatolahi Jul 20 '17 at 18:40
  • ok Thanks It will work But I how can I add image and can I hide controller of the player in lock screen or not? – Saeed Rahmatolahi Jul 20 '17 at 19:02
  • you can use same code as mentioned above to set image on lock screen MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyArtist : AudioCenter.sharedInstnce.currentReciter().name, MPMediaItemPropertyTitle : AudioCenter.sharedInstnce.currentSurah()!.name, MPMediaItemPropertyArtwork:MPMediaItemArtwork(image: UIImage(named: "Logo")!)] – Ellen Jul 20 '17 at 19:04
  • thanks a lot it worked for me can I hide audio controllers in lock screen or not? I just want to show image in the lock screen – Saeed Rahmatolahi Jul 20 '17 at 19:09
  • Check Edit2 . It might result into disappear your controls even after reset.This problem is going on ..Disabling controls can be safer option. – Ellen Jul 20 '17 at 19:27
  • I used Edited 2 codes But when I used that the picture will disappear too :) – Saeed Rahmatolahi Jul 20 '17 at 19:29