Пример iOS-приложения с двумя плеерами
Данный пример показывает, как отобразить в одном приложении более одного плеера, каждый из которых может играть свой видеопоток.
Работа с кодом примера
Для разбора кода возьмем версию примера Player, которая доступена доступна для скачивания в сборке 2.5.2.
...
1. Импорт API. код
Code Block | ||||
---|---|---|---|---|
| ||||
#import <FPWCSApi2/FPWCSApi2.h> |
...
- URL WCS-сервера
- имя серверного приложения defaultApp
Code Block | ||||
---|---|---|---|---|
| ||||
FPWCSApi2SessionOptions *options = [[FPWCSApi2SessionOptions alloc] init]; options.urlServer = _connectUrl.text; options.appKey = @"defaultApp"; NSError *error; FPWCSApi2Session *session = [FPWCSApi2 createSession:options error:&error]; |
...
FPWCSApi2Session connect код
Code Block | ||||
---|---|---|---|---|
| ||||
[session connect]; |
...
ViewController onConnected код
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)onConnected:(FPWCSApi2Session *)session { [_connectButton setTitle:@"DISCONNECT" forState:UIControlStateNormal]; [self changeViewState:_connectButton enabled:YES]; [self onStopped1]; [self onStopped2]; } |
...
- имя воспроизводимого потока
- вид для отображения потока
Code Block | ||||
---|---|---|---|---|
| ||||
- (FPWCSApi2Stream *)play1Stream { FPWCSApi2Session *session = [FPWCSApi2 getSessions][0]; FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init]; options.name = _player1StreamName.text; options.display = _player1Display; NSError *error; player1Stream = [session createStream:options error:nil]; if ... if(![player1Stream play:&error]) { UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Failed to play" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self onStopped1]; }]; [alert addAction:okButton]; [self presentViewController:alert animated:YES completion:nil]; } return nilplayer1Stream; } [player1Stream on:kFPWCSStreamStatusPlaying callback:^} |
6. Воспроизведение второго видеопотока.
FPWCSApi2Session createStream, FPWCSApi2Stream play код
Методу createStream передаются параметры:
- имя воспроизводимого потока
- вид для отображения потока
Code Block | ||||
---|---|---|---|---|
| ||||
- (FPWCSApi2Stream *rStream)play2Stream { FPWCSApi2Session *session = [FPWCSApi2 [self changeStream1Status:rStreamgetSessions][0]; FPWCSApi2StreamOptions *options = [self onPlaying1:rStream[[FPWCSApi2StreamOptions alloc] init]; }]options.name = _player2StreamName.text; options.display = _player2Display; [player1Stream on:kFPWCSStreamStatusNotEnoughtBandwidth callback:^(FPWCSApi2Stream *rStream){ NSError *error; player2Stream = [session NSLog(@"Not enough bandwidth stream %@, consider using lower video resolution or bitrate. Bandwidth %ld bitrate %ld", [rStream getName], [rStream getNetworkBandwidth] / 1000, [rStream getRemoteBitrate] / 1000); createStream:options error:nil]; ... if(![player2Stream play:&error]) { UIAlertController * alert = [UIAlertController [self changeStream1Status:rStream]; }]; [player1Stream on:kFPWCSStreamStatusStopped callback:^(FPWCSApi2Stream *rStream){ [self changeStream1Status:rStream];alertControllerWithTitle:@"Failed to play" [self onStopped1]; }]; [player1Stream on:kFPWCSStreamStatusFailed callback:^(FPWCSApi2Stream *rStream){ [self changeStream1Status:rStream];message:error.localizedDescription [self onStopped1]; }]; if(![player1Stream play:&error]) { preferredStyle:UIAlertControllerStyleAlert]; UIAlertController UIAlertAction* alertokButton = [UIAlertControllerUIAlertAction alertControllerWithTitleactionWithTitle:@"Failed to playOk" message:error.localizedDescriptionstyle:UIAlertActionStyleDefault preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* okButton = [UIAlertAction handler:^(UIAlertAction * action) { actionWithTitle:@"Ok" style:UIAlertActionStyleDefault}]; [alert addAction:okButton]; [self presentViewController:alert animated:YES completion:nil]; } handler:^(UIAlertAction * action) return player2Stream; } |
7. Остановка воспроизведения первого видеопотока.
FPWCSApi2Stream stop код
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)player1Button:(UIButton *)button { [self changeViewState:button enabled:NO]; if ([button.titleLabel.text isEqualToString:@"STOP"]) { if ([FPWCSApi2 getSessions].count) { NSError *error; [player1Stream stop:&error]; } else { }]; NSLog(@"Stop playing, no session"); [alertself addAction:okButtononStopped1]; [self presentViewController:alert animated:YES completion:nil]; } }... return player1Stream;} } |
68. Воспроизведение Остановка воспроизведения второго видеопотока.
FPWCSApi2Session createStream, FPWCSApi2Stream play код
Методу createStream передаются параметры:
- имя воспроизводимого потока
- вид для отображения потока
stop код
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)player2Button:(FPWCSApi2StreamUIButton *)play2Streambutton { FPWCSApi2Session *session = [FPWCSApi2 getSessions][0]; FPWCSApi2StreamOptions *options = [[FPWCSApi2StreamOptions alloc] init]; options.name = _player2StreamName.text; options.display = _player2Display; NSError *error; player2Stream = [session createStream:options error:nil]; if (!player2Stream) { UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Failed to play" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self onStopped2]; }]; [alert addAction:okButton]; [self presentViewController:alert animated:YES completion:nil]; return nil; } [player2Stream on:kFPWCSStreamStatusPlaying callback:^(FPWCSApi2Stream *rStream){ [self changeStream2Status:rStream]; [self onPlaying2:rStream]; }]; [player2Stream on:kFPWCSStreamStatusNotEnoughtBandwidth callback:^(FPWCSApi2Stream *rStream){ NSLog(@"Not enough bandwidth stream %@, consider using lower video resolution or bitrate. Bandwidth %ld bitrate %ld", [rStream getName], [rStream getNetworkBandwidth] / 1000, [rStream getRemoteBitrate] / 1000); [self changeStream2Status:rStream]; }]; [player2Stream on:kFPWCSStreamStatusStopped callback:^(FPWCSApi2Stream *rStream){ [self changeStream2Status:rStream]; [self onStopped2]; }]; [player2Stream on:kFPWCSStreamStatusFailed callback:^(FPWCSApi2Stream *rStream){ [self changeStream2Status:rStream]; [self onStopped2]; }]; if(![player2Stream play:&error]) { UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Failed to play" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; [alert addAction:okButton]; [self presentViewController:alert animated:YES completion:nil]; } return player2Stream; } |
7. Остановка воспроизведения первого видеопотока.
FPWCSApi2Stream stop код
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)player1Button:(UIButton *)button {
[self changeViewState:button enabled:NO];
if ([button.titleLabel.text isEqualToString:@"STOP"]) {
if ([FPWCSApi2 getSessions].count) {
NSError *error;
[player1Stream stop:&error];
} else {
NSLog(@"Stop playing, no session");
[self onStopped1];
}
} else {
if ([FPWCSApi2 getSessions].count) {
[self changeViewState:_player1StreamName enabled:NO];
[self play1Stream];
} else {
NSLog(@"Start playing, no session");
[self onStopped1];
}
}
} |
8. Остановка воспроизведения второго видеопотока.
FPWCSApi2Stream stop код
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)player2Button:(UIButton *)button { [self changeViewState:button enabled:NO]; if ([button.titleLabel.text isEqualToString:@"STOP"]) { if ([FPWCSApi2 getSessions].count) { NSError *error; [player2Stream stop:&error]; } else { NSLog(@"Stop playing, no session"); [self onStopped2]; } } else[self changeViewState:button enabled:NO]; if ([button.titleLabel.text isEqualToString:@"STOP"]) { if ([FPWCSApi2 getSessions].count) { [self changeViewState:_player2StreamName enabled:NO]NSError *error; [self play2Streamplayer2Stream stop:&error]; } else { NSLog(@"StartStop playing, no session"); [self onStopped2]; } ... } } |
9. Закрытие соединения.
FPWCSApi2Session disconnect код
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)connectButton:(UIButton *)button { [self changeViewState:button enabled:NO]; if ([button.titleLabel.text isEqualToString:@"DISCONNECT"]) { if ([FPWCSApi2 getSessions].count) { FPWCSApi2Session *session = [FPWCSApi2 getSessions][0]; NSLog(@"Disconnect session with server %@", [session getServerUrl]); [session disconnect]; } else { NSLog(@"Nothing to disconnect"); [self onDisconnected]; } } else { //todo check url is not empty [self changeViewState:_connectUrl enabled:NO]; [self connect]; } } |
...
ViewController onDisconnected код
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)onDisconnected { [_connectButton setTitle:@"CONNECT" forState:UIControlStateNormal]; [self changeViewState:_connectButton enabled:YES]; [self changeViewState:_connectUrl enabled:YES]; [self onStopped1]; [self onStopped2]; } |