Control system volume and customize MPVolumeView

Source: Internet
Author: User
Tags uicontrol

One requirement recently is to control the volume of the system. We all know that the principle is in the mediaPlayer. framework.

// The current volume of playing music, in the range of 0.0 to 1.0.// This property is deprecated -- use MPVolumeView for volume control instead.@property(nonatomic) float volume NS_DEPRECATED_IOS(3_0, 7_0);[[MPMusicPlayerController applicationMusicPlayer] setVolume:self.volumeSlider.value];

But in fact, it was abandoned after ios7. The official saying is that we want to replace MPVolumeView. well, now there is a problem: MPVolumeView is difficult to customize. He can only customize some images. If we want to use gestures to control the volume up and down, how can we do it?

MPVolumeView *volumeView = [[MPVolumeView alloc] init];    [self.view addSubview:volumeView];    [volumeView sizeToFit];    NSLog(@"%@",volumeView.subviews);


Print his subViews and you will find


There is a class called MPVolumeSlider, so we only need to be able to control this class, but it cannot be implemented if we force create this class, but it doesn't matter, its baseClass is UISlider. we can implement it using this method.

self.slider = [[UISlider alloc]init];    self.slider.backgroundColor = [UIColor blueColor];    for (UIControl *view in volumeView.subviews) {        if ([view.superclass isSubclassOfClass:[UISlider class]]) {            NSLog(@"1");            self.slider = (UISlider *)view;        }    }    self.slider.autoresizesSubviews = NO;    self.slider.autoresizingMask = UIViewAutoresizingNone;    [self.view addSubview:self.slider];

At this time, you will also find that the frame is (0, 0, 35, 34), then you will find that your equal slider is always in the upper left corner of the screen, my solution to this problem is to directly hidden = yes, and then re-establish a slider to associate with its value, and then rewrite the touch event, so that you can directly control it through gestures. the complete code is as follows:


#import "ViewController.h"@interface ViewController ()@property (nonatomic,strong)UISlider *volumeSlider;@property (nonatomic,strong)UISlider *slider;@property (nonatomic,assign)CGPoint firstPoint;@property (nonatomic,assign)CGPoint secondPoint;@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.            MPVolumeView *volumeView = [[MPVolumeView alloc] init];    [self.view addSubview:volumeView];    [volumeView sizeToFit];    NSLog(@"%@",volumeView.subviews);        self.slider = [[UISlider alloc]init];    self.slider.backgroundColor = [UIColor blueColor];    for (UIControl *view in volumeView.subviews) {        if ([view.superclass isSubclassOfClass:[UISlider class]]) {            NSLog(@"1");            self.slider = (UISlider *)view;        }    }    self.slider.autoresizesSubviews = NO;    self.slider.autoresizingMask = UIViewAutoresizingNone;    [self.view addSubview:self.slider];    self.slider.hidden = YES;    NSLog(@"%f",self.slider.value);//    UISlider *slider =    //    self.volumeSlider = [[UISlider alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];//    self.volumeSlider.backgroundColor = [UIColor yellowColor];//    self.volumeSlider.minimumValue = 0.0;//    self.volumeSlider.maximumValue = 1.0;//    self.volumeSlider.continuous = YES;//    [self.volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];//    [self.view addSubview:self.volumeSlider];    UISlider *slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0, 100, 200, 20)];    slider1.tag = 1000;    slider1.minimumValue = self.slider.minimumValue;    slider1.maximumValue = self.slider.maximumValue;    slider1.value = self.slider.value;    [slider1 addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];    [self.view addSubview:slider1];}- (void)updateValue:(UISlider *)slider{    self.slider.value = slider.value;}- (void)volumeChange{    [[MPMusicPlayerController applicationMusicPlayer] setVolume:self.volumeSlider.value];    }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    for(UITouch *touch in event.allTouches) {                self.firstPoint = [touch locationInView:self.view];    }        UISlider *slider = (UISlider *)[self.view viewWithTag:1000];    slider.value = self.slider.value;    NSLog(@"touchesBegan");}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    for(UITouch *touch in event.allTouches) {                self.secondPoint = [touch locationInView:self.view];            }    NSLog(@"firstPoint==%f || secondPoint===%f",self.firstPoint.y,self.secondPoint.y);    NSLog(@"first-second==%f",self.firstPoint.y - self.secondPoint.y);       self.slider.value += (self.firstPoint.y - self.secondPoint.y)/500.0;        UISlider *slider = (UISlider *)[self.view viewWithTag:1000];    slider.value = self.slider.value;    NSLog(@"value == %f",self.slider.value);    self.firstPoint = self.secondPoint;}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"touchesEnded");    self.firstPoint = self.secondPoint = CGPointZero;}


Source code download: http://download.csdn.net/detail/woshiwls/7548545

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.