0

我需要用户控制设备的媒体音量。我见过许多使用音量视图或 AVAudioPlayer 的解决方案,但我想使用设备音量按钮来设置应用程序音量,就像许多应用程序一样。

谢谢!

4

2 回答 2

0

要使用此功能,您需要将音频会话设置为播放。这是通过以下方式完成的:

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
于 2012-04-16T16:33:12.173 回答
0

倾倒在 appdelegate 中

  - (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(volumeChanged:)
         name:@"AVSystemController_SystemVolumeDidChangeNotification"
         object:nil];
    }

    - (void)volumeChanged:(NSNotification *)notification
    {
        float volume =
        [[[notification userInfo]
          objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
         floatValue];

        // Do stuff with volume
    }
于 2015-10-14T13:06:55.497 回答