185 lines
7.2 KiB
Swift
185 lines
7.2 KiB
Swift
//
|
||
// MPPositive_BottomShowView.swift
|
||
// MusicPlayer
|
||
//
|
||
// Created by Mr.Zhou on 2024/4/19.
|
||
//
|
||
|
||
import UIKit
|
||
import Kingfisher
|
||
///b面底部播放音乐展示View
|
||
class MPPositive_BottomShowView: UIView {
|
||
//绿色背景图片
|
||
private lazy var bgGreenImageView:UIImageView = UIImageView(image: .init(named: "BottomShow'bg"))
|
||
//音乐封面展示圆框
|
||
private lazy var coverImageView:UIImageView = {
|
||
let imageView:UIImageView = .init(frame: .init(x: 26*width, y: 13*width, width: 48*width, height: 48*width))
|
||
imageView.contentMode = .scaleAspectFill
|
||
imageView.layer.cornerRadius = 24*width
|
||
imageView.layer.masksToBounds = true
|
||
return imageView
|
||
}()
|
||
//标题Label
|
||
private lazy var titleLabel:UILabel = createLabel(font: .systemFont(ofSize: 16*width, weight: .semibold), textColor: .init(hex: "#000000"), textAlignment: .left)
|
||
//副标题(歌手/专辑/乐队)
|
||
private lazy var subtitleLabel:UILabel = createLabel(font: .systemFont(ofSize: 12*width, weight: .light), textColor: .init(hex: "#000000"), textAlignment: .left)
|
||
//生成一个ProgressView
|
||
private lazy var progressView:UIProgressView = {
|
||
let progressView:UIProgressView = .init()
|
||
progressView.isUserInteractionEnabled = false
|
||
progressView.progressTintColor = .white
|
||
progressView.trackTintColor = .clear
|
||
progressView.progress = 0
|
||
return progressView
|
||
}()
|
||
//音乐列表按钮
|
||
private lazy var nextBtn:UIButton = {
|
||
let btn = UIButton()
|
||
btn.setBackgroundImage(UIImage(named: "Bottom Next'logo"), for: .normal)
|
||
btn.addTarget(self, action: #selector(nextClick(_ :)), for: .touchUpInside)
|
||
return btn
|
||
}()
|
||
//音乐播放器状态按钮
|
||
private lazy var playStatuBtn:UIButton = {
|
||
let btn = UIButton()
|
||
btn.setBackgroundImage(UIImage(named: "Bottom Pause'logo"), for: .normal)
|
||
btn.setBackgroundImage(UIImage(named: "Bottom Play'logo"), for: .selected)
|
||
btn.addTarget(self, action: #selector(switchStatuClick(_ :)), for: .touchUpInside)
|
||
return btn
|
||
}()
|
||
//展开音乐播放列表
|
||
var showListBlock:(() -> Void)?
|
||
override init(frame: CGRect) {
|
||
super.init(frame: frame)
|
||
//添加一个监听
|
||
NotificationCenter.notificationKey.add(observer: self, selector: #selector(currentVideoSwitchAction(_:)), notificationName: .positive_player_reload)
|
||
NotificationCenter.notificationKey.add(observer: self, selector: #selector(statusSwitchAction(_:)), notificationName: .switch_player_status)
|
||
|
||
confirgue()
|
||
//打开时检索播放器状态,好调整内容
|
||
MP_PlayerManager.shared.bottomProgressBlock = { [weak self] (currentTime, duration) in
|
||
guard let self = self else { return }
|
||
DispatchQueue.main.async {
|
||
//调整进度条内容
|
||
let value = currentTime/duration
|
||
self.progressView.progress = Float(value)
|
||
}
|
||
}
|
||
}
|
||
|
||
required init?(coder: NSCoder) {
|
||
super.init(coder: coder)
|
||
}
|
||
deinit {
|
||
NotificationCenter.default.removeObserver(self)
|
||
}
|
||
//配置
|
||
private func confirgue() {
|
||
addSubview(bgGreenImageView)
|
||
bgGreenImageView.snp.makeConstraints { make in
|
||
make.left.right.top.bottom.equalToSuperview()
|
||
}
|
||
addSubview(coverImageView)
|
||
addSubview(nextBtn)
|
||
nextBtn.snp.makeConstraints { make in
|
||
make.right.equalToSuperview().offset(-24*width)
|
||
make.centerY.equalToSuperview()
|
||
make.height.width.equalTo(24*width)
|
||
}
|
||
addSubview(playStatuBtn)
|
||
playStatuBtn.snp.makeConstraints { make in
|
||
make.width.height.equalTo(24*width)
|
||
make.centerY.equalTo(nextBtn)
|
||
make.right.equalTo(nextBtn.snp.left).offset(-20*width)
|
||
}
|
||
addSubview(titleLabel)
|
||
titleLabel.snp.makeConstraints { make in
|
||
make.top.equalTo(coverImageView).offset(5*width)
|
||
make.left.equalTo(coverImageView.snp.right).offset(12*width)
|
||
make.right.equalTo(playStatuBtn.snp.left).offset(-10*width)
|
||
}
|
||
addSubview(subtitleLabel)
|
||
subtitleLabel.snp.makeConstraints { make in
|
||
make.left.equalTo(titleLabel)
|
||
make.top.equalTo(titleLabel.snp.bottom).offset(4*width)
|
||
make.right.equalTo(titleLabel)
|
||
}
|
||
addSubview(progressView)
|
||
progressView.snp.makeConstraints { make in
|
||
make.height.equalTo(4*width)
|
||
make.width.equalTo(282*width)
|
||
make.centerX.equalToSuperview()
|
||
make.top.equalToSuperview()
|
||
}
|
||
}
|
||
//切换当前播放音乐时会触发
|
||
@objc private func currentVideoSwitchAction(_ sender:Notification) {
|
||
//更新显示的内容
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
if MP_PlayerManager.shared.loadPlayer?.currentVideo != nil {
|
||
//存在当前播放音乐,更新显示
|
||
coverImageView.kf.setImage(with: MP_PlayerManager.shared.loadPlayer?.currentVideo.coverUrl, placeholder: placeholderImage)
|
||
titleLabel.text = MP_PlayerManager.shared.loadPlayer?.currentVideo?.title ?? ""
|
||
subtitleLabel.text = MP_PlayerManager.shared.loadPlayer?.currentVideo?.subtitle ?? ""
|
||
progressView.progress = 0
|
||
}
|
||
}
|
||
}
|
||
//切换播放器状态时
|
||
@objc private func statusSwitchAction(_ sender:Notification) {
|
||
if sender.object != nil {
|
||
let state:MP_PlayerStateType = sender.object as! MP_PlayerStateType
|
||
DispatchQueue.main.async {
|
||
[weak self] in
|
||
switch state {
|
||
case .Playing:
|
||
self?.playStatuBtn.isSelected = true
|
||
default:
|
||
self?.playStatuBtn.isSelected = false
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//展开当前音乐列表
|
||
@objc private func nextClick(_ sender:UIButton) {
|
||
// guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
|
||
// MP_HUD.text("Bad connection~".localizableString(), delay: 2.0, completion: nil)
|
||
// return
|
||
// }
|
||
guard MP_PlayerManager.shared.loadPlayer?.currentVideo != nil else {
|
||
return
|
||
}
|
||
MPPositive_Debouncer.shared.call {
|
||
[weak self] in
|
||
guard let self = self else {return}
|
||
progressView.progress = 0
|
||
MP_PlayerManager.shared.nextEvent()
|
||
}
|
||
|
||
}
|
||
//切换播放音乐状态
|
||
@objc private func switchStatuClick(_ sender:UIButton) {
|
||
guard MP_PlayerManager.shared.loadPlayer != nil else {
|
||
return
|
||
}
|
||
//在当前音乐填充好之前,禁止触发点击
|
||
switch MP_PlayerManager.shared.getPlayState() {
|
||
case .Null:
|
||
//启动播放
|
||
MP_PlayerManager.shared.play()
|
||
case .Playing:
|
||
//播放中,进入暂停
|
||
MP_PlayerManager.shared.pause {
|
||
[weak self] in
|
||
}
|
||
case .Pause:
|
||
//暂停中,进入继续
|
||
MP_PlayerManager.shared.resume {
|
||
[weak self] in
|
||
}
|
||
}
|
||
}
|
||
}
|