Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

macos - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...