码迷,mamicode.com
首页 > 其他好文 > 详细

控制系统音量,自定义MPVolumeView

时间:2014-06-26 10:44:33      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:ios   mpvolumeview   custom   自定义mpvolumeview   

最近有一个需求,就是控制系统的音量,我们都知道原理在mediaPlayer.framework框架下,有方法

<span style="font-size:18px;">// 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];</span>

但实际上,在ios7以后,就被弃用了,官方的说法是希望我们用MPVolumeView来代替.那么好,现在有一个问题是,MPVolumeView很难自定义,他只能自定义一些图片,如果我们想通过手势来上下来控制音量大小怎么做呢?

<span style="font-size:18px;">MPVolumeView *volumeView = [[MPVolumeView alloc] init];
    [self.view addSubview:volumeView];
    [volumeView sizeToFit];
    NSLog(@"%@",volumeView.subviews);</span>


打印他的subViews,你会发现

bubuko.com,布布扣

有一个叫MPVolumeSlider得类,那么我们只要能控制这个类就行了,可是如果我们强制创建这个类是无法实现的,但是没关系,他的baseClass是UISlider我们可以通过这种方法实现

<span style="font-size:18px;">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];

</span>

这个时候还会发现一个问题就是,他的frame是(0,0,35,34),那么你会发现你等于的slider永远都在屏幕的左上角,这个问题我的处理方式是直接hidden = yes,然后重新在建立一个slider与他的值进行关联,然后重写touch事件,这样就可以直接通过手势来控制了.下面是完整代码


<span style="font-size:18px;">
#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;
}


</span>


源码下载:http://download.csdn.net/detail/woshiwls/7548545

控制系统音量,自定义MPVolumeView,布布扣,bubuko.com

控制系统音量,自定义MPVolumeView

标签:ios   mpvolumeview   custom   自定义mpvolumeview   

原文地址:http://blog.csdn.net/ioswyl88219/article/details/34419377

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!