Re: Help on adding volume control
Re: Help on adding volume control
- Subject: Re: Help on adding volume control
- From: Arnab Ganguly <email@hidden>
- Date: Fri, 5 Jun 2009 19:02:26 +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 Fri, Jun 5, 2009 at 12:53 AM, Kyle Sluder
<email@hidden> wrote:
On Thu, Jun 4, 2009 at 6:30 AM, Arnab Ganguly <
email@hidden> wrote:
> I have very less time.Yes I have started looking into.Would be very happy if
> somebody can help.
Lack of planning on your part does not constitute an emergency on our part.
--Kyle Sluder
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden