In-Game Voice Chat
  • Overview
  • Sample Codes
  • SDK Integration
  • Solution Implementation
  • API Documents
  • Error Codes
  • Documentation
  • In-Game Voice Chat
  • Solution Implementation
  • iOS

Function Realization Process

Last updated:2022-03-22 13:06

One of the typical game voice scenarios is that during the game, members in the same room conduct game voice conversations.

Take the game voice between 2 people as an example, the main process is as follows:

Note:

  1. In the above process, the game voice between 2 room members is taken as an example. In fact, ZEGO SDK supports multiplayer game voice. It is recommended that developers design according to their needs.

  2. In order to facilitate developers to understand the logic in AudioLive more quickly, the following sections will pick out and explain the core source code fragments. Developers can also directly read AudioLive source code, the two are the same.

1 Login Room

Before real-time voice conversations between users, they need to log in to the same room.

The real-time audio of the relevant source code snippets registered in AudioLive is shown below, for reference only:

ZegoAudioLiveViewController.m

// log in to the room
[[ZegoAudioLive api] loginRoom:self.sessionID completionBlock:^(int errorCode) {
    if (errorCode != 0)
    {
        [self addLogString:[NSString stringWithFormat:NSLocalizedString(@"Failed to join session: %d", nil), errorCode]];
        self.tipsLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Failed to login the room: %d", nil), errorCode];
    }
    else
    {
        self.mutedButton.enabled = YES;
        self.publishButton.enabled = YES;
        self.messageButton.enabled = YES;

        [self addLogString:[NSString stringWithFormat:NSLocalizedString(@"Success to join session", nil)]];
        self.tipsLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Success to login the room", nil)];
    }
}];

2 Publish Live Broadcast

Members in the room can post live broadcast, but there are two ways to post live broadcast:

Automatically publish live broadcast, please refer to: Quick Start-Automatically Publish Live Broadcast.
to publish the live broadcast manually, please refer to: Quick Start-Manually Publish the Live Broadcast.

Note: If the user chooses to publish the live broadcast automatically, after logging in successfully, the SDK will automatically start streaming, and the user does not need to manually trigger.

The following is a demonstration of manually releasing live-related source code snippets in AudioLive, for reference only:

ZegoAudioLiveViewController.m

// the user actively clicks the publish live button
- (IBAction)onPublishButton:(id)sender
{
    if (self.isPublished)
    {
        // stop live streaming
        [[ZegoAudioLive api] stopPublish];
        [self.publishButton setTitle:NSLocalizedString(@"Start live broadcast.", nil) forState:UIControlStateNormal];
        self.isPublished = NO;

        // delete stream
        for (ZegoAudioStream *audioStream in self.streamList)
        {
            if ([audioStream.userID isEqualToString:[ZegoSettings sharedInstance].userID])
            {
                [self.streamList removeObject:audioStream];
                break;
            }
        }

        [self.tableView reloadData];
    }
    else
    {
         // publish live broadcast manually
        BOOL result = [[ZegoAudioLive api] startPublish];
        if (result == NO)
        {
            self.tipsLabel.text = NSLocalizedString(@"Failed to start broadcasting, live streaming exceeds the upper limit.", nil);
        }
        else
        {
            [self.publishButton setTitle:NSLocalizedString(@"Stop live streaming.", nil) forState:UIControlStateNormal];
            self.publishButton.enabled = NO;
        }
    }
}

3 Play Live Broadcast

Members in the room can play the live broadcast. After logging in to the room, the SDK will automatically pull the stream to play the live broadcast. Users do not need to manually trigger the pull stream.

If the developer wants to know more about broadcasting live broadcast, please refer to: Quick Start-Broadcast Live Broadcast.

4 End Audio Live Broadcast

The operations after the audio call is over are mainly to log out of the room, clean up the view or data, etc. Developers can call on demand.

The source code snippets related to the ending audio live broadcast in AudioLive are shown below for reference only:

ZegoAudioLiveViewController.m

// turn off game voice calls ViewController
- (IBAction)closeView:(id)sender
{
     // exit the room
    [[ZegoAudioLive api] logoutRoom];
    [self.streamList removeAllObjects];

    [self dismissViewControllerAnimated:YES completion:nil];
}
Page Directory