2008-10-18 80 views
6

一些iPhone應用程序(例如潘多拉似乎直接操縱硬件音量並響應物理音量按鈕。這是如何完成的?如何在iPhone應用程序中設置硬件音量?

AudioSessionServices允許您使用kAudioSessionProperty_CurrentHardwareOutputVolume屬性獲取當前硬件輸出量,但它是(據稱)只讀。

回答

7

他們使用MPVolumeView,簡單地添加它,當用戶觸摸它時,它會使其餘部分停下來。注意:在iPhone Simulator中不起作用。我認爲也提到的發行說明不要直接在Interface Builder中使用它。

MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(25, 378, 270, 30)]; 
[self.view addSubview:volumeView]; 
[volumeView release]; 
+0

[MPVolumeView類參考](http://developer.apple.com /library/ios/#documentation/MediaPlayer/Reference/MPVolumeView_Class/Reference/Reference.html) – JohnK 2013-05-18 18:14:51

6

這裏是設置硬件體積並按下硬鍵後檢索的體積的另一個(完成)例如:

// AVAudiosession Delegate Method 
- (void)endInterruptionWithFlags:(NSUInteger)flags 
{ 
    // When interruption ends - set the apps audio session active again 
    [[AVAudioSession sharedInstance] setActive:YES error:nil]; 

    if(flags == AVAudioSessionInterruptionFlags_ShouldResume) { 
     // Resume playback of song here!!! 
    } 
} 

// Hardware Button Volume Callback 
void audioVolumeChangeListenerCallback (
             void      *inUserData, 
             AudioSessionPropertyID inID, 
             UInt32     inDataSize, 
             const void    *inData) 
{ 
    UISlider * volumeSlider = (__bridge UISlider *) inUserData; 
    Float32 newGain = *(Float32 *)inData; 
    [volumeSlider setValue:newGain animated:YES]; 
} 

// My UISlider Did Change Callback 
- (IBAction)volChanged:(id)sender 
{ 
    CGFloat oldVolume = [[MPMusicPlayerController applicationMusicPlayer] volume]; 
    CGFloat newVolume = ((UISlider*)sender).value; 

    // Don't change the volume EVERYTIME but in discrete steps. 
    // Performance will say "THANK YOU" 
    if(fabsf(newVolume - oldVolume) > 0.05 || newVolume == 0 || newVolume == 1 ) 
     [[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolume]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // Set the volume slider to the correct value on appearance of the view 
    volSlider.value = [[MPMusicPlayerController applicationMusicPlayer] volume]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

    // Activate the session and set teh delegate 
    [[AVAudioSession sharedInstance] setActive:YES error:nil]; 
    [[AVAudioSession sharedInstance] setDelegate:self]; 

    // Create a customizable slider and add it to the view 
    volSlider = [[UISlider alloc] init]; 
    CGRect sliderRect = volSlider.frame; 
    sliderRect.origin.y = 50; 
    sliderRect.size.width = self.view.bounds.size.width; 
    volSlider.frame = sliderRect; 
    [volSlider addTarget:self action:@selector(volChanged:) forControlEvents:UIControlEventValueChanged]; 
    [self.view addSubview:volSlider]; 

    // Regoister the callback to receive notifications from the hardware buttons 
    AudioSessionAddPropertyListener (
                   kAudioSessionProperty_CurrentHardwareOutputVolume , 
                   audioVolumeChangeListenerCallback, 
                   (__bridge void*)volSlider 
                   ); 

    [...] 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

    // Remove the Hardware-Button-Listener 
    AudioSessionRemovePropertyListenerWithUserData(
      kAudioSessionProperty_CurrentHardwareOutputVolume, 
      audioVolumeChangeListenerCallback, 
      (__bridge void*)volSlider); 
} 
+0

真棒回答:-) – Flori 2014-10-09 09:26:31

相關問題