【问题标题】:How do I tie a audio track volume slider to the physical volume buttons如何将音轨音量滑块绑定到物理音量按钮
【发布时间】:2014-09-29 02:47:44
【问题描述】:

我想将音量控制的滑块动作与音量按钮联系起来,这样如果使用按钮,滑块也会进行调整。我认为这是可能的,但我不确定。

目前我有一个可用的滑块音量控制,但它与物理按钮无关。

【问题讨论】:

    标签: iphone volume


    【解决方案1】:

    我没有测试这些,只是做了一些研究并复制粘贴。

    1a。从设备获取音量。

        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
    
    1. 将滑块设置为音量。

      self.volumeSlider.value = (double) self.volume;
      

    或者 使用苹果的 MPVolumeView

    使用音量视图向用户展示用于设置系统音频输出音量的滑块控件,以及用于选择音频输出路径的按钮。首次显示时,滑块的位置反映了当前系统音频输出音量。当用户拖动滑块时,更改会更新音量。如果用户在播放声音时按下设备音量按钮,则滑块会移动以反映新音量。 - Apple Docs

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

    编辑: 从阅读 MPMediaPlayer 文档看来,Apple 似乎希望每个人都使用自己的音量滑块并在需要时自定义其外观。

    【讨论】:

    • 这是一个非常详细和有用的答案。干得好!
    猜你喜欢
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多