CNLiveStreamerController.m
6.13 KB
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
//
// 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