CNLiveMainPlayerViewController.m 7.18 KB
//
//  CNLiveMainPlayerViewController.m
//  QNLiveStreamingDemo
//
//  Created by iOS on 17/4/7.
//  Copyright © 2017年 zhulin. All rights reserved.
//

#import "CNLiveMainPlayerViewController.h"
#import "CNLivePlayerViewController.h"

@interface CNLiveMainPlayerViewController ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>

{
    UITextField *_textField;
    UITableView *_historyTableView;
}

@end

@implementation CNLiveMainPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    NSArray *titlesArr = @[@"视讯云直播播放",@"视讯云点播播放",@"普通播放"];
    
    for (int i = 0; i < 3; i ++) {
        UIButton *cnlivePlayerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cnlivePlayerBtn.frame = CGRectMake((CGRectGetWidth(self.view.frame)-150)/2, 80+i*(40+20), 150, 40);
        [cnlivePlayerBtn setTitle:titlesArr[i] forState:UIControlStateNormal];
        [cnlivePlayerBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        [cnlivePlayerBtn addTarget:self action:@selector(cnlivePlayerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        cnlivePlayerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        cnlivePlayerBtn.tag = 10000+i;
        cnlivePlayerBtn.layer.cornerRadius = 10;
        cnlivePlayerBtn.layer.masksToBounds = YES;
        cnlivePlayerBtn.layer.borderColor = [UIColor blackColor].CGColor;
        cnlivePlayerBtn.layer.borderWidth = 1;
        [self.view addSubview:cnlivePlayerBtn];
    }
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 260, 80, 30)];
    label.text = @"播放地址:";
    label.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:label];
    
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(90, 260, self.view.frame.size.width-100, 30)];
    _textField.delegate = self;
    [self.view addSubview:_textField];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.returnKeyType = UIReturnKeyDone;
    _textField.clearButtonMode = UITextFieldViewModeAlways;
    
    NSArray *array = [[NSArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"historyURLs"]];
    if (array.count > 0) {
        _textField.text = array[0];
    } else {
        _textField.text = @"http://video00.cnlive.com/video/data1/2015/0518/cnlive_200.mp4";
    }
    
    _historyTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 300, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height-80-300) style:UITableViewStylePlain];
    _historyTableView.delegate = self;
    _historyTableView.dataSource = self;
    [self.view addSubview:_historyTableView];
    
    UIButton *exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    exitBtn.frame = CGRectMake((CGRectGetWidth(self.view.frame)-150)/2, [UIScreen mainScreen].bounds.size.height-70, 150, 40);
    [exitBtn setTitle:@"退出" forState:UIControlStateNormal];
    [exitBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
    [exitBtn addTarget:self action:@selector(exitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    exitBtn.layer.cornerRadius = 10;
    exitBtn.layer.masksToBounds = YES;
    exitBtn.layer.borderColor = [UIColor blackColor].CGColor;
    exitBtn.layer.borderWidth = 1;
    [self.view addSubview:exitBtn];


}

- (void)exitBtnAction
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)cnlivePlayerBtnAction:(UIButton *)button
{
    if (button.tag == 10002) {
        [self normalPlayerBtnAction];
    } else {
        CNLivePlayerViewController *cnlive = [[CNLivePlayerViewController alloc] init];
        cnlive.type = (PlayerType)button.tag-10000;
        [self presentViewController:cnlive animated:YES completion:NULL];
    }
    
}

- (void)normalPlayerBtnAction
{
    if (_textField.text.length == 0 || [_textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0) {
        UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"请输入播放地址!"
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:nil, nil];
        [toast show];
        double duration = 2;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [toast dismissWithClickedButtonIndex:0 animated:NO];
        });
        return;
    }
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"historyURLs"]];
    if (![array containsObject:_textField.text]) {
        [array insertObject:_textField.text atIndex:0];
        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"historyURLs"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [_historyTableView reloadData];
    }
    
    CNLivePlayerViewController *cnlive = [[CNLivePlayerViewController alloc] init];
    cnlive.type = PlayerTypeNormal;
    cnlive.videoUrlString = _textField.text;
    [self presentViewController:cnlive animated:YES completion:NULL];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = [[NSArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"historyURLs"]];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"historyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"historyURLs"];
    cell.textLabel.text = array[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"historyURLs"];
    _textField.text = array[indexPath.row];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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