diff --git a/shared/video-sdk/get-started/get-started-sdk/project-implementation/android.mdx b/shared/video-sdk/get-started/get-started-sdk/project-implementation/android.mdx index a950b545a..0a637c100 100644 --- a/shared/video-sdk/get-started/get-started-sdk/project-implementation/android.mdx +++ b/shared/video-sdk/get-started/get-started-sdk/project-implementation/android.mdx @@ -18,25 +18,28 @@ To use the sample code, copy the following lines into the `/app/src/main/java/co -{`import androidx.appcompat.app.AppCompatActivity; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; - +{` import android.Manifest; import android.content.pm.PackageManager; +import android.os.Bundle; import android.view.SurfaceView; import android.widget.FrameLayout; +import android.widget.Toast; -import android.os.Bundle; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +import io.agora.rtc2.ChannelMediaOptions; import io.agora.rtc2.Constants; import io.agora.rtc2.IRtcEngineEventHandler; import io.agora.rtc2.RtcEngine; import io.agora.rtc2.RtcEngineConfig; import io.agora.rtc2.video.VideoCanvas; -import io.agora.rtc2.ChannelMediaOptions; public class MainActivity extends AppCompatActivity { - // Fill in the app ID from Agora Console + // Fill in the App ID obtained from the Agora Console private String appId = ""; // Fill in the channel name private String channelName = ""; @@ -46,17 +49,33 @@ public class MainActivity extends AppCompatActivity { private RtcEngine mRtcEngine; private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() { + // Callback when successfully joining the channel + @Override + public void onJoinChannelSuccess(String channel, int uid, int elapsed) { + super.onJoinChannelSuccess(channel, uid, elapsed); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "Join channel success", Toast.LENGTH_SHORT).show(); + }); + } + + // Callback when a remote user or host joins the current channel @Override - // Monitor remote users in the channel and obtain their uid public void onUserJoined(int uid, int elapsed) { - runOnUiThread(new Runnable() { - @Override - public void run() { - // After obtaining uid, set up the remote video view - setupRemoteVideo(uid); - } + runOnUiThread(() -> { + // When a remote user joins the channel, display the remote video stream for the specified uid + setupRemoteVideo(uid); }); } + + // Callback when a remote user or host leaves the current channel + @Override + public void onUserOffline(int uid, int reason) { + super.onUserOffline(uid, reason); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "User offline: " + uid, Toast.LENGTH_SHORT).show(); + }); + } + }; private void initializeAndJoinChannel() { @@ -91,8 +110,8 @@ public class MainActivity extends AppCompatActivity { // In the video calling scenario, set the channel profile to CHANNEL_PROFILE_COMMUNICATION options.channelProfile = Constants.CHANNEL_PROFILE_COMMUNICATION; - // Use the temporary token to join the channel - // Specify the user ID yourself and ensure it is unique within the channel + // Join the channel using a temporary token and channel name, setting uid to 0 means the engine will randomly generate a username + // The onJoinChannelSuccess callback will be triggered upon success mRtcEngine.joinChannel(token, channelName, 0, options); } @@ -135,6 +154,15 @@ public class MainActivity extends AppCompatActivity { return true; } + // System permission request callback + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (checkPermissions()) { + initializeAndJoinChannel(); + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -147,6 +175,15 @@ public class MainActivity extends AppCompatActivity { } } + // System permission request callback + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (checkPermissions()) { + initializeAndJoinChannel(); + } + } + @Override protected void onDestroy() { super.onDestroy(); @@ -161,22 +198,24 @@ public class MainActivity extends AppCompatActivity { -{`import androidx.appcompat.app.AppCompatActivity; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; - -import android.Manifest; +{`import android.Manifest; import android.content.pm.PackageManager; +import android.os.Bundle; import android.view.SurfaceView; import android.widget.FrameLayout; +import android.widget.Toast; -import android.os.Bundle; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +import io.agora.rtc2.ChannelMediaOptions; import io.agora.rtc2.Constants; import io.agora.rtc2.IRtcEngineEventHandler; import io.agora.rtc2.RtcEngine; import io.agora.rtc2.RtcEngineConfig; import io.agora.rtc2.video.VideoCanvas; -import io.agora.rtc2.ChannelMediaOptions; public class MainActivity extends AppCompatActivity { // Fill in the app ID from Agora Console @@ -189,15 +228,30 @@ public class MainActivity extends AppCompatActivity { private RtcEngine mRtcEngine; private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() { + // Callback when successfully joining the channel + @Override + public void onJoinChannelSuccess(String channel, int uid, int elapsed) { + super.onJoinChannelSuccess(channel, uid, elapsed); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "Join channel success", Toast.LENGTH_SHORT).show(); + }); + } + + // Callback when a remote user or host joins the current channel @Override - // Monitor remote users in the channel and obtain their uid public void onUserJoined(int uid, int elapsed) { - runOnUiThread(new Runnable() { - @Override - public void run() { - // After obtaining uid, set up the remote video view - setupRemoteVideo(uid); - } + runOnUiThread(() -> { + // When a remote user joins the channel, display the remote video stream for the specified uid + setupRemoteVideo(uid); + }); + } + + // Callback when a remote user or host leaves the current channel + @Override + public void onUserOffline(int uid, int reason) { + super.onUserOffline(uid, reason); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "User offline: " + uid, Toast.LENGTH_SHORT).show(); }); } }; @@ -280,6 +334,15 @@ public class MainActivity extends AppCompatActivity { return true; } + // System permission request callback + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (checkPermissions()) { + initializeAndJoinChannel(); + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -306,22 +369,24 @@ public class MainActivity extends AppCompatActivity { -{`import androidx.appcompat.app.AppCompatActivity; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; - -import android.Manifest; +{`import android.Manifest; import android.content.pm.PackageManager; +import android.os.Bundle; import android.view.SurfaceView; import android.widget.FrameLayout; +import android.widget.Toast; -import android.os.Bundle; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +import io.agora.rtc2.ChannelMediaOptions; import io.agora.rtc2.Constants; import io.agora.rtc2.IRtcEngineEventHandler; import io.agora.rtc2.RtcEngine; import io.agora.rtc2.RtcEngineConfig; import io.agora.rtc2.video.VideoCanvas; -import io.agora.rtc2.ChannelMediaOptions; public class MainActivity extends AppCompatActivity { // Fill in the app ID from Agora Console @@ -334,15 +399,30 @@ public class MainActivity extends AppCompatActivity { private RtcEngine mRtcEngine; private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() { + // Callback when successfully joining the channel + @Override + public void onJoinChannelSuccess(String channel, int uid, int elapsed) { + super.onJoinChannelSuccess(channel, uid, elapsed); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "Join channel success", Toast.LENGTH_SHORT).show(); + }); + } + + // Callback when a remote user or host joins the current channel @Override - // Monitor remote users in the channel and obtain their uid public void onUserJoined(int uid, int elapsed) { - runOnUiThread(new Runnable() { - @Override - public void run() { - // After obtaining uid, set up the remote video view - setupRemoteVideo(uid); - } + runOnUiThread(() -> { + // When a remote user joins the channel, display the remote video stream for the specified uid + setupRemoteVideo(uid); + }); + } + + // Callback when a remote user or host leaves the current channel + @Override + public void onUserOffline(int uid, int reason) { + super.onUserOffline(uid, reason); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "User offline: " + uid, Toast.LENGTH_SHORT).show(); }); } }; @@ -425,6 +505,15 @@ public class MainActivity extends AppCompatActivity { return true; } + // System permission request callback + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (checkPermissions()) { + initializeAndJoinChannel(); + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -456,19 +545,22 @@ public class MainActivity extends AppCompatActivity { Complete sample code for real-time -{`import androidx.appcompat.app.AppCompatActivity; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; - +{` import android.Manifest; import android.content.pm.PackageManager; - import android.os.Bundle; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +import io.agora.rtc2.ChannelMediaOptions; import io.agora.rtc2.Constants; import io.agora.rtc2.IRtcEngineEventHandler; import io.agora.rtc2.RtcEngine; import io.agora.rtc2.RtcEngineConfig; -import io.agora.rtc2.ChannelMediaOptions; public class MainActivity extends AppCompatActivity { @@ -484,9 +576,25 @@ public class MainActivity extends AppCompatActivity { private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() { @Override - // Listen for remote hosts in the channel to obtain the UID information of the hosts + public void onJoinChannelSuccess(String channel, int uid, int elapsed) { + super.onJoinChannelSuccess(channel, uid, elapsed); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "Join channel success", Toast.LENGTH_SHORT).show(); + }); + } + @Override public void onUserJoined(int uid, int elapsed) { + super.onUserJoined(uid, elapsed); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "User joined: " + uid, Toast.LENGTH_SHORT).show(); + }); } + @Override + public void onUserOffline(int uid, int reason) { + super.onUserOffline(uid, reason); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "User offline: " + uid, Toast.LENGTH_SHORT).show(); + }); }; private void initializeAndJoinChannel() { @@ -504,12 +612,13 @@ public class MainActivity extends AppCompatActivity { // Create a ChannelMediaOptions object and configure it ChannelMediaOptions options = new ChannelMediaOptions(); - // Set the channel profile to BROADCASTING (live broadcasting scenario) - options.channelProfile = Constants.CHANNEL_PROFILE_COMMUNICATION; // Set the user role to BROADCASTER (host) or AUDIENCE (audience) options.clientRoleType = Constants.CLIENT_ROLE_BROADCASTER; + // Set the channel profile to BROADCASTING (live broadcasting scenario) + options.channelProfile = Constants.CHANNEL_PROFILE_COMMUNICATION; - // Join the channel using a temporary Token, specify the user ID, and ensure its uniqueness within the channel + // Join the channel using a temporary token and channel name, setting uid to 0 means the engine will randomly generate a username + // The onJoinChannelSuccess callback will be triggered upon success mRtcEngine.joinChannel(token, channelName, 0, options); } @@ -553,6 +662,14 @@ public class MainActivity extends AppCompatActivity { } } + // System permission request callback + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (checkPermissions()) { + initializeAndJoinChannel(); + } + } @Override protected void onDestroy() { @@ -729,10 +846,33 @@ mRtcEngine = RtcEngine.create(config); private RtcEngine mRtcEngine; private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() { - @Override - // Listen for remote hosts within the channel and obtain their UID information - public void onUserJoined(int uid, int elapsed) { - } + // Callback when successfully joining the channel + @Override + public void onJoinChannelSuccess(String channel, int uid, int elapsed) { + super.onJoinChannelSuccess(channel, uid, elapsed); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "Join channel success", Toast.LENGTH_SHORT).show(); + }); + } + + // Callback when a remote user or host joins the current channel + @Override + // Listen for remote hosts in the channel to get the host's uid information + public void onUserJoined(int uid, int elapsed) { + super.onUserJoined(uid, elapsed); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "User joined: " + uid, Toast.LENGTH_SHORT).show(); + }); + } + + // Callback when a remote user or host leaves the current channel + @Override + public void onUserOffline(int uid, int reason) { + super.onUserOffline(uid, reason); + runOnUiThread(() -> { + Toast.makeText(MainActivity.this, "User offline: " + uid, Toast.LENGTH_SHORT).show(); + }); + } }; // Create an RtcEngineConfig object and configure it diff --git a/shared/video-sdk/get-started/get-started-sdk/reference/android.mdx b/shared/video-sdk/get-started/get-started-sdk/reference/android.mdx index b4f00fcfa..92d112668 100644 --- a/shared/video-sdk/get-started/get-started-sdk/reference/android.mdx +++ b/shared/video-sdk/get-started/get-started-sdk/reference/android.mdx @@ -3,7 +3,7 @@ ### Sample project -Agora provides open source sample projects on [GitHub](https://github.com/AgoraIO/API-Examples) for your reference. Download or view the [JoinChannelVideo](https://github.com/AgoraIO/API-Examples/blob/main/Android/APIExample/app/src/main/java/io/agora/api/example/examples/basic/JoinChannelVideo.java) project for a more detailed example. +Agora provides open source sample projects on [GitHub](https://github.com/AgoraIO/API-Examples) for your reference. Download or view the [JoinChannelVideo](https://github.com/AgoraIO-Community/Agora-RTC-QuickStart/tree/main/Android/Agora-RTC-QuickStart-Android) project for a more detailed example.