Control iTunes from a cocoa application

后端 未结 3 1142
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 22:40

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 itu

3条回答
  •  一整个雨季
    2020-12-08 23:40

    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.

提交回复
热议问题