CNLiveStreamerController.m 6.13 KB
//
//  StreamerViewController.m
//  CNLivePlayerSDKTest
//
//  Created by 雷浩杰 on 16/8/10.
//  Copyright © 2016年 雷浩杰. All rights reserved.
//

#import "CNLiveStreamerController.h"
#import "StreamerDetailViewController.h"
#import "AudioStreamingViewController.h"

#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height

@interface CNLiveStreamerController ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *urlTextField;
@property (nonatomic, strong) UISwitch *pureAudioSwitch;
@property (nonatomic, strong) UISegmentedControl *liveType;

@end

@implementation CNLiveStreamerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.urlTextField];
    [self.view addSubview:self.pureAudioSwitch];
    [self.view addSubview:self.liveType];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 100, 50);
    btn.center = CGPointMake(self.view.center.x, self.view.center.y);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnDidClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

- (void)btnDidClicked{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - Actioon
- (void)onSegmentedControl:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 0) {
        if (_urlTextField.text.length == 0) {
            [self toast:@"请输入推流地址"];
            
        }
        else{
            if (_pureAudioSwitch.on) {
                AudioStreamingViewController *vc = [[AudioStreamingViewController alloc] init];
                vc.isAuthLive = NO;
                vc.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.urlTextField.text]];
                [self presentViewController:vc animated:YES completion:NULL];
            }
            else {
                StreamerDetailViewController *vc = [[StreamerDetailViewController alloc] init];
                vc.isAuthLive = NO;
                vc.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.urlTextField.text]];
                [self presentViewController:vc animated:YES completion:NULL];
            }
        }
        
    }
    else if (sender.selectedSegmentIndex == 1) {
        
        if (_pureAudioSwitch.on) {
            AudioStreamingViewController *vc = [[AudioStreamingViewController alloc] init];
            vc.isAuthLive = YES;
            [self presentViewController:vc animated:YES completion:NULL];
        }
        else {
            StreamerDetailViewController *vc = [[StreamerDetailViewController alloc] init];
            vc.isAuthLive = YES;
            vc.url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.urlTextField.text]];
            [self presentViewController:vc animated:YES completion:NULL];
        }
        
    }
    else if (sender.selectedSegmentIndex == 2) {
        StreamerDetailViewController *vc = [[StreamerDetailViewController alloc] init];
        vc.isRecord = YES;
        [self presentViewController:vc animated:YES completion:NULL];

    }

    _liveType.selectedSegmentIndex = -1;

}


- (void)toast:(NSString*)message {
    UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:nil
                                          otherButtonTitles:nil, nil];
    [toast show];
    double duration = 2.5;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [toast dismissWithClickedButtonIndex:0 animated:NO];
    });
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([_urlTextField isFirstResponder]) {
        [_urlTextField resignFirstResponder];
    }
}

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

#pragma mark - getter
- (UITextField *)urlTextField {
    if (!_urlTextField) {
        _urlTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 170, KScreenWidth-20, 40)];
        _urlTextField.placeholder = @"请输入推流地址";
        _urlTextField.borderStyle = UITextBorderStyleRoundedRect;
        _urlTextField.returnKeyType = UIReturnKeyDone;
        _urlTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _urlTextField.delegate = self;
        _urlTextField.text = @"rtmp://jsrtmp.up.cnlive.com/live/ceshi";
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -30, 80, 30)];
        label.text = @"推流地址:";
        label.font = [UIFont systemFontOfSize:15];
        [_urlTextField addSubview:label];
    }
    return _urlTextField;
}

- (UISwitch *)pureAudioSwitch {
    if (!_pureAudioSwitch) {
        _pureAudioSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(110, 85, 100, 30)];
        UILabel *modeLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 100, 20)];
        modeLab.textColor = [UIColor blackColor];
        modeLab.text = @"纯音频推流";
        [self.view addSubview:modeLab];
    }
    return _pureAudioSwitch;
}

- (UISegmentedControl *)liveType {
    if (!_liveType) {
        NSArray *items = @[ @"普通直播",
                            @"鉴权直播",
                            @"本地录制"];
        
        _liveType = [[UISegmentedControl alloc] initWithItems:items];
        _liveType.frame = CGRectMake(10, self.view.bounds.size.height-80, self.view.bounds.size.width-20, 40);
        _liveType.tintColor= [UIColor blackColor];
        _liveType.selectedSegmentIndex = 0;
        _liveType.layer.cornerRadius = 5;
        _liveType.selectedSegmentIndex = -1;
        [_liveType addTarget:self action:@selector(onSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    }
    return _liveType;
}


@end