// // SysSoundSetPage.m // pregnancy_guard // // Created by WindShan on 2017/7/12. // Copyright © 2017年 WindShan. All rights reserved. // #import "SysSoundSetPage.h" #import "CSAudioManager.h" #import "NetworkSingleton.h" #import "MusicPlayCell.h" #import "MusicModel.h" #import "SysTipsView.h" #import "QWAlertView.h" @interface SysSoundSetPage () { AVPlayerItem *playerItem; } @property(nonatomic,strong) UITableView * tableView; @property (nonatomic, strong) NSMutableArray *modelArray; @property (nonatomic,strong) CSAudioManager *csManager; @property (nonatomic,strong) NSData *mp3Data; @property (nonatomic, strong) AVPlayer *player; @end @implementation SysSoundSetPage - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; //_tableView.separatorColor = [UIColor clearColor]; [self.view addSubview:_tableView]; [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(UIEdgeInsetsMake(0 , 0, 0, 0)); }]; _tableView.tableFooterView = [UIView new]; } return _tableView; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"系统铃声"; [self setNavigationLeft:@"返回" sel:@selector(backAction)]; //[self setNavigationRight:@"重置" sel:@selector(resetAction)]; self.csManager = [CSAudioManager defaultManager]; self.mp3Data = nil; MPWeakSelf(self); [self.csManager setBlockPlayerStartPlay:^{ NSLog(@"控制器-要开始播放了"); [weakself.tableView reloadData]; }]; [self.csManager setBlockPlayerStopPlay:^{ NSLog(@"控制器-要停止播放了"); [weakself.tableView reloadData]; }]; [self.csManager setBlockPlayerDidFinishPlaying:^{ NSLog(@"控制器-知道播放结束了"); //[weakself.tableView reloadData]; }]; self.player = [[AVPlayer alloc]init]; self.modelArray = [[NSMutableArray alloc] init]; // 2.路径在/System/Library/Audio/UISounds 里,需要更改的可以根据以下列表进行替换 // 3详细列表: // 信息 // ReceivedMessage.caf--收到信息,仅在短信界面打开时播放。 // sms-received1.caf-------三全音 // sms-received2.caf-------管钟琴 // sms-received3.caf-------玻璃 // sms-received4.caf-------圆号 // sms-received5.caf-------铃声 // sms-received6.caf-------电子乐 NSArray *soundFileNameArr = @[@"sms-received1.caf",@"sms-received2.caf",@"sms-received3.caf",@"sms-received4.caf",@"sms-received5.caf",@"sms-received6.caf"]; NSArray *soundMusicNameArr = @[@"三全音",@"管钟琴",@"玻璃",@"圆号",@"铃声",@"电子乐"]; // 0 默认 1 自定义 for (int i = 1; i < 7; i++) { MusicModel * model = [[MusicModel alloc] init]; model.username = [NSString stringWithFormat:@"%@",soundMusicNameArr[i-1]]; model.user_id = [NSString stringWithFormat:@"ID000%d",i]; model.musicName = [NSString stringWithFormat:@"%@",soundMusicNameArr[i-1]]; model.musicStatus = 1; model.musicUrl = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@",soundFileNameArr[i-1]]; [self.modelArray addObject:model]; } self.tableView.hidden = NO; [self.tableView reloadData]; } -(void)backAction { [self.csManager stopAllMusic]; [self dismissViewControllerAnimated:YES completion:nil]; //[self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.modelArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50.0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"MusicSetCell"; MusicPlayCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[MusicPlayCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; } cell.delegate = self; MusicModel * model = [self.modelArray objectAtIndex:indexPath.row]; NSInteger selIndex = [[UserDefault stringForKey:@"video"] integerValue]; if( selIndex == indexPath.row ) { model.setStatus = 1; } model.indexRow = indexPath.row; model._id = [NSString stringWithFormat:@"judqiowu%ld",indexPath.row]; model.xuhaoIndex = [NSString stringWithFormat:@"%02ld",indexPath.row+1]; [cell setItemView:model setName:@"icon_musicset"]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger index = [indexPath row]; // 跳转酒店备用界面 // 跳转选择酒店 //HotelSelPage* page = [[HotelSelPage alloc] initIsFirstPage:NO]; //page.bSelRoom = 2; // 跳转界面 //BaseNaviController *baseNav = [[BaseNaviController alloc] initWithRootViewController:page]; //[self presentViewController:baseNav animated:YES completion:nil]; //[self showActionForPhoto]; } - (void)musicPlaySection:(MusicModel*)model { if ( model.cmd == 1 ) { if( model.playStatus == 1 ) { // 其他播放恢复默认值 for( NSUInteger i = 0; i < self.modelArray.count;i++) { MusicModel * tempModel = [self.modelArray objectAtIndex:i]; if( [tempModel._id isEqualToString:model._id] ) continue; //停止音频 if(tempModel.playStatus == 1) { [self.csManager stopMusicWithPlayID:tempModel.user_id]; [self.csManager disposeSoundWithPlayID:tempModel.user_id]; } tempModel.playStatus = 0; } if( model.musicStatus == 0 ) { [self.csManager playingMusicWithURL:[NSURL URLWithString:model.musicUrl] playID:model.user_id]; } else { // NSString *mp3Path = [[NSBundle mainBundle] pathForResource:model.musicUrl ofType:nil]; // NSLog(@"mp3Path : %@",mp3Path); self.mp3Data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:model.musicUrl]]; //播放音频 [self.csManager playingMusicWithData:self.mp3Data playID:model.user_id]; } } else if( model.playStatus == 2 ) { //停止音频 [self.csManager stopMusicWithPlayID:model.user_id]; } } else if( model.cmd == 2 ) { // 设置系统音乐 [UserDefault setInteger:1 forKey:[NSString stringWithFormat:@"musicStatus%ld",self.soundIndex]]; [UserDefault setObject:model.musicName forKey:[NSString stringWithFormat:@"ID000%ldName",self.soundIndex]]; [UserDefault setObject:model.musicUrl forKey:[NSString stringWithFormat:@"ID000%ldPATH",self.soundIndex]]; [UserDefault synchronize]; [Global alertMessageEx:@"报警音效设置成功!" title:@"温馨提示" okTtitle:nil cancelTitle:@"OK" delegate:self]; } } -(void)update_video:(MusicModel *)model { //[UserDefault stringForKey:@"user_id"] NSString *path = [[NSString alloc] initWithFormat:UPDATE_USERINFO]; NSMutableDictionary *param = [[NSMutableDictionary alloc] init]; //['refresh_frequency', 'video', 'is_open_upload'] [param setValue:[UserDefault stringForKey:@"user_id"] forKey:@"user_id"]; [param setValue:@"video" forKey:@"field"]; // 1 上传 0 不传 [param setValue:[NSString stringWithFormat:@"%ld",model.indexRow] forKey:@"value"]; MPWeakSelf(self); [NetworkSingleton networkingGetMethod:param urlName:path success:^(id responseBody) { MPStrongSelf(self); BaseResModel * resModel = [Global toBaseModel:responseBody]; if(resModel.code == 0) { [UserDefault setObject:[NSString stringWithFormat:@"%ld",model.indexRow] forKey:@"video"]; [UserDefault synchronize]; [self.tableView reloadData]; [Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; } else { [Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self]; } } failure:^(NSString *error) { [Global alertMessageEx:error title:@"获取失败" okTtitle:nil cancelTitle:@"OK" delegate:self]; }]; } /* #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