Have you ever sung online?

Since the emergence of online karaoke in 2014, it has developed an extremely large user group. One in two people have experienced online karaoke, and its prospect cannot be underestimated.

Such a large market size, as well as the gradual reduction of the threshold for the use of audio and video technology, all kinds of online karaoke players have increased substantially, and they are showing their splendor in the karaoke track. For developers, how to quickly achieve the online KTV scene becomes very important.

If you want to quickly implement online karaoke development, it seems difficult, that is because you do not know ZEGO construction technology, just implement the following three steps to quickly implement online karaoke development.

Today’s article will lead you to understand the structure and implementation process of online KTV, so that the development becomes easier!

First, preparation

1. Preparation of the development environment.

For more details, click integration Process

  • Android Studio 2.1 or later.
  • Android SDK 25, Android SDK build-tools 25.0.2, Android SDK platform-tools 25.x.x or later.
  • Android 4.1 or later, and support audio and video Android devices.
  • Android devices are already connected to the Internet.

2. ZEGO platform account registration

After the development environment is set up, you need to create an application on the ZEGO management console to obtain the AppId and AppSign required for development. For details on how to create an application, see the article “Console Creation Project Flow”

Second, detailed introduction of the implementation process

1. Interface specification description

First, the API input and output specifications of ZEGO are analyzed. According to the API usage documentation, the call interface format is as follows:

Create ZegoExpressEngine single column object as a column, each parameter is detailed parse its role. Each interface describes its role and usage.

2. KTV project development (The implementation process)

KTV is an entertainment scene attached to a voice chat room. In this scenario, a KTV room contains three characters (host, chorus, and audience).

House owner: Create KTV room and push voice and accompaniment to the far end, and initiate mixed-flow tasks. The owner of the house will automatically be on the mic and fixed at the top of the mic. Optional songs (including songs ordered by oneself and songs ordered by the chorus)

Chorus: a chorus member may request a song or join in a chorus with another chorus member. (If you become a chorus player on the mic, you can only select the song that you have selected.)

Audience: After entering the KTV room, pull and play the mixed stream in the room.

The functional modules are room management, mic management, song system, chorus synchronization management, lyrics synchronization management five business modules.

The overall architecture diagram is as follows:

The overall implementation process is as follows: Access reference links

(1) The user needs to call the createEngine interface to initialize the ZEGO Express SDK.

/** Define SDK engine object */ ZegoExpressEngine engine; ZegoEngineProfile profile = new ZegoEngineProfile(); /** Please register with the official website in the format of 123456789L */ profile.appID = appID; AppSign = appSign; /** 64 characters. Please register and obtain the name from the official website in the format of "0123456789" */ profile.appSign = appSign; /** Scenario = zegoscenario.general; /** Set the app application object */ profile.application = getApplication(); / * * * create engine/engine = ZegoExpressEngine createEngine (profile, null);Copy the code

(2) The user needs to invoke the createCopyrightedMusic interface to create copyright music objects.

/** Define CopyrightedMusic objects */ ZegoCopyrightedMusic CopyrightedMusic; / * * * / CopyrightedMusic objects created CopyrightedMusic = engine. CreateCopyrightedMusic ();Copy the code

(3) The user needs to call initCopyrightedMusic interface to initialize the copyright music object.

/** initCopyrightedMusic */ /** CopyrightedMusicConfig */ ZegoCopyrightedMusicConfig config = new ZegoCopyrightedMusicConfig(); /** Enter userID, userName */ String userID =; String userName = ; ZegoUser user = new ZegoUser(userID, userName); config.user = user; copyrightedMusic.initCopyrightedMusic(config, new IZegoCopyrightedMusicInitCallback() { @Override public void onInitCallback(int i) { } });Copy the code

(4) Users can call sendExtendedRequest interface, send copyright music extension request, and obtain songID (songID is the unique identification of a song).

String command = "/playlist/classify"; // Expand request parameter (' CATEGORY_ID '=' category_id ':' 577 ', 'page' : 1, 'size' : 10} '); copyrightedMusic.sendExtendedRequest(command, params, new IZegoCopyrightedMusicSendExtendedRequestCallback() { @Override public void onSendExtendedRequestCallback(int i, String s, String s1) {// s: command // s1: result JSON format String}});Copy the code

(5) The user can call requestSong and requestAccompaniment interface to order songs or accompaniment. Then through ZegoCopyrightedMusicRequestSongCallback, ZegoCopyrightedMusicRequestAccompanimentCallback callback interface, Obtain the resourceID and shareToken corresponding to songID.

/ * * * / ZegoCopyrightedMusicRequestConfig jukebox configuration config = new ZegoCopyrightedMusicRequestConfig (); /** songID */ config.songID =; / * * pricing model * / config. Mode = ZegoCopyrightedMusicBillingMode. GetZegoCopyrightedMusicBillingMode (0); / * * some accompaniment. * / copyrightedMusic requestAccompaniment (config, new IZegoCopyrightedMusicRequestAccompanimentCallback() { @Override public void onRequestAccompanimentCallback(int i, String s) { // s: result } });Copy the code

(6) After obtaining the shareToken shared by other users, getMusicByToken interface can be called to obtain the shared music resources within the valid time. Then get corresponding resourceID shareToken through ZegoCopyrightedMusicGetMusicByTokenCallback callback.

/** Enter shareToken */ String shareToken =; copyrightedMusic.getMusicByToken(shareToken, new IZegoCopyrightedMusicGetMusicByTokenCallback() { @Override public void onGetMusicByTokenCallback(int i, String s) { // s: result } });Copy the code

(7) the user can call getLrcLyric interface to get the lyrics, will get to the result through ZegoCopyrightedMusicGetLrcLyricCallback callback.

/** music songID */ String songID =; copyrightedMusic.getLrcLyric(songID, New IZegoCopyrightedMusicGetLrcLyricCallback () {@ Override public void onGetLrcLyricCallback (int errorCode, , String lyrics) { // s: result } });Copy the code

(8) After obtaining the resourceID, users can call the Download interface to download resources.

/** resourceID */ 
String resourceID = ; 
 copyrightedMusic.download(resourceID, new IZegoCopyrightedMusicDownloadCallback() {     
 @Override     
 public void onDownloadCallback(int i) {      } });
Copy the code

(9) After downloading the resource successfully, users can invoke the Start interface to play the resource, or adjust the playing status through pause and stop interfaces.

/** music resourceID */ String resourceID =; // play long startPosition = ; // create ZegoMediaPlayer mediaPlayer = engine.createmediaPlayer (); mediaPlayer.loadCopyrightedMusicResourceWithPosition(resourceID, startPosition); mediaPlayer.start(); // pause mediaPlayer.pause(); // resume mediaPlayer.resume(); // stop mediaPlayer.stop(); // seekTo long position = ; mediaPlayer.seekTo(position, new IZegoMediaPlayerSeekToCallback() { @Override public void onSeekToCallback(int i) { } });Copy the code

3. Role realization process

(1) The realization process of the host side

  1. Create the engine and configure it.
  2. Create a room in the service background and call loginRoom to join the RTC room.
  3. The homeowner defaults to the mic and is at the top of the mic.
  4. The owner of the house is free to order songs, cut songs (their own songs, the chorus of the song).
  5. The precise mixing task is only initiated by the homeowner, and the mixing includes the homeowner’s voice stream, the homeowner’s accompaniment stream, and the chorus stream.
  6. Send SEI message to everyone in the room to synchronize the song playing progress.
  7. The owner of the house can have other members of the house take off the house, and all members of the house need to be notified of the change to update the house information.
  8. The owner calls the business background to destroy the room, and after success calls logoutRoom to destroy the RTC room, and all members in the room automatically check out.

The flow chart is as follows:

 

(2) Implementation process of chorus

  1. Create the engine and configure it.
  2. Obtain the room list and add it to the service background room. Then call loginRoom to add it to the RTC room.
  3. After entering the room on the mic to become a chorus. On the wheat after the song, the current play is their own point of the song can also cut the song.
  4. Monitor song changes in the room and load songs and lyrics on demand.
  5. Push your own vocal stream and pull all chorus streams, but not mix streams.
  6. Analyze SEI information sent by the owner, and calibrate the progress and lyrics of the local player when the gap between the song playing progress and the owner is too large.

The flow chart is as follows:

(3) Listener side implementation process

  1. Create the engine and configure it.
  2. Obtain the room list and add it to the service background room. Then call loginRoom to add it to the RTC room.
  3. Monitor song changes in the room and load lyrics on demand.
  4. Pull mixed flow.
  5. Parse the SEI message sent by the homeowner to calibrate the progress of local lyrics.

The flow chart is as follows:

Three, online KTV effect display

To try out the Demo, click the link to download the Android APK installation package

Four,

Newton once said: I can see far, because I stand on the shoulders of giants!

ZEGO is based on the technology of audio and video functions, online karaoke solutions, some operation down, it is very simple to develop a “KTV” demo. The online KTV chorus experience can make a breakthrough, thanks to the significant progress in the end-to-end call delay of structure technology, namely structure through the optimization of each link of data transmission, under the premise of guaranteeing the effect of chorus experience, the end-to-end sensory delay is reduced to 70ms, bringing the most extreme audio-visual feast!

Let’s do it together!