CNLiveMainPlayerViewController.m
7.18 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// 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