Raz
Raz

Reputation: 1932

Control iTunes from a cocoa application

I am developing a mac application that involves audio playback. I would like to pause other audio players when our playback starts.

how can I
1) detect that itunes is running
2) detect that itunes is currently playing
3) pause itunes
4) resume itunes when I am done

also:
5) Is the a way to pause other types of media playback as well? audio and video in the in the browser for example?

Upvotes: 13

Views: 5021

Answers (3)

MKroehnert
MKroehnert

Reputation: 3737

You can do that with the following code which is using ScriptingBridge:

#import "iTunes.h"
#import "Cocoa/Cocoa.h"

int main()
{
  iTunesApplication* iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];

  // check if iTunes is running (Q1)
  if ([iTunes isRunning])
  {
    // pause iTunes if it is currently playing (Q2 and Q3)
    if (iTunesEPlSPlaying == [iTunes playerState])
      [iTunes playpause];

    // do your stuff

    // start playing again (Q4)
    [iTunes playpause];
  }
  return 0;
}

The file iTunes.h is generated by running sdef /Applications/iTunes.app | sdp -fh --basename iTunes from a commandline. The error unknown type name "tdta" can be ignored.

You also need to add ScriptingBridge.framework to the linked frameworks.

Here is also a link to the ScriptingBridge documentation.

Upvotes: 18

Peter Hosey
Peter Hosey

Reputation: 96323

1) detect that itunes is running

NSRunningApplication or Process Manager.

2) detect that itunes is currently playing
3) pause itunes
4) resume itunes when I am done

Scripting Bridge.

5) Is the a way to pause other types of media playback as well?

That depends on the application.

audio and video in the in the browser for example?

No.

Upvotes: 5

Eric Brotto
Eric Brotto

Reputation: 54251

Maybe creating an AppleScript can solve the problem?

Check this out:

http://dougscripts.com/itunes/

or some command line scripts?:

http://www.macosxhints.com/article.php?story=20011108211802830

Upvotes: 2

Related Questions