2014-09-29 38 views

回答

2

我没有测试过这些,只是做了一些研究和复制和粘贴。 1)。从设备获取音量。

float volume = [[AVAudioSession sharedInstance] outputVolume]; 
    self.volume = vol; 

#import <MediaPlayer/MediaPlayer.h> 
    //... 
    float vol = [MPMusicPlayerController iPodMusicPlayer].volume; 
    self.volume = vol; 

1b中。当体积变化得到通知

[[NSNotificationCenter defaultCenter] 
     addObserver:self 
     selector:@selector(volumeChanged:) 
     name:@"AVSystemController_SystemVolumeDidChangeNotification" 
     object:nil]; 

    -(void)volumeChanged:(NSNotification*)notification{ 
    float vol = [[[notification userInfo] 
       objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] 
       floatValue]; 
    self.volume = vol; 
    } 
    // Some said the Notification used is a private apple API so you should do a bit of research for using it in a app you are submitting to apple. 
    // There is also this notification though: MPMusicPlayerControllerVolumeDidChangeNotification 
  • 设置滑块音量。

    self.volumeSlider.value = (double) self.volume; 
    
  • 或从Apple 使用MPVolumeView

    使用体积视图与滑块控制用于设置系统音频输出音量,并用于选择所述音频输出的按钮向用户呈现路线。第一次显示时,滑块的位置反映当前系统音频输出音量。当用户拖动滑块时,更改会更新音量。如果用户在播放声音时按下设备音量按钮,则滑块会移动以反映新音量。 - Apple Docs

     mpVolumeViewParentView.backgroundColor = [UIColor clearColor]; 
        MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds]; 
        [mpVolumeViewParentView addSubview: myVolumeView]; 
    

    编辑: 从阅读MPMediaPlayer文档它看起来好像苹果希望每个人都用自己的音量滑块,并根据需要定制它的外观。

    +0

    这是一个非常详细和有用的答案。做得好! – Andrew 2014-09-29 04:01:12