是否可以使用UIButton实例更改系统卷?

时间:2012-12-24 03:48:14

标签: ios volume

我试图避免使用MPVolumeView,因为它没有为自定义提供太多余地。基本上,我想实现一个音量控制,让用户可以提前设置调用者的语音音量。这就是屏幕看起来像是:

enter image description here

我想使用UIButton创建增加和减少音量按钮,我想在IB中实现目标动作机制,如下所示:

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

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

有没有人知道是否有一个类允许我提取系统音量元素并以这种方式递增它们(基本上不必使用MPVolumeView)?

1 个答案:

答案 0 :(得分:1)

尝试这样,

1.define属性并合成到您的MPMusicPlayerController对象

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

2.使用一个浮点变量,

   float volume_control=0;

3.在viewDidLoad方法中使用NSNotification,如下所示更改音量,

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

4.define方法for handleExternalVolumeChanged:as,

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

5.现在定义音量控制按钮以增加/减少音量,

-(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;
   }
  }

希望它会帮助你......