Le 16 avr. 08 à 09:23, Antonio Nunes a écrit :

Hi,

I've put together a simple wrapper to easily change the system sound volume using Objective-C, obviating the need to deal directly with lower-level CoreAudio calls.

The files are available here: 
http://sintraworks.com/media/code/ANSystemSoundWrapper.zip

The wrapper consists of a single class that implements three class methods to affect the system sound volume level, and a single class method to get the current level:
+ (float)getSystemVolume;
+ (void)setSystemVolume:(float)inVolume;
+ (void)increaseSystemVolumeBy:(float)amount;
+ (void)decreaseSystemVolumeBy:(float)amount;

To use it you need to link against the CoreAudio framework and include the header file in any implementation file where you want to call any of the above methods. The code is distributed under the MIT license, so you can do pretty much anything you want with it. Any comments on the code always appreciated.

The sample below uses a timer to trigger periodical changes to the sound volume, and will play a sound to feed the new volume back, as long as there is an actual change in level (i.e. until the max or min sound level has been reached, either 0 or 1):

- (void)increaseSystemVolume:(NSTimer*)timer
{
        float oldVol = [ANSystemSoundWrapper getSystemVolume];
        [ANSystemSoundWrapper increaseSystemVolumeBy:.05];      
        if (oldVol != [ANSystemSoundWrapper getSystemVolume]) {
                [[NSSound soundNamed:@"Tink"] play];
        }
}

- (void)decreaseSystemVolume:(NSTimer*)timer
{
        float oldVol = [ANSystemSoundWrapper getSystemVolume];
        [ANSystemSoundWrapper decreaseSystemVolumeBy:.05];      
        if (oldVol != [ANSystemSoundWrapper getSystemVolume]) {
                [[NSSound soundNamed:@"Tink"] play];
        }
}

-António

I don't want to start another "Code design" war, but just wonder if doing "Utility Class" is a common practice in obj-c. Unlike Java or other object oriented language, obj-c is a superset of C and support simple functions. Wouldn't it be simpler to declare 4 functions instead of 4 class methods ?

float ANGetSystemVolume();
void ANSetSystemVolume:(float volume);
..


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to