单军华
2018-04-28 1d39caf7235522ae121db7a188ba41a706217407
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//
//  MusicSetPage.m
//  pregnancy_guard
//
//  Created by WindShan on 2017/7/12.
//  Copyright © 2017年 WindShan. All rights reserved.
//
 
#import "MusicSetPage.h"
#import "CSAudioManager.h"
#import "NetworkSingleton.h"
#import "MusicPlayCell.h"
#import "MusicModel.h"
#import "SysTipsView.h"
#import "QWAlertView.h"
#import "SysSoundSetPage.h"
#import "BaseNaviController.h"
 
@interface MusicSetPage ()<UITableViewDelegate,UITableViewDataSource,musicPlayDelegate,SelectedActionDelegate>
{
    
}
@property (nonatomic,strong) NSData *mp3Data;
@property (nonatomic,strong) CSAudioManager *csManager;
@property(nonatomic,strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, strong) SysTipsView * sysTipsView; // 广告控件
@end
 
@implementation MusicSetPage
 
- (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;
}
 
- (SysTipsView *)sysTipsView
{
    if(!_sysTipsView)
    {
        _sysTipsView = [[SysTipsView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) tipsTitle:@"温馨提示" tipsContext:@"是否重置为默认铃声"];
        self.sysTipsView.delegate = self;
    }
    else
    {
        [_sysTipsView setHidden:FALSE];
    }
    
    return _sysTipsView;
}
 
- (void)didSelectedAction:(NSInteger)type
{
    [self.sysTipsView setHidden:TRUE];
    [[QWAlertView sharedMask] dismiss];
    
    // 确定重置
    if (type == 1)
    {
        [self.csManager stopAllMusic];
        [self.modelArray removeAllObjects];
        
        MusicModel * model1 = [[MusicModel alloc] init];
        model1.username = [NSString stringWithFormat:@"%@",@"轻微铃声"];
        model1.user_id = [NSString stringWithFormat:@"%@",@"ID0001"];
        model1.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model1.musicStatus = 0;
        model1.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music.mp3"];
        [self.modelArray addObject:model1];
        
        MusicModel * model2 = [[MusicModel alloc] init];
        model2.username = [NSString stringWithFormat:@"%@",@"中度铃声"];
        model2.user_id = [NSString stringWithFormat:@"%@",@"ID0002"];
        model2.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model2.musicStatus = 0;
        model2.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music1.mp3"];
        [self.modelArray addObject:model2];
        
        MusicModel * model3 = [[MusicModel alloc] init];
        model3.username = [NSString stringWithFormat:@"%@",@"严重铃声"];
        model3.user_id = [NSString stringWithFormat:@"%@",@"ID0003"];
        model3.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model3.musicStatus = 0;
        model3.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music2.mp3"];
        
        [self.modelArray addObject:model3];
        
        [self.tableView reloadData];
    }
}
 
- (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];
    }];
 
    
    //播放系统声音,系统声音名详见CSAudioManager.h 最下方
    //[self.csManager playSoundWithSoundName:@"sms-received1" PlayID:kPlayID2];
    
    
    self.modelArray = [[NSMutableArray alloc] init];
//    @property(nonatomic, strong) NSString * _id;
//    @property(nonatomic, strong) NSString * user_id;
//    @property(nonatomic, strong) NSString * username;
//    @property(nonatomic, strong) NSString * musicName;
//    @property(nonatomic, strong) NSString * musicUrl; // 音乐地址
//    @property(nonatomic, strong) NSString * xuhaoIndex;
//
//    @property(nonatomic, assign) int  cmd; // 1 播放 2 设置默认
//    @property(nonatomic, assign) int  playStatus; // 默认0 播放状态命令 1 播放操作 2 暂停操作
//    @property(nonatomic, assign) int  musicStatus; // 默认0 设置状态命令 1 默认设置 2 默认取消
//    @property(nonatomic, assign) NSInteger  indexRow; // 当前索引 http://yfsapi.7drlb.com/video/music.mp3
 
//    [UserDefault stringForKey:@"username"]
    // 0 默认 1 自定义
    NSInteger musicStatus1 = [UserDefault integerForKey:@"musicStatus1"];
    NSInteger musicStatus2 = [UserDefault integerForKey:@"musicStatus2"];
    NSInteger musicStatus3 = [UserDefault integerForKey:@"musicStatus3"];
    
    if( musicStatus1 == 0 )
    {
        MusicModel * model1 = [[MusicModel alloc] init];
        model1.username = [NSString stringWithFormat:@"%@",@"轻微铃声"];
        model1.user_id = [NSString stringWithFormat:@"%@",@"ID0001"];
        model1.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model1.musicStatus = 0;
        model1.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music.mp3"];
        [self.modelArray addObject:model1];
    }
    else
    {
        MusicModel * model1 = [[MusicModel alloc] init];
        model1.username = [NSString stringWithFormat:@"%@",@"轻微铃声"];
        model1.user_id = [NSString stringWithFormat:@"%@",@"ID0001"];
        model1.musicName = [UserDefault stringForKey:@"ID0001Name"];
        model1.musicStatus = 1;
        model1.musicUrl = [UserDefault stringForKey:@"ID0001PATH"];
        [self.modelArray addObject:model1];
    }
    
    
    if( musicStatus2 == 0 )
    {
        MusicModel * model2 = [[MusicModel alloc] init];
        model2.username = [NSString stringWithFormat:@"%@",@"中度铃声"];
        model2.user_id = [NSString stringWithFormat:@"%@",@"ID0002"];
        model2.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model2.musicStatus = 0;
        model2.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music1.mp3"];
        [self.modelArray addObject:model2];
    }
    else
    {
        MusicModel * model2 = [[MusicModel alloc] init];
        model2.username = [NSString stringWithFormat:@"%@",@"中度铃声"];
        model2.user_id = [NSString stringWithFormat:@"%@",@"ID0002"];
        model2.musicName = [UserDefault stringForKey:@"ID0002Name"];
        model2.musicStatus = 1;
        model2.musicUrl = [UserDefault stringForKey:@"ID0002PATH"];
        [self.modelArray addObject:model2];
    }
    
    if( musicStatus3 == 0 )
    {
        MusicModel * model3 = [[MusicModel alloc] init];
        model3.username = [NSString stringWithFormat:@"%@",@"严重铃声"];
        model3.user_id = [NSString stringWithFormat:@"%@",@"ID0003"];
        model3.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model3.musicStatus = 0;
        model3.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music2.mp3"];
        
        [self.modelArray addObject:model3];
    }
    else
    {
        MusicModel * model3 = [[MusicModel alloc] init];
        model3.username = [NSString stringWithFormat:@"%@",@"严重铃声"];
        model3.user_id = [NSString stringWithFormat:@"%@",@"ID0003"];
        model3.musicName = [UserDefault stringForKey:@"ID0003Name"];
        model3.musicStatus = 1;
        model3.musicUrl = [UserDefault stringForKey:@"ID0003PATH"];
        
        [self.modelArray addObject:model3];
    }
    
    self.tableView.hidden = NO;
    [self.tableView reloadData];
}
 
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.modelArray removeAllObjects];
    // 0 默认 1 自定义
    NSInteger musicStatus1 = [UserDefault integerForKey:@"musicStatus1"];
    NSInteger musicStatus2 = [UserDefault integerForKey:@"musicStatus2"];
    NSInteger musicStatus3 = [UserDefault integerForKey:@"musicStatus3"];
    
    if( musicStatus1 == 0 )
    {
        MusicModel * model1 = [[MusicModel alloc] init];
        model1.username = [NSString stringWithFormat:@"%@",@"轻微铃声"];
        model1.user_id = [NSString stringWithFormat:@"%@",@"ID0001"];
        model1.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model1.musicStatus = 0;
        model1.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music.mp3"];
        [self.modelArray addObject:model1];
    }
    else
    {
        MusicModel * model1 = [[MusicModel alloc] init];
        model1.username = [NSString stringWithFormat:@"%@",@"轻微铃声"];
        model1.user_id = [NSString stringWithFormat:@"%@",@"ID0001"];
        model1.musicName = [UserDefault stringForKey:@"ID0001Name"];
        model1.musicStatus = 1;
        model1.musicUrl = [UserDefault stringForKey:@"ID0001PATH"];
        [self.modelArray addObject:model1];
    }
    
    
    if( musicStatus2 == 0 )
    {
        MusicModel * model2 = [[MusicModel alloc] init];
        model2.username = [NSString stringWithFormat:@"%@",@"中度铃声"];
        model2.user_id = [NSString stringWithFormat:@"%@",@"ID0002"];
        model2.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model2.musicStatus = 0;
        model2.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music1.mp3"];
        [self.modelArray addObject:model2];
    }
    else
    {
        MusicModel * model2 = [[MusicModel alloc] init];
        model2.username = [NSString stringWithFormat:@"%@",@"中度铃声"];
        model2.user_id = [NSString stringWithFormat:@"%@",@"ID0002"];
        model2.musicName = [UserDefault stringForKey:@"ID0002Name"];
        model2.musicStatus = 1;
        model2.musicUrl = [UserDefault stringForKey:@"ID0002PATH"];
        [self.modelArray addObject:model2];
    }
    
    if( musicStatus3 == 0 )
    {
        MusicModel * model3 = [[MusicModel alloc] init];
        model3.username = [NSString stringWithFormat:@"%@",@"严重铃声"];
        model3.user_id = [NSString stringWithFormat:@"%@",@"ID0003"];
        model3.musicName = [NSString stringWithFormat:@"%@",@"默认"];
        model3.musicStatus = 0;
        model3.musicUrl = [NSString stringWithFormat:@"%@",@"http://yfsapi.7drlb.com/video/music2.mp3"];
        
        [self.modelArray addObject:model3];
    }
    else
    {
        MusicModel * model3 = [[MusicModel alloc] init];
        model3.username = [NSString stringWithFormat:@"%@",@"严重铃声"];
        model3.user_id = [NSString stringWithFormat:@"%@",@"ID0003"];
        model3.musicName = [UserDefault stringForKey:@"ID0003Name"];
        model3.musicStatus = 1;
        model3.musicUrl = [UserDefault stringForKey:@"ID0003PATH"];
        
        [self.modelArray addObject:model3];
    }
    
    self.tableView.hidden = NO;
    [self.tableView reloadData];
}
 
-(void)resetAction
{
    [[QWAlertView sharedMask] show:self.sysTipsView withType:(QWAlertViewStyle)QWAlertViewStyleAlert];
}
 
-(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];
    
    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 )
        {
            // 已存在销毁音频文件
//            //if( self.mp3Data != nil )
//            {
//                //停止音频
//                [self.csManager stopMusicWithPlayID:model.user_id];
//                [self.csManager disposeSoundWithPlayID:model.user_id];
//            }
            
            // 其他播放恢复默认值
            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 )
    {
         [self.csManager stopAllMusic];
        // 打开本地音频文件
        SysSoundSetPage* Page = [[SysSoundSetPage alloc] initIsFirstPage:NO];
        Page.soundIndex = [model.xuhaoIndex integerValue];
        BaseNaviController *baseNav = [[BaseNaviController alloc] initWithRootViewController:Page];
        [self presentViewController:baseNav animated:YES completion:nil];
    }
}
 
 
-(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