Search code examples
iosobjective-cjailbreakiphone-privateapi

Check if iOS is playing music ("Is button in control center Pause?")


I want to check in my app if the user is playing any music. Basically I would like to know if the button in the control center shows pause instead of play (since then the phone is playing some music). I don't want to use the solution from here since that is also true when on a call for example.


Solution

  • Found two ways, but for both the Springboard is required. Since apps and tools are in a sandbox, these solutions can only be used in a Tweak that hooks into Springboard!

    First:

    @interface SBMediaController : NSObject
    + (id)sharedInstance;
    - (BOOL)isPlaying;
    @end
    
    bool isMusicPlaying = [[%c(SBMediaController) sharedInstance] isPlaying]; // this is always false if not hooked into Springboard!
    

    Second (this solution is asynchronous):

    #import <MediaRemote/MediaRemote.h> // also add MediaRemote to your XXX_PRIVATE_FRAMEWORKS
    
    MRMediaRemoteGetNowPlayingInfo(dispatch_get_main_queue(), ^(CFDictionaryRef information) {
      NSDictionary *dict=(__bridge NSDictionary *)(information);
      if( dict != NULL && [dict objectForKey:(__bridge NSString *)kMRMediaRemoteNowPlayingInfoPlaybackRate] != NULL ){
        float rate = [[dict objectForKey:(__bridge NSString *)kMRMediaRemoteNowPlayingInfoPlaybackRate] floatValue];
        NSLog(@"playbackRate %f", rate);
        bool isMusicPlaying = rate > 0.0;
      }
    });