Mika Schiller
Mika Schiller

Reputation: 425

Is it possible to change system volume with UIButton instances?

I"m trying to avoid MPVolumeView because it doesn't provide much leeway for customization. Basically, I want to implement a volume control that lets the user set the voice volume of a caller ahead of time. This is what that screen is going to look like:

enter image description here

I want to use UIButton to create the increase and decrease volume buttons and I'm thinking of implementing a target action mechanism in IB like this:

- (IBAction)increaseVolume:(id)sender
{
    [someVolumeObject setVolume:[systemVolume volume] + 1];
}

- (IBAction)decreaseVolume:(id)sender
{
    [someVolumeObject setVolume:[systemVolume volume] - 1];
}

Does anyone know if there's a class out there that will allow me to extract system volume elements and increment them this way(basically without having to use MPVolumeView)?

Upvotes: 0

Views: 470

Answers (1)

iSpark
iSpark

Reputation: 952

try like this,

1.define property and synthesize to your MPMusicPlayerController object,

@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
@synthesize musicPlayer;

2.take one float variable as,

   float volume_control=0;

3.use NSNotification in your viewDidLoad method as below to change the volume,

self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self 
                       selector:@selector(handleExternalVolumeChanged:)
                           name:MPMusicPlayerControllerVolumeDidChangeNotification 
                         object:self.musicPlayer];
[self.musicPlayer beginGeneratingPlaybackNotifications];

4.define Method for handleExternalVolumeChanged: as,

- (void)handleExternalVolumeChanged:(id)notification {
  volume_control = self.musicPlayer.volume;
}

5.Now define your volume Control buttons to increase/decrease Volume,

-(IBAction)decrease_volume:(id)sender{
  if (volume_control>0) {
      self.musicPlayer.volume =self.musicPlayer.volume-0.1;  
      volume_control-=0.1;
      }
   }
 -(IBAction)increase_volume:(id)sender{
    if (volume_control<1) {
    self.musicPlayer.volume =self.musicPlayer.volume+0.1;  
    volume_control+=0.1;
   }
  }

hope it will helps you..

Upvotes: 1

Related Questions