应用音频音量



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

谢谢!

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

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];

在 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
    }

最新更新