Re: Help on controlling system volume
Re: Help on controlling system volume
- Subject: Re: Help on controlling system volume
- From: Arnab Ganguly <email@hidden>
- Date: Fri, 5 Jun 2009 19:03:27 +0530
Hi All,
Thanks to all for help.I got the solution anybody facing the same
problem can also use the below function for controlling system sound
from NSSlider.
1)Step 1 need to have
a)cocoa, coreaudio , coredata foundation, Appkit, Foundation frameworks included in the proj.
2) Step 2
Method for getting the volume
#include <CoreAudio/AudioHardware.h>
#import <CoreAudio/CoreAudio.h>
- (float) getVolume
{
float b_vol;
OSStatus err;
AudioDeviceID device;
UInt32 size;
UInt32 channels[2];
float volume[2];
// get device
size = sizeof device;
err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device);
if(err!=noErr)
{
NSLog(@"audio-volume error get device");
return 0.0;
}
// try set master volume (channel 0)
size = sizeof b_vol;
err = AudioDeviceGetProperty(device, 0, 0,
kAudioDevicePropertyVolumeScalar, &size, &b_vol);
//kAudioDevicePropertyVolumeScalarToDecibels
if(noErr==err) return b_vol;
// otherwise, try seperate channels
// get channel numbers
size = sizeof(channels);
err = AudioDeviceGetProperty(device, 0, 0,kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
if(err!=noErr) NSLog(@"error getting channel-numbers");
size = sizeof(float);
err = AudioDeviceGetProperty(device, channels[0], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[0]);
if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[0]);
err = AudioDeviceGetProperty(device, channels[1], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[1]);
if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[1]);
b_vol = (volume[0]+volume[1])/2.00;
return b_vol;
}
Method for setting the volume
- (void)setVolume:(float)involume {
OSStatus err;
AudioDeviceID device;
UInt32 size;
Boolean canset = false;
UInt32 channels[2];
//float volume[2];
// get default device
size = sizeof device;
printf("setVolume:: value of the volume set =%lf", involume);
err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device);
if(err!=noErr) {
NSLog(@"audio-volume error get device");
return;
}
// try set master-channel (0) volume
size = sizeof canset;
err = AudioDeviceGetPropertyInfo(device, 0, false, kAudioDevicePropertyVolumeScalar, &size, &canset);
if(err==noErr && canset==true) {
size = sizeof involume;
err = AudioDeviceSetProperty(device, NULL, 0, false, kAudioDevicePropertyVolumeScalar, size, &involume);
return;
}
// else, try seperate channes
// get channels
size = sizeof(channels);
err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
if(err!=noErr) {
NSLog(@"error getting channel-numbers");
return;
}
// set volume
size = sizeof(float);
err = AudioDeviceSetProperty(device, 0, channels[0], false, kAudioDevicePropertyVolumeScalar, size, &involume);
if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[0]);
err = AudioDeviceSetProperty(device, 0, channels[1], false, kAudioDevicePropertyVolumeScalar, size, &involume);
if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[1]);
}
3) Step 3
NSSlider function
-(IBAction) slide:(id)sender
{
float sliderValue = [ slider floatValue ];
printf("slide:: The Value of the Volume before setting =%lf\n",[self getVolume]);
sliderValue = sliderValue/100;
[self setVolume : sliderValue ];
printf("slide:: The Value of the Volume after setting =%lf\n",[self getVolume]);
}
all set to have the volume control through the app.
Thanks
Arnab
On Thu, Jun 4, 2009 at 5:13 PM, Hamish Allan
<email@hidden> wrote:
On Thu, Jun 4, 2009 at 12:29 PM, Arnab Ganguly <
email@hidden> wrote:
> Apologize if it is not in correct grp. How do I control the volume of the
> system.Like increasing,reducing and muting the system volume.I am using
> NSSlider in the nib file.So when I pull the slider would like the system
> volume to be controlled.
This group is for questions about the toolchain used for development
on Mac OS X and iPhone, rather than the APIs.
As chance would have it, this topic came up recently on the
CoreAudio-API list, so check the archives for May.
Best wishes,
Hamish
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden