iOS 設置系統音量和監聽系統音量變化


很簡單的調用 

首先在工程引入MediaPlayer.framework

#import <MediaPlayer/MediaPlayer.h>

1. 獲取系統音量

// 獲取系統音量
MPVolumeView *volumeView = [[MPVolumeView alloc] init];

UISlider *volumeViewSlider= nil;

    for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider
= (UISlider*)view;
break; } } float systemVolume = volumeViewSlider.value;

 

2.監聽方法

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
- (void)volumeChanged:(NSNotification *)notification
{
     [notification.userInfo[@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];

}

 

 3.記得銷毀哦

- (void)dealloc
{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM