How do you set the hardware volume in an iPhone app?

扶醉桌前 提交于 2019-11-28 23:25:46

问题


Some iPhone applications, such as Pandora seem to directly manipulate the hardware volume and respond to physical volume button. How is this done?

AudioSessionServices allows you to get the current hardware output volume with the kAudioSessionProperty_CurrentHardwareOutputVolume property, but it is (allegedly) read-only.


回答1:


They use the MPVolumeView, simple add it and it's makes the rest when the user touch it. Note: Doesn't work in iPhone Simulator. I think the release note also mentioned do not use it direct in Interface Builder.

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



回答2:


Here is another (complete) example of setting the hardware volume AND retrieving the volume after pressing the hardware keys:

// 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);
}


来源:https://stackoverflow.com/questions/215524/how-do-you-set-the-hardware-volume-in-an-iphone-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!