最近在做關于視頻方面的東西,然后參考的播放界面是bilibili,這里遇到的坑和解決的辦法記錄一下
1. MPVolumeView的隱藏和音量顯示的自定義化
我看了一些直播app和一些關于播放視頻的app,在屏幕的右邊拖動的時候都是調用的原生的音量HUD的顯示,但是我們UI參考的是bilibili播放界面,音量HUD的顯示就是要自定義,結果我又是接手的二手代碼,妮瑪,那個坑啊,代碼亂就不說了,還妮瑪沒有注釋,悲催,下面就介紹一下怎么去隱藏的系統音量鍵的HUD
第一個思路用的是MPVolumeView
在vc中懶加載MPVolumeView 通過kvo獲取到音量鍵的UISlider
具體的代碼如下
// oc代碼
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero];
for (UIView* newView in volumeView.subviews) {
if ([newView.class.description isEqualToString:@"MPVolumeSlider"]){
self.volumeSlider = (UISlider*)newView;
break;
}
}
//swift 代碼
lazy var volumeView:MPVolumeView = {
let volumeView:MPVolumeView = MPVolumeView.init()
for view in volumeView.subviews {
if (NSStringFromClass(view.classForCoder) == "MPVolumeSlider"){
actionView.volumeViewSlider = (view as! UISlider)
break
}
}
volumeView.frame = CGRect(x: -1000, y: -1000, width: 100, height: 100)
return volumeView
}()
注意:
1.frame值設置成Zero或者只要不在可視化界面上面展示都可以
- MPVolumeView的Hidden不能設置成NO,不然也會在界面上面展示出系統的HUD
然后在MPVolumeView上面設置一層不透明的視圖,就可以讓MPVolumeView隱藏掉或者是將MPVolumeView至于視圖的最底層
第二個思路用的是MPMusicPlayerController
獲取到系統的音量大小的方法
//oc代碼
self.startVB = [AVAudioSession sharedInstance].outputVolume
//swift代碼
self.startVB = CGFloat(AVAudioSession.sharedInstance().outputVolume)
然后在給系統的音量賦值,但是在iOS7之后蘋果就是廢除了這個方法,但是oc還能用,swift就不能用了,沒有這個屬性
//oc代碼
[[MPMusicPlayerController applicationMusicPlayer]setVolume:value];
如果有知道在swift上面可以用這個屬性的請留言給我說一下
2.音量鍵的監聽
當上上面是通過上下滑動屏幕的方法來改變音量的大小
下面就是通過設備硬件也就是音量鍵來改變音量大小的方式
直接上代碼,沒有什么好說的
//oc代碼
//監聽
NSError *error
[[AVAudioSession sharedInstance] setActive:YES error:&error]
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//監聽方法
- (void) volumeChanged:(NSNotification *)notification{
NSDictionary *userInfo = notification.userInfo;
NSString *reasonStr = userInfo[@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"];
if ([reasonStr isEqualToString:@"ExplicitVolumeChange"]){
self.actionView.brightnessSlider.value = userInfo[@"AVSystemController_AudioVolumeNotificationParameter"] ;
}
}
//移除
- (void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
}
//swift代碼
//監聽
do{
try AVAudioSession.sharedInstance().setActive(true)
}catch let error as NSError{
print("\(error)")
}
NotificationCenter.default.addObserver(self, selector: #selector(self.changeVolumSlider(notifi:)), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
UIApplication.shared.beginReceivingRemoteControlEvents()
//監聽方法
@objc private func changeVolumSlider(notifi:NSNotification){
self.actionView.brightnessImageView.image = UIImage.init(named: "C_general_voice")
self.actionView.brightnessSlider.value = AVAudioSession.sharedInstance().outputVolume
let userInfo:NSDictionary = notifi.userInfo! as NSDictionary
if ((userInfo["AVSystemController_AudioVolumeChangeReasonNotificationParameter"] as! String) == "ExplicitVolumeChange"){
print("userInfo----\(userInfo)----->\(AVAudioSession.sharedInstance().outputVolume)")
self.actionView.brightnessView.isHidden = false
self.actionView.brightnessSlider.value = (userInfo["AVSystemController_AudioVolumeNotificationParameter"] as! Float)
DispatchQueue.main.asyncAfter(deadline: .now()+1.5, execute:
{
self.actionView.brightnessView.isHidden = true
})
}
}
//移除
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
UIApplication.shared.endReceivingRemoteControlEvents()
}
如果oc的代碼單詞有錯的話請見諒,畢竟是手動敲出來的