Files
yinmeng-ios/xplan-ios/Main/Room/View/BackMusic/View/SubViews/XPRoomBackMusicPlayerView.m
2022-05-13 16:14:47 +08:00

217 lines
7.3 KiB
Objective-C

//
// XPRoomBackMusicPlayerView.m
// xplan-ios
//
// Created by 冯硕 on 2022/5/9.
//
#import "XPRoomBackMusicPlayerView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor.h"
#import "UIImage+Utils.h"
#import "XCCurrentVCStackManager.h"
#import "XPCoreDataManager.h"
#import "RtcManager.h"
///Model
#import "Music+CoreDataClass.h"
///View
#import "XPRoomMusicLibraryViewController.h"
@interface XPRoomBackMusicPlayerView ()<NSFetchedResultsControllerDelegate>
///显示歌曲的名字
@property (nonatomic,strong) UILabel *titleLabel;
///声音的logo
@property (nonatomic,strong) UIImageView *voiceImageView;
///调节声音
@property (nonatomic,strong) UISlider *voiceSliderView;
///播放暂停
@property (nonatomic,strong) UIButton *playButton;
///下一首
@property (nonatomic,strong) UIButton *nextButton;
///更多
@property (nonatomic,strong) UIButton *moreButton;
@property(nonatomic,strong)NSFetchedResultsController *fetchedResultController;
@end
@implementation XPRoomBackMusicPlayerView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 40.f;
[self addSubview:self.titleLabel];
[self addSubview:self.voiceImageView];
[self addSubview:self.voiceSliderView];
[self addSubview:self.playButton];
[self addSubview:self.nextButton];
[self addSubview:self.moreButton];
}
- (void)initSubViewConstraints {
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self).inset(30);
make.top.mas_equalTo(self).offset(17);
}];
[self.voiceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(12, 15));
make.left.mas_equalTo(self.titleLabel);
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(18);
}];
[self.voiceSliderView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.voiceImageView.mas_right).offset(5);
make.right.mas_equalTo(self.playButton.mas_left).offset(-10);
make.centerY.mas_equalTo(self.voiceImageView);
}];
[self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(22, 22));
make.centerY.mas_equalTo(self.voiceImageView);
make.right.mas_equalTo(self.nextButton.mas_left).offset(-10);
}];
[self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(22, 22));
make.centerY.mas_equalTo(self.voiceImageView);
make.right.mas_equalTo(self.moreButton.mas_left).offset(-10);
}];
[self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(22, 22));
make.centerY.mas_equalTo(self.voiceImageView);
make.right.mas_equalTo(self).offset(-30);
}];
}
#pragma mark - Event Response
- (void)voiceSliderViewValueChange:(UISlider *)slider {
}
- (void)moreButtonAction:(UIButton *)sender {
XPRoomMusicLibraryViewController * musicLibraryVC = [[XPRoomMusicLibraryViewController alloc] init];
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:musicLibraryVC animated:YES];
}
- (void)playButtonAction:(UIButton *)sender {
if (self.fetchedResultController.fetchedObjects.count <=0) {
XPRoomMusicLibraryViewController * musicLibraryVC = [[XPRoomMusicLibraryViewController alloc] init];
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:musicLibraryVC animated:YES];
} else {
switch (sender.tag) {
case 1000:
{
NSArray * array = self.fetchedResultController.fetchedObjects;
Music * music = [array objectAtIndex:0];
[[RtcManager instance] playBackMusic:music.filePath musicId:0];
[[RtcManager instance] updateUserSound:50];
[[RtcManager instance] updateMusicSound:50];
}
break;
case 1001:
break;
case 1002:
break;
default:
break;
}
}
}
#pragma mark - Getters And Setters
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:13];
_titleLabel.textColor = UIColor.whiteColor;
_titleLabel.text = @"暂无音乐播放";
}
return _titleLabel;
}
- (UIImageView *)voiceImageView {
if (!_voiceImageView) {
_voiceImageView = [[UIImageView alloc] init];
_voiceImageView.userInteractionEnabled = YES;
_voiceImageView.image = [UIImage imageNamed:@"room_music_small_player_voice"];
}
return _voiceImageView;
}
- (UISlider *)voiceSliderView {
if (!_voiceSliderView) {
_voiceSliderView = [[UISlider alloc]init];
_voiceSliderView.minimumValue = 0;
_voiceSliderView.maximumValue = 100;
_voiceSliderView.value = 50;
_voiceSliderView.minimumTrackTintColor = [UIColor whiteColor];
_voiceSliderView.maximumTrackTintColor = UIColorFromRGB(0x999999);
[_voiceSliderView setThumbImage:[UIImage imageNamed:@"room_music_small_player_white_dot"] forState:UIControlStateNormal];
[_voiceSliderView addTarget:self action:@selector(voiceSliderViewValueChange:) forControlEvents:UIControlEventValueChanged];
}
return _voiceSliderView;
}
- (UIButton *)playButton {
if (!_playButton) {
_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_playButton setImage:[UIImage imageNamed:@"room_music_small_player_play"] forState:UIControlStateNormal];
[_playButton setImage:[UIImage imageNamed:@"room_music_small_player_pause"] forState:UIControlStateSelected];
_playButton.tag = 1000;
[_playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _playButton;
}
- (UIButton *)moreButton {
if (!_moreButton) {
_moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_moreButton setImage:[UIImage imageNamed:@"room_music_small_player_more"] forState:UIControlStateNormal];
[_moreButton setImage:[UIImage imageNamed:@"room_music_small_player_more"] forState:UIControlStateSelected];
_moreButton.tag = 1001;
[_moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
_moreButton.transform = CGAffineTransformMakeRotation(-M_PI /2);
}
return _moreButton;
}
- (UIButton *)nextButton {
if (!_nextButton) {
_nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_nextButton setImage:[UIImage imageNamed:@"room_music_small_player_next"] forState:UIControlStateNormal];
[_nextButton setImage:[UIImage imageNamed:@"room_music_small_player_next"] forState:UIControlStateSelected];
_nextButton.tag = 1002;
[_nextButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _nextButton;
}
-(NSFetchedResultsController *)fetchedResultController{
if (!_fetchedResultController) {
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Music"];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"musicId" ascending:true];
request.sortDescriptors = @[sort];
_fetchedResultController = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:[XPCoreDataManager shareInstance].managedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultController.delegate = self;
[_fetchedResultController performFetch:nil];
}
return _fetchedResultController;
}
@end