Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control iPod from another app?

I took a look at Apple's AddMusic sample code, but that's not quite what I want. I'm trying to find a way to start/stop music playing in the iPod app, not controlling what's contained in my app's sandbox.

Something like this...

 ____________
|            |
| |<  ||  >| |    skip back, play/pause, skip forward
|            |
|  XXXXXXXX  |    album art
|  XXXXXXXX  |
|  XXXXXXXX  |
|  XXXXXXXX  |
|            |
|____________|

Of course the app is more complex than this, but for now, I want to focus on assuming direct control.
Bonus points to anyone who gets the reference without cheating.


— Artwork loading error moved to a new question.

— Text-to-Speech implementation moved to a new question.

— Progress bar fills to 100% as soon as the song starts. This code works:

// your must register for notifications to use them
- (void)handleNowPlayingItemChanged:(id)notification {
    …
    // this is what I forgot to do
    NSNumber *duration = [item valueForProperty:MPMediaItemPropertyPlaybackDuration];
    float totalTime = [duration floatValue];
    progressSlider.maximumValue = totalTime;
…
}

// called on a one-second repeating timer
- (void)updateSlider {
    progressSlider.value = musicPlayer.currentPlaybackTime;
    [progressSlider setValue:musicPlayer.currentPlaybackTime animated:YES];
}
like image 758
Thromordyn Avatar asked Jun 13 '11 14:06

Thromordyn


People also ask

Can you remotely control an iOS device?

Yes, it is absolutely possible (and easy) to access an iPad remotely by using Switch Control or a trusted 3rd-party application. If users opt for the 3rd-party method, then both devices participating in the remote connection must have the app installed.

Can I control Apple music from another device?

iTunes Remote is the best way to control Apple Music, iTunes, or the Apple TV app from anywhere in your home. Simply download the app to your iPhone or iPad, and connect directly to Apple Music, iTunes, or the Apple TV app on your Mac or PC.

Can you remotely control an iPad with an iPhone?

How to Control an iPad Remotely with an iPhone, iPod, or iPad. With the arrival of iOS 10, iPads received a function called Switch Control. This allows a user to take over the target iPad remotely with another device. Note that both devices must be connected to the same network and Apple ID account.


1 Answers

The MediaPlayer framework is what you are looking for. There is a useful guide which also contains a link to the MediaPlayer API overview.

Specifically, you're looking for MPMusicPlayerController. Your app can control either a dedicated app media player or the iPod media player which appears to be what you want.

A simple example would be something like (header omitted):

@implementation MyPlaybackController

- (id)initWithNibName:(NSString *)nibName bundle:(id)bundleOrNil 
{
  if ((self = [super initWithNibName:nibName bundle:bundleOrNil])) {
    self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
  }  
  return self;
}

- (IBAction)skipForward:(id)sender
{
  [self.musicPlayer skipToNextItem];
}

- (IBAction)skipBack:(id)sender
{
  [self.musicPlayer skipToPreviousItem];
}

- (IBAction)togglePlayback:(id)sender
{
  if (self.musicPlayer.playbackState == MPMusicPlaybackStatePlaying) {
    [self.musicPlayer pause];
  } else {
    [self.musicPlayer play];
  }
}

@end

Do take time to read the documentations and take note of the caveats mentioned with regards to Home Sharing. You'll also want to register for notifications in changes to the player state that can occur from outside your control.

like image 110
Luke Redpath Avatar answered Oct 01 '22 05:10

Luke Redpath