单军华
2018-04-28 1d39caf7235522ae121db7a188ba41a706217407
pregnancy_guard/BaseProject/Controller/Mine/MusicSetPage.m
@@ -7,13 +7,24 @@
//
#import "MusicSetPage.h"
#import "CSAudioManager.h"
#import "NetworkSingleton.h"
#import "MusicPlayCell.h"
#import "HotelAccount.h"
#import "MusicModel.h"
#import "SysTipsView.h"
#import "QWAlertView.h"
#import "SysSoundSetPage.h"
#import "BaseNaviController.h"
@interface MusicSetPage ()<UITableViewDelegate,UITableViewDataSource,musicPlayDelegate>
@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
@@ -40,10 +51,273 @@
    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
@@ -76,7 +350,18 @@
        cell = [[MusicPlayCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    
    HotelAccount * model = [self.modelArray objectAtIndex:indexPath.row];
    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;
@@ -99,11 +384,107 @@
    //[self showActionForPhoto];
}
- (void)musicPlaySection:(HotelAccount*)model
- (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