0

I am making my Apple CarPlay app, and my list items need to watch and see if the media player is playing or not.

As I need to show the spinning wheel until such time that the media player starts playing.

I know I need to access MPRemoteCommandCenter and do something like this:

 let commandCenter = MPRemoteCommandCenter.shared()

    // Add handler for Play Command
    commandCenter.playCommand.addTarget { [unowned self] event in
        if self.player?.rate == 0.0 {
            self.player?.play()
            return .success
        }
        return .commandFailed
    }

    // Add handler for Pause Command
    commandCenter.pauseCommand.addTarget { [unowned self] event in
        if self.player?.rate == 1.0 {
            self.player?.pause()
            return .success
        }
        return .commandFailed
    }

However since I already have the media player (mediaplayer.swift) I thought I would be able to watch it.

I tried to do an if statement however that did not work.

if MusicPlayer.shared.player?.rate == 1.0 {
completion()
}

However it does not wait until the media player is playing to run this, instead it checks it and if at the time of the check it is playing it will complete otherwise it will just keep spinning.

I know there is a way to do this. My thinking is I'll need to make a func that watching the media player?

halfer
  • 19,824
  • 17
  • 99
  • 186
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
  • Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer May 14 '21 at 01:06
  • @halfer sorry for being polite. – RussellHarrower May 14 '21 at 02:37
  • Please read [Being polite while asking a question?](https://meta.stackoverflow.com/questions/266525/being-polite-while-asking-a-question) – halfer May 14 '21 at 07:31
  • Please read [Should 'Hi', 'thanks', taglines, and salutations be removed from posts?](https://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) – halfer May 14 '21 at 07:31
  • Please read [What's wrong with using polite language in questions?](https://meta.stackexchange.com/questions/216602/whats-wrong-with-using-polite-language-in-questions) – halfer May 14 '21 at 07:32
  • Please read [Should I remove 'fluff' when editing questions?](https://meta.stackoverflow.com/questions/260776/should-i-remove-fluff-when-editing-questions) (This is the canonical version. There are plenty more similar posts in the Related sidebar). – halfer May 14 '21 at 07:32
  • I think we have had this conversation before, but I've added various guideline posts from Meta Stack Overflow and Meta Stack Exchange. If this is genuinely the first time you've encountered this, then please aim for brevity going forward. Posts here can be thought of as like documentation or Wikipedia articles - and it would not be normal to add thanks, greetings, and conversational material to those. – halfer May 14 '21 at 07:34

0 Answers0