控制系統音量,自己定義MPVolumeView

2021-09-08 22:44:43 字數 3783 閱讀 2429

近期有乙個需求,就是控制系統的音量,我們都知道原理在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);

但實際上,在ios7以後,就被棄用了,官方的說法是希望我們用mpvolumeview來取代.那麼好,如今有乙個問題是,mpvolumeview非常難自己定義,他僅僅能自己定義一些,假設我們想通過手勢來上下來控制音量大小怎麼做呢?

mpvolumeview *volumeview = [[mpvolumeview alloc] init];

[self.view addsubview:volumeview];

[volumeview sizetofit];

nslog(@"%@",volumeview.subviews);

列印他的subviews,你會發現

有乙個叫mpvolumeslider得類,那麼我們僅僅要能控制這個類即可了,但是假設我們強制建立這個類是無法實現的,但是沒關係,他的baseclass是uislider我們能夠通過這樣的方法實現

self.slider = [[uislider alloc]init];

self.slider.backgroundcolor = [uicolor bluecolor];

for (uicontrol *view in volumeview.subviews)

}self.slider.autoresizessubviews = no;

self.slider.autoresizingmask = uiviewautoresizingnone;

[self.view addsubview:self.slider];

這個時候還會發現乙個問題就是,他的frame是(0,0,35,34),那麼你會發現你等於的slider永遠都在螢幕的左上角,這個問題我的處理方式是直接hidden = yes,然後又一次在建立乙個slider與他的值進行關聯,然後重寫touch事件,這樣就能夠直接通過手勢來控制了.以下是完整**

#import "viewcontroller.h"

@inte***ce 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

}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

- (void)volumechange

- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event

uislider *slider = (uislider *)[self.view viewwithtag:1000];

slider.value = self.slider.value;

nslog(@"touchesbegan");

}- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event

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

C 控制系統音量

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.text using system.window...

windows api 控制系統音量

以下 針對window10支援,其他版本有待測試。當然如果通過系統鍵盤鉤子獲取vk volume down和vk volume up標識也是可以的,直接拋給windows系統自己去處理。另外需要注意筆記本鍵盤和外設hid鍵盤處理也是不一樣的。systemvolumn.h pragma once if...

iOS控制系統音量的大小

看到很多朋友在問能不能修改系統的音量,所以我也去找了一下。發現,sdk中確實有設定system volume的方法,是乙個private class,官方不允許使用,所以就算你用了,蘋果的審核也不會通過,因為標明了,那個是私有的類!還有人提到mpvolumeview,這個可以,但是這個的實現,是在螢...