Example of iOS application with two players
This example demonstrates how two or more players can be displayed in one application. Each of the players can be used to play a different stream.
Work with code of the example
To analyze the code, let's take TwoPlayers example version with hash 088a74d, which can be downloaded with build 2.5.2.4.
View class for the main view of the application: ViewController (header file ViewController.h; implementation file ViewController.m).
Functions of establishing connection, creating stream and starting playback work the same as described in the example Player.
Unlike the example Player, method for establishing connection is called when Connect button is tapped.
...
1. Import of API. code
Code Block | ||||
---|---|---|---|---|
| ||||
#import <FPWCSApi2/FPWCSApi2.h> |
2. Session creation
FPWCSApi2 createSession code
The options include:
- URL of WCS server
- appKey of internal server-side application (defaultApp)
Code Block | ||||
---|---|---|---|---|
| ||||
FPWCSApi2SessionOptions *options = [[FPWCSApi2SessionOptions alloc] init];
options.urlServer = _connectUrl.text;
options.appKey = @"defaultApp";
NSError *error;
FPWCSApi2Session *session = [FPWCSApi2 createSession:options error:&error]; |
3. Connection to the server
FPWCSApi2Session connect code
Code Block | ||||
---|---|---|---|---|
| ||||
[selfsession connect]; |
This example has two views for displaying video and two corresponding Play buttons; corresponding method is called to start playing when one of the buttons is tapped.
...
4. Receiving the event confirming successful connection.
ViewController onConnected code
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)onConnected:(FPWCSApi2Session *)session { [_connectButton setTitle:@"DISCONNECT" forState:UIControlStateNormal]; [self changeViewState:_connectButton enabled:YES]; [self play1Stream onStopped1]; [self onStopped2]; |
...
} |
5. Playback of video stream 1.
FPWCSApi2Session createStream, FPWCSApi2Stream play code
Object with next stream options is passed to createStream method:
- stream name
- view to display video
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(![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) { }]; [alert addAction:okButton]; [self play2Stream]; |
Playback is stopped when corresponding Stop button is tapped: FPWCSApi2Stream method stop is called for corresponding stream
Code Block | ||||
---|---|---|---|---|
| ||||
[player1Stream stop:&error]; |
Code Block | ||||
---|---|---|---|---|
| ||||
[player2Stream stop:&error]; presentViewController:alert animated:YES completion:nil]; } return player1Stream; } |
6. Playback of video stream 2.
FPWCSApi2Session createStream, FPWCSApi2Stream play code
Object with next stream options is passed to createStream method:
- stream name
- view to display video
Code Block | ||||
---|---|---|---|---|
| ||||
- (FPWCSApi2Stream *)play2Stream {
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 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. Stream 1 playback stop.
FPWCSApi2Stream stop code
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];
}
...
}
} |
8. Stream 2 playback stop.
FPWCSApi2Stream stop code
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];
}
...
}
} |
9. Disconnection.
FPWCSApi2Session disconnect code
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];
}
} |
10. Receiving the event confirming successful disconnection.
ViewController onDisconnected code
Code Block | ||||
---|---|---|---|---|
| ||||
- (void)onDisconnected {
[_connectButton setTitle:@"CONNECT" forState:UIControlStateNormal];
[self changeViewState:_connectButton enabled:YES];
[self changeViewState:_connectUrl enabled:YES];
[self onStopped1];
[self onStopped2];
} |