You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
7.1 KiB

2 years ago
//
// MusicManageViewControllers.m
// FireBoltt
2 years ago
//
// Created by ecell on 2023/3/10.
// Copyright © 2023 Sheldon. All rights reserved.
//
#import "MusicManageViewControllers.h"
2 years ago
#import <MediaPlayer/MediaPlayer.h>
#import "MusiclistViewControllers.h"
#import "BLEConstFireBoltt.h"
2 years ago
#import "MusicModel.h"
#import "ListTableViewCell.h"
@interface MusicManageViewControllers ()<UITableViewDelegate,UITableViewDataSource>
2 years ago
@property (nonatomic ,strong) UITableView *listTable;
@property (nonatomic ,strong) NSMutableArray *songArr;
@end
@implementation MusicManageViewControllers
2 years ago
- (void)viewDidLoad
{
[super viewDidLoad];
// // Do any additional setup after loading the view.
[self.view addSubview:self.listTable];
self.listTable.frame = self.view.frame;
[self updataModels];
[self requestAuthorizationForMediaLibrary];
[self getItunesMusic];
}
- (void)updataModels
{
NSString *reconnectionUUIDSrting = [[NSUserDefaults standardUserDefaults] objectForKey:AutomaticReconnectionDevice];
NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:reconnectionUUIDSrting]);
NSArray *modelArr = [[NSUserDefaults standardUserDefaults] objectForKey:reconnectionUUIDSrting];
if (modelArr.count > 0)
{
for (NSDictionary *dic in modelArr)
{
MusicModel *model = [MusicModel new];
[model setValuesForKeysWithDictionary:dic];
[self.songArr addObject:model];
}
[self.listTable reloadData];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 80, 49);
[button setTitle:NSLocalizedString(@"音乐管理", nil) forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size: 20];
[button setTitleColor:[UIColor colorWithRed:45.0/255.0 green:45.0/255.0 blue:45.0/255.0 alpha:1] forState:0];
[button setImage:[UIImage imageNamed:@"fb_nav_back"] forState:0];
2 years ago
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = backButtonItem;
UIButton *addbtn = [UIButton buttonWithType:UIButtonTypeCustom];
addbtn.frame = CGRectMake(0, 0, 60, 49);
[addbtn setTitle:NSLocalizedString(@"添加", nil) forState:UIControlStateNormal];
addbtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size: 20];
[addbtn setTitleColor:[UIColor colorWithRed:45.0/255.0 green:45.0/255.0 blue:45.0/255.0 alpha:1] forState:0];
[addbtn addTarget:self action:@selector(addMusic) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addbtn];
self.navigationItem.rightBarButtonItem = rightButtonItem;
}
- (UITableView *)listTable
{
if (!_listTable)
{
_listTable = [[UITableView alloc] init];
_listTable.backgroundColor = [UIColor clearColor];
_listTable.dataSource = self;
_listTable.delegate = self;
[_listTable setRowHeight:70];
// _listTable.separatorStyle = UITableViewCellSeparatorStyleNone;
[_listTable registerClass:ListTableViewCell.class forCellReuseIdentifier:NSStringFromClass(ListTableViewCell.class)];
//__weak typeof(self) weakSelf = self;
}
return _listTable;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ListTableViewCell *cell = [ListTableViewCell cellWithTableView:tableView indexPath:indexPath];
cell.msgModel = self.songArr[indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.songArr.count;
}
#pragma mark 添加音乐
- (void)addMusic
{
MusiclistViewControllers *vc = [MusiclistViewControllers new];
2 years ago
vc.view.backgroundColor = UIColor.whiteColor;
vc.updataOK = ^{
[self updataModels];
};
[self.navigationController pushViewController:vc animated:YES];
}
- (void)buttonClick
{
[self.navigationController popViewControllerAnimated:YES];
}
- (NSMutableArray *)songArr
{
if (!_songArr) _songArr = [NSMutableArray new];
return _songArr;
}
// MARK:- 判断是否有权限
- (void)requestAuthorizationForMediaLibrary
{
__weak typeof(self) weakSelf = self;
// 请求媒体资料库权限
MPMediaLibraryAuthorizationStatus authStatus = [MPMediaLibrary authorizationStatus];
if (authStatus != MPMediaLibraryAuthorizationStatusAuthorized) {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
if (appName == nil) {
appName = @"APP";
}
NSString *message = [NSString stringWithFormat:@"允许%@访问你的媒体资料库?", appName];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *setAction = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
[weakSelf dismissViewControllerAnimated:YES completion:nil];
}
}];
[alertController addAction:okAction];
[alertController addAction:setAction];
[self presentViewController:alertController animated:YES completion:nil];
}
}
// MARK:- 获取 iTunes 中的音乐
- (void)getItunesMusic
{
// 创建媒体选择队列
MPMediaQuery *query = [[MPMediaQuery alloc] init];
// 创建读取条件
MPMediaPropertyPredicate *albumNamePredicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic] forProperty:MPMediaItemPropertyMediaType];
// 给队列添加读取条件
[query addFilterPredicate:albumNamePredicate];
// 从队列中获取条件的数组集合
NSArray *itemsFromGenericQuery = [query items];
// 遍历解析数据
for (MPMediaItem *music in itemsFromGenericQuery) {
// [self resolverMediaItem:music];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end