CD Insertion with Cocoa

I had added code to juce_mac_Messaging.cpp back in 1.45 to handle CD insertion/ejection notification using Carbon Events (kEventClassVolume/kEventVolumeMounted and friends)

Now that I’m on the tip, does anyone know of a more Cocoa-friendly way to do this?

Look into NSWorkspace and NSFileManager there should be methods there for it I ll get back to you if I find one.

There is another thing you can do, have a thread looking for any drives being mounted into “/volumes” location and call juce “isOnCDRomDrive ()”.

If the thread detects anything new you will can always callback. But this would not be the right way to do it.

These are some of the api you can use to detect mounting and unmounting of drives or disks

– mountedRemovableMedia
– mountNewRemovableMedia
– mountedLocalVolumePaths
– checkForRemovableMedia

please go through this link

http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html

I haven’t used them before so you will have to check how to use these calls

Thanks!

Not tested but…

[code]@interface JDVolumeWatcher : NSObject
{
}

  • (id)init;
  • (void)dealloc;
  • (void)volumeMountedNotification:(NSNotification*)note;
  • (void)volumeUnMountedNotification:(NSNotification*)note;
    @end

@implementation JDVolumeWatcher

  • (id)init
    {
    if (![super init])
    return nil;

    [[[NSWorkspace sharedWorkspace]notificationCenter]addObserver:self selector:@selector(volumeMountedNotification:) name:NSWorkspaceDidMountNotification object:nil];
    [[[NSWorkspace sharedWorkspace]notificationCenter]addObserver:self selector:@selector(volumeUnMountedNotification:) name:NSWorkspaceDidUnmountNotification object:nil];
    return self;
    }

  • (void)dealloc
    {
    [[[NSWorkspace sharedWorkspace]notificationCenter]removeObserver:self];
    [super dealloc];
    }

  • (void)volumeMountedNotification:(NSNotification*)note
    {
    NSLog(@“Volume Mounted:”);
    }

  • (void)volumeUnMountedNotification:(NSNotification*)note
    {
    NSLog(@“Volume UnMounted:”) ;
    }
    @end[/code]

In your constructor

volumeWatcher=[[JDVolumeWatcher alloc]init];

In the destructor

[volumeWatcher release];

And to get the volume mounted…

  • (void)volumeMountedNotification:(NSNotification*)note
    {

NSLog([[note userInfo]objectForKey:@“NSDevicePath”]);
}